Operators in Python (Arithmetic, Comparison, Logical)
1. Introduction
Understanding operators in Python (Arithmetic, Comparison, Logical) is essential for performing calculations, making decisions, and controlling the flow of a program. Operators help manipulate data and perform different operations like addition, subtraction, comparisons, and logical evaluations.

In this guide, we will cover arithmetic, comparison, and logical operators, their syntax, real-life applications, common mistakes, and how to fix them.
Focus Keyphrase: Operators in Python (Arithmetic, Comparison, Logical)
2. Arithmetic Operators in Python
Arithmetic operators perform basic mathematical calculations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 2 | 12 |
/ | Division | 9 / 3 | 3.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Example of Arithmetic Operators
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.3333
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponentiation:", a ** b) # 1000
Real-Life Application:
- E-commerce Websites: Calculating total price, discounts, and taxes.
- Banking Applications: Computing loan interest and EMI payments.
3. Comparison Operators in Python
Comparison operators compare two values and return True
or False
.
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 5 < 10 | True |
>= | Greater than or equal to | 10 >= 10 | True |
<= | Less than or equal to | 5 <= 3 | False |
Example of Comparison Operators
x = 15
y = 10
print(x > y) # True
print(x < y) # False
print(x == y) # False
print(x != y) # True
print(x >= 15) # True
print(y <= 10) # True
Real-Life Application:
- Authentication Systems: Checking if the entered password matches the stored password.
- Age Verification Systems: Determining if a user is eligible for certain services.
4. Logical Operators in Python
Logical operators combine multiple conditions and return True
or False
.
Operator | Description | Example | Output |
---|---|---|---|
and | Returns True if both conditions are True | (5 > 2) and (10 > 3) | True |
or | Returns True if at least one condition is True | (5 < 2) or (10 > 3) | True |
not | Reverses the result (negation) | not(5 > 2) | False |
Example of Logical Operators
a = 5
b = 10
c = 15
print((a < b) and (b < c)) # True
print((a > b) or (b < c)) # True
print(not (a > b)) # True
Real-Life Application:
- Login Systems: Checking if both username and password are correct (
and
). - Search Filters: Filtering products with “in stock” OR “on sale” conditions.
5. Common Mistakes and How to Fix Them
Mistake 1: Using =
Instead of ==
in Comparisons
❌ Incorrect:
if age = 18: # Error: Single '=' is used for assignment
print("You are an adult.")
✅ Fix: Use ==
for comparisons.
if age == 18:
print("You are an adult.")
Mistake 2: Integer Division Instead of Float Division
❌ Incorrect:
result = 5 / 2 # Output: 2.5 (Expected, but may cause issues in old Python 2 versions)
✅ Fix: If integer division is needed, use //
.
result = 5 // 2 # Output: 2
Mistake 3: Incorrect Logical Operator Usage
❌ Incorrect:
if age > 18 and age < 30 or age == 40:
print("Eligible")
✅ Fix: Use parentheses for clarity.
if (age > 18 and age < 30) or (age == 40):
print("Eligible")
6. Conclusion
Operators in Python (Arithmetic, Comparison, Logical) are essential for performing calculations, making decisions, and controlling program flow. Mastering these operators helps in writing efficient and error-free code. Learn more advanced operators
Next Steps:
- Experiment with different Python operators.
- Use operators in real-life projects like calculators and data analysis.
- Learn about bitwise and assignment operators for advanced programming.