Welcome back, coding crew! π Today, we are turning Python into the most overpowered pocket calculator youβve ever seen.
Whether youβre calculating score multipliers in a video game, adding sales tax to an e-commerce shopping cart, or computing data averages, you need Arithmetic Operators.
You already know the basics from grade school math, but Python has a few secret mathematical superpowers hidden up its sleeve. Let's look at them! π’
π§± The Standard Four
Addition (+), Subtraction (-), Multiplication (*), and Division (/) work exactly how youβd expect:
print(10 + 5) # Addition -> 15
print(10 - 5) # Subtraction -> 5
print(10 * 5) # Multiplication -> 50
print(10 / 5) # Division -> 2.0
π‘ Pro-Tip: Did you notice that 10 / 5 gave us 2.0 instead of a whole number 2? In Python, standard division always returns a float, even if it divides perfectly!
π The Python Math Superpowers
Now for the fun stuff that they didn't teach you in standard calculator class:
1. Exponents ()
Want to calculate exponents (powers)? Use two asterisks!
print(2 ** 3) # Meaning: 2 cubed (2 * 2 * 2) -> 8
2. Floor Division (//)
Want to divide numbers but completely chop off the decimal part to get a clean whole number integer? Use double slashes!
print(11 // 3) # 3 goes into 11 three times... -> 3
3. Modulo (%)
This is the ultimate programmer favorite. Modulo divides the first number by the second number and returns only the leftover remainder.
print(11 % 3) # 11 divided by 3 leaves a remainder of -> 2
Why is Modulo useful? If any_number % 2 gives you 0, you instantly know the number is even! If it leaves a 1, it's odd! π€―
π Today's Challenge π
Create a variable called total_cookies = 23.
You have 4 friends. Use floor division (//) to find out how many whole cookies each friend gets.
Use Modulo (%) to find out how many leftover cookies you get to keep for yourself!
Print both results with clean labels.
Let me know how many cookies you kept in the comments! Tomorrow, we learn how to make our code compare values using Comparison Operators! βοΈ
Top comments (0)