Different Types of Operators in Python
1. Warm-up (10 mins)
Ask:
• What is 10 + 5?
• What is 9 % 2?
• Is 12 greater than 20?
• Can we use Python to check that?
Mini magic moment:
print(10 + 5) 15
print(9 % 2) 1
print(12 > 20) False
These symbols (+, >, %) are called operators. They help Python do math, compare things, and
make decisions.
#write a program to take two numbers as input and print which one is greater.
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
if a>b:
print(a, "is greater than", b)
elif a<b:
print(b, "is greater than", a)
else:
print(a, "is equals to ", b)
2. Mini Lesson: Types of Operators (25 mins)
1. Arithmetic Operators — (Do Math): This operator does the mathematical calculations.
Operator Action Example Output
+ Add 7+3 10
- Subtract 9-4 5
* Multiply 6*3 18
/ Divide 8/2 4.0
// Floor Divide (Integer division) 9 // 2 4
% Remainder (mod) 9%2 1
** Power (exponent) 2 ** 3 8
Testing in interactive mode:
Examples:
print("Add:", 10 + 5)
print("Subtract:", 15 - 4)
print("Multiply:", 3 * 7)
print("Divide:", 12 / 4)
print("Floor Divide:", 13 // 4)
print("Remainder:", 13 % 4)
print("Power:", 2 ** 5)
2. Assignment Operators — (Store values): This operator is used to assign or store some value
to a variable.
Operator Action Example Result
= Assign x = 10 Value of x becomes 10
+= Add and assign x += 5 x=x+5
-= Subtract and assign x -= 2 x=x-2
*= Multiply and assign x *= 3 x=x*3
/= Divide and assign x /= 2 x=x/2
Tested in interactive mode:
Examples:
x = 10
x += 5 # x = x + 5
print("x after += :", x)
y = 20
y -= 3 # y = y - 3
print("y after -= :", y)
z=4
z *= 2
print("z after *= :", z)
Challenges:
1. Write a program to calculate the area and perimeter of a rectangle given its length and
width. Formula of calculating area and perimeter is given below:
Area= Length x width, Perimeter= 2 x (Length+Width)
2. Write a program to calculate the average of three numbers.
3. Write a program to calculate the simple interest given the principal amount, rate of interest,
and time period.
Simple Interest=(Principal amount x time period x rate of Interest)/100
4. Write a program to calculate the square and cube of a number.
5. Write a program to calculate the BMI (Body Mass Index) given the weight and height.
BMI=weight/ (height)2
Where weight is measured in Kg and height is in meter.