DEV Community

Bonface Thuo
Bonface Thuo

Posted on

βž• DAY 8: Arithmetic Operators

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

πŸ’‘ 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
Enter fullscreen mode Exit fullscreen mode

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

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

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)