DEV Community

Cover image for pythoonzie DAY 5!
Bhartiprof
Bhartiprof

Posted on

pythoonzie DAY 5!

_

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

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
Enter fullscreen mode Exit fullscreen mode

Example:

2 + 3 * 4      # 14
(2 + 3) * 4    # 20
Enter fullscreen mode Exit fullscreen mode

👉 Brackets change priority.


4️⃣ Associativity Rule

Exponent (**) works Right → Left

2 ** 3 ** 2
Enter fullscreen mode Exit fullscreen mode

Means:

2 ** (3 ** 2)
Enter fullscreen mode Exit fullscreen mode

5️⃣ Common Practical Formulas

📌 Average

avg = (p + c + m) / 3
Enter fullscreen mode Exit fullscreen mode

📌 Algebra Expansion

(a**3) + (b**3) + 3*(a**2)*b + 3*a*(b**2)
Enter fullscreen mode Exit fullscreen mode

6️⃣ Quick Memory Tricks

  • / → decimal answer
  • // → remove decimal
  • % → remainder
  • ** → power
  • () → VIP control

#python #Day1 #DataScience #bhartiinsan

Top comments (0)