Comprehensive Python and Django Course

Day 2: Basic Operations and Control Flow - Building on Your Python Foundation


Welcome back! Today, we'll dive deeper into Python by exploring basic operations, control flow, and more hands-on exercises. By the end of this lesson, you'll be comfortable performing arithmetic operations, using comparison and logical operators, and writing conditional statements.


Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations. Here are some of the most common ones:

+ (Addition)

- (Subtraction)

* (Multiplication)

/ (Division)

% (Modulus)

** (Exponentiation)

// (Floor Division)


Examples:

print(5 + 3)    # 8

print(5 - 3)    # 2

print(5 * 3)    # 15

print(5 / 3)    # 1.666...

print(5 % 3)    # 2

print(5 ** 3)   # 125

print(5 // 3)   # 1


Comparison and Logical Operators

  • == (Equal to)
  • != (Not equal to)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)
  • and (Logical AND)
  • or (Logical OR)
  • not (Logical NOT)

Comparison operators compare two values and return True or False. Logical operators combine multiple conditions.

Comparison Operators:

Logical Operators:

and (Logical AND)

or (Logical OR)

not (Logical NOT)


Examples:

print(5 > 3)     # True

print(5 == 5)    # True

print(5 != 3)    # True

print(5 > 3 and 5 < 10)  # True

print(5 > 3 or 5 < 2)    # True

print(not 5 > 3)         # False

Conditional Statements


Conditional statements allow you to execute code based on certain conditions using if, elif, and else.


Example:

x = 10

if x > 5:

    print("x is greater than 5")

elif x == 5:

    print("x is exactly 5")

else:

    print("x is less than 5")


Hands-On Practice

Let's practice these concepts with some exercises.


Exercise 1: 

Positive, Negative, or Zero Write a program to determine if a number is positive, negative, or zero.

number = int(input("Enter a number: "))

if number > 0:

    print("The number is positive.")

elif number < 0:

    print("The number is negative.")

else:

    print("The number is zero.")


Exercise 2: Basic Arithmetic 

Perform basic arithmetic operations and print the results.

a = 10

b = 3

print("Addition:", a + b)

print("Subtraction:", a - b)

print("Multiplication:", a * b)

print("Division:", a / b)

print("Modulus:", a % b)


Exercise 3: Find the Largest of Three Numbers 

Write a program to find the largest of three numbers.

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):

    largest = num1

elif (num2 >= num1) and (num2 >= num3):

    largest = num2

else:

    largest = num3

print("The largest number is", largest)


Wrap-Up and Q&A

  • Today, we explored arithmetic, comparison, and logical operators.
  • We learned how to use conditional statements to control the flow of our programs.
  • If you have any questions or need further clarification on any topics, feel free to ask.
  • Write a program to check if a number is even or odd.

Review:

  • Today, we explored arithmetic, comparison, and logical operators.
  • We learned how to use conditional statements to control the flow of our programs.
  • Homework:

    • Experiment with the concepts learned today by writing small programs.

    • Example tasks:

    • Write a program to check if a number is even or odd.


    num = int(input("Enter a number: "))

    if num % 2 == 0:
        print("The number is even.")

    else:
        print("The number is odd.")

    .Write a program to calculate the grade based on a score.

    score = float(input("Enter your score: "))

    if score >= 90:
        grade = 'A'

    elif score >= 80:
        grade = 'B'

    elif score >= 70:
        grade = 'C'

    elif score >= 60:
        grade = 'D'

    else:
        grade = 'F'
    print("Your grade is:", grade)

    Congratulations on completing your second day of Python! Tomorrow, we'll dive into loops and iterations. Stay tuned!



    Comments

    Popular posts from this blog

    Lesson 3: Basic Python Operations

    🚀 Python Quest: Unleashing the Magic of Conditional Statements