What are Operators?
Operators are special symbols used to perform operations on values and variables.
In
10 + 5, + is an operator.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
+ Addition - Subtraction * Multiplication / Division % Modulus // Floor Division ** Exponentiation
Example
a = 10 b = 3 print(a + b) print(a - b) print(a * b) print(a / b) print(a % b) print(a // b) print(a ** b)
13 7 30 3.3333333333333335 1 3 1000
2. Comparison Operators
Comparison operators compare two values.
They return either True or
False.
== Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
Example
a = 10 b = 5 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)
False True True False True False
3. Assignment Operators
Assignment operators are used to assign or update values in variables.
= Assign += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign
Example
x = 10 x += 5 print(x) x -= 3 print(x) x *= 2 print(x)
15 12 24
4. Logical Operators
Logical operators are used to combine multiple conditions.
and True if both conditions are True or True if at least one condition is True not Reverses the result
Example
age = 20 print(age >= 18 and age <= 60) print(age < 18 or age > 60) print(not(age >= 18))
True False False
5. Practical Example
Operators can be used together to solve real programming problems.
math = 80
python = 90
dsa = 85
total = math + python + dsa
average = total / 3
print("Total =", total)
print("Average =", average)
Total = 255 Average = 85.0
Quick Quiz
Which operator is used to find the remainder?
Finished this lesson?