Home → Lessons → Python Operators

Python Operators

Learn how operators are used to perform calculations and compare values in Python.

Lesson 05 Beginner 15 min

What are Operators?

Operators are special symbols used to perform operations on values and variables.

Example:

In 10 + 5, + is an operator.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Arithmetic Operators
+   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)
Output:
13
7
30
3.3333333333333335
1
3
1000

2. Comparison Operators

Comparison operators compare two values. They return either True or False.

Comparison Operators
==   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)
Output:
False
True
True
False
True
False

3. Assignment Operators

Assignment operators are used to assign or update values in variables.

Assignment Operators
=    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)
Output:
15
12
24

4. Logical Operators

Logical operators are used to combine multiple conditions.

Logical Operators
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))
Output:
True
False
False

5. Practical Example

Operators can be used together to solve real programming problems.

Calculate Total Marks
math = 80
python = 90
dsa = 85

total = math + python + dsa
average = total / 3

print("Total =", total)
print("Average =", average)
Output:
Total = 255
Average = 85.0

Quick Quiz

Which operator is used to find the remainder?

Finished this lesson?