_
Today I explored Python operators and understood how they work as the backbone of programming logic. Operators are special symbols used to perform operations on operands, which can be values, variables, or expressions. I practiced arithmetic operators like addition, subtraction, multiplication, exponent, division, floor division, and modulus. I learned the difference between true division (which gives decimal results) and floor division (which removes decimals), and how modulus returns the remainder, which is useful in real-life scenarios like grouping or hashing. I also understood operator precedence, where Python follows an order of operations similar to BODMAS, and how parentheses help control execution priority. I discovered that exponentiation follows right-to-left associativity, which was an interesting concept. Additionally, I applied operators to solve practical problems like calculating averages and expanding algebraic formulas. Overall, I gained clarity on how operators control calculations and logical flow in Python programs.
_
π Python Operators Cheat Sheet (Quick Reuse Guide)
πΉ What is an Operator?
An operator performs an action on operands (values, variables, expressions).
1οΈβ£ Arithmetic Operators
| Operator | Meaning | Example | Output |
|---|---|---|---|
+ |
Addition | 10 + 4 |
14 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 10 * 4 |
40 |
/ |
True Division | 10 / 4 |
2.5 |
// |
Floor Division | 10 // 4 |
2 |
% |
Modulus (Remainder) | 10 % 4 |
2 |
** |
Exponent (Power) | 2 ** 3 |
8 |
2οΈβ£ Important Rules
β
True Division (/)
- Returns decimal
- Used when accuracy matters
β
Floor Division (//)
- Removes decimal part
- Used when whole number required
β
Modulus (%)
- Returns remainder
-
Useful for:
- Even/Odd check β
num % 2 - Grouping / hashing logic
- Even/Odd check β
3οΈβ£ Operator Precedence (Order of Execution)
Python follows:
1. () - LEFT TO RIGHT ASSOCIATIVITY
2. ** - RIGHT TO LEFT ASSOCIATIVITY
3. *, /, //, % - LEFT TO RIGHT ASSOCIATIVITY
4. +, - - LEFT TO RIGHT ASSOCIATIVITY
Example:
2 + 3 * 4 # 14
(2 + 3) * 4 # 20
π Brackets change priority.
4οΈβ£ Associativity Rule
Exponent (**) works Right β Left
2 ** 3 ** 2
Means:
2 ** (3 ** 2)
5οΈβ£ Common Practical Formulas
π Average
avg = (p + c + m) / 3
π Algebra Expansion
(a**3) + (b**3) + 3*(a**2)*b + 3*a*(b**2)
6οΈβ£ Quick Memory Tricks
-
/β decimal answer -
//β remove decimal -
%β remainder -
**β power -
()β VIP control
#python #Day1 #DataScience #bhartiinsan
Top comments (0)