Operators are special symbols in Python that perform mathematical or logical tasks. They are the building blocks of any program! ✨
1. Arithmetic Operators ➕
Used for basic math calculations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 4 + 2 | 6 |
| % | Modulus (Remainder) | 5 % 2 | 1 |
| ** | Exponent (Power) | 5 ** 2 | 25 |
| / | Division | 7 / 2 | 3.5 |
| // | Floor Division | 7 // 2 | 3 |
2. Comparison Operators ⚖️
These return either True or False.
| Symbol | Meaning | Example (5 ? 2) | Result |
|---|---|---|---|
| == | Equal to | 5 == 2 | False |
| != | Not equal to | 5 != 2 | True |
| > | Greater than | 5 > 2 | True |
| >= | Greater than or equal to | 5 >= 2 | True |
3. Logical Operators 🧠
- and: Returns True if both statements are true.
- or: Returns True if at least one statement is true.
- not: Reverses the result (True becomes False).
logical operators
4. Identity Operators 🆔
Used to check if two variables are the same object in memory.
x = 2
y = 2
print(x is y) # Output: True
y = 2
print(x is y) # Output: True
![]() |
| python reuses small integers in memory |
*
In Python 🐍, small integers from -5 to 256 are created and stored in memory when the interpreter starts 🧠
These numbers are cached (saved) in advance ⚡
So when you use any number in this range, Python reuses the same memory value instead of creating a new one 🔁
⚡ Precedence: Which one goes first?
| Operator precedence table |
Solve:Example 1 (Different Level): 10 + 5 * 2 ** 2
- Power (**): 2 ** 2 = 4
- Multiply (*): 5 * 4 = 20
- Add (+): 10 + 20 = 30 ✅
Example 2 (Same Level - Left to Right): 20 / 2 * 5 % 4
- 1. Division: 20 / 2 = 10.0
- 2. Multiply: 10.0 * 5 = 50.0
- 3. Modulus: 50.0 % 4 = 2.0 ✅
📖 Vocabulary
Operand: The value or variable that an operator works on.
Precedence: The order in which operations are performed.
