Skip to content

Arithmetic Operators πŸš€ΒΆ

Prerequisites: Numbers & Type Casting

Mentor's Note: Think of your computer as a super-fast calculator. Arithmetic operators are the buttons on that calculator! πŸ’‘


🌟 The Scenario: The Recipe Scaler πŸ•ΒΆ

Imagine you have a recipe for 1 pizza, but you have 5 friends coming over.

  • The Logic: You need to multiply the flour, add more cheese, and divide the final pizza into 6 slices. πŸ“¦
  • The Result: A perfect party! In Python, these symbols (+, -, *, /) are the tools you use to "scale" your data. βœ…

πŸ“– Concept ExplanationΒΆ

-- 🧠 The Logic (Common to all) --

What are Arithmetic Operators?ΒΆ

Arithmetic operators are used to perform mathematical calculations like addition, subtraction, and multiplication. They are the most basic building blocks of programming logic.

Operator Operation Description
+ Addition Adds two values together
- Subtraction Subtracts the right value from the left value
* Multiplication Multiplies two values
/ Division Divides the left value by the right value
% Modulus Returns the remainder after division

Note: While the basic behavior is the same across languages, how they handle things like "Integer Division" (e.g., 5 / 2) can vary.

Python's "Special Powers" ✨¢

Operator Name Logic Example
// Floor Division Divides and rounds DOWN to nearest whole number. 10 // 3 = 3
** Exponentiation Raises to the power. 2 ** 3 = 8

🎨 Visual Logic: The Modulus Analogy β­•ΒΆ

Think of % (Modulus) as the leftover after sharing fairly. - 7 Cookies shared between 3 people. - Each gets 2. - 1 Cookie is left. -> 7 % 3 = 1.


πŸ’» Implementation: The Math LabΒΆ

# πŸ›’ Scenario: Splitting a Bill
# πŸš€ Action: Using different operators

total_bill = 1000
people = 3

# πŸ’² Floating Division
exact_share = total_bill / people

# πŸ”’ Floor Division (The whole amount)
base_share = total_bill // people

# πŸͺ™ Modulus (The leftover change)
leftover = total_bill % people

# πŸ›οΈ Outcome:
print(f"Exact: {exact_share}") # 333.333...
print(f"Base: {base_share}")   # 333
print(f"Remainder: {leftover}") # 1

πŸ“Š Sample Dry Run (Precedence)ΒΆ

How Python thinks for: 10 + 2 * 3

Step Operation Result Why?
1 2 * 3 6 Multiplication happens first (BODMAS) βš™οΈ
2 10 + 6 16 Addition happens last.

πŸ“ˆ Technical AnalysisΒΆ

  • Precedence: () > ** > * , / , // , % > + , -.
  • Performance: // is slightly faster than / because it doesn't need to track decimal precision.

πŸ§ͺ Interactive Lab πŸ§ͺΒΆ

Hands-on Exercise

Task: Write a program that takes a number and uses the Modulus operator to check if it's even. Hint: If number % 2 == 0, it's Even! πŸ’‘

Test Your KnowledgeΒΆ

Quick Quiz

Which operator would you use to find the remainder of a division? - [ ] / - [ ] // - [x] % - [ ] **

Explanation: The Modulus (%) operator returns the remainder. For example, 10 % 3 is 1.


πŸ’‘ Pro Tip: "Knowledge is power." - Francis Bacon


πŸ“ˆ Learning PathΒΆ