Imagine Python as a magical world where values are little creatures…
And operators are the spells that make them move, fight, compare, and transform. 🪄🐍
Every time you write +, ==, or and, you’re basically whispering a command to Python:
“Hey, do this. And be quick about it.”
Let’s explore the most important operators — in a fun and interactive way!
1. Introduction: What Are Operators?
Operators are symbols or keywords that tell Python what action to perform.
Think of them as:
- The plus sign waving its hand and saying, “Let’s join forces!”
- The comparison operators judging things like a reality show panel 👀
- The logical operators making decisions like a wise monk.
You use operators every single time you code — even if you don’t notice.
2. Arithmetic Operators (Your Math Superheroes)
These operators work with numbers.
They’re the Avengers of the Python world (but with less CGI).
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 6 * 4 | 24 |
/ | Division | 10 / 4 | 2.5 |
// | Floor Division | 10 // 4 | 2 (no decimals) |
% | Modulus | 10 % 4 | 2 (remainder) |
** | Exponent | 3 ** 3 | 27 |
Quick Example
a = 15
b = 4
print(a // b) # Output: 3
print(a % b) # Output: 3
Floor division and modulus are siblings:
one gives the quotient, the other gives the remainder.
3. Comparison Operators (Judging Mode ON 😎)
These operators compare two values.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 8 | True |
< | Less than | 2 < 9 | True |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 3 <= 5 | True |
Example
age = 18
print(age >= 18) # True
Python’s way of saying:
“You’re officially an adult… in this variable.”
4. Logical Operators (The Wise Decision-Makers)
These operators combine conditions.
Like checking multiple items on your to-do list.
AND
All conditions must be true.
age = 25
has_id = True
print(age >= 18 and has_id) # True
OR
At least one condition must be true.
is_member = False
has_coupon = True
print(is_member or has_coupon) # True
NOT
Flips True to False and vice-versa.
is_raining = False
print(not is_raining) # True
Logical operators = Python’s brain. 🧠
5. Practical Examples & Common Mistakes
✔ Example 1 — Checking if a number is even
num = 12
if num % 2 == 0:
print("Even number!")
✔ Example 2 — Login check
username = "amit"
password = "1234"
if username == "amit" and password == "1234":
print("Welcome back!")
❌ Common Mistake 1 — Mixing = and ==
= is assignment== is comparison
x = 10 # correct
x == 10 # comparison only
❌ Common Mistake 2 — Confusing / and //
/ → decimal// → whole number

Leave a Reply