DEV Community

SAINAPEI LENAPUNYA
SAINAPEI LENAPUNYA

Posted on

Day 5: Arithmetic Operators - Python as Your Calculator - 30 Days of Python Challenge

Welcome Back to Day 5! đź‘‹

Hey everyone! It's Day 5 of my 30 Days of Python Challenge, and today we're turning Python into our personal calculator!

If you missed the previous days:

  • [Day 1: Print Statements]
  • [Day 2: Variables and Data Types]
  • [Day 3: Type Casting]
  • [Day 4: User Input]

Today, we're learning how to perform mathematical operations in Python. Let's crunch some numbers!

🔢 Day 5: Arithmetic Operators - Doing the Math

Today's mission: Arithmetic Operators. Python isn't just for text and data—it's incredibly powerful for calculations! Whether you're building a calculator app, analyzing data, or solving problems, understanding arithmetic is essential.

What I Learned

Python has a full set of arithmetic operators that let you:

  • Perform basic calculations (add, subtract, multiply, divide)
  • Do advanced operations (exponentiation, modulus)
  • Work with different types of division
  • Build the foundation for data analysis and algorithms

Some of these operators surprised me—especially floor division and modulus!

My Code

Here's what I wrote for Day 5:

# Day 5 - Arithmetic
import math

a = 8
b = 3

#Addition
print("a + b =", a + b)

#Subtraction
print("a - b =", a - b)

#Multiplication
print("a * b =", a * b)

#Division
print("a / b =", a / b)

#Floor Division
print("a // b =", a // b)

#Exponentiation
print("a ** b =", a ** b)

#Modulus
print("a % b =", a % b)
Enter fullscreen mode Exit fullscreen mode

Breaking It Down 🔍

Let me explain each arithmetic operator:

  1. a + b (Addition) - Simple addition! 8 + 3 = 11. This one's straightforward.

  2. a - b (Subtraction) - Basic subtraction. 8 - 3 = 5.

  3. a * b (Multiplication) - Use the asterisk * for multiplication. 8 Ă— 3 = 24.

  4. a / b (Division) - Regular division that gives you a decimal result. 8 Ă· 3 = 2.666...

  5. a // b (Floor Division) - This was new to me! Floor division gives you the whole number part only, rounding down. 8 Ă· 3 = 2 (ignoring the remainder). Super useful!

  6. a ** b (Exponentiation) - This raises a number to a power. 8Âł (8 Ă— 8 Ă— 8) = 512. The double asterisk ** means "to the power of."

  7. a % b (Modulus) - Returns the remainder after division. 8 Ă· 3 = 2 remainder 2. So 8 % 3 = 2. This is incredibly useful for checking if numbers are even/odd or for cycling through ranges!

Output đź’»

When you run this code, you'll see:

a + b = 11
a - b = 5
a * b = 24
a / b = 2.6666666666666665
a // b = 2
a ** b = 512
a % b = 2
Enter fullscreen mode Exit fullscreen mode

🎯 Operator Quick Reference

Here's a handy reference for all the arithmetic operators:

  • + Addition - Adds two numbers
  • - Subtraction - Subtracts second number from first
  • * Multiplication - Multiplies two numbers
  • / Division - Divides and gives decimal result
  • // Floor Division - Divides and gives whole number (rounds down)
  • ``** Exponentiation - Raises to a power
  • % Modulus - Gives remainder after division

🤔 Real-World Uses

You might wonder when you'd use these operators. Here are practical examples:

  • Addition/Subtraction: Calculating totals, budgets, scores
  • Multiplication/Division: Converting units, splitting bills, scaling recipes
  • Floor Division: Distributing items evenly, pagination (items per page)
  • Exponentiation: Calculating compound interest, exponential growth
  • Modulus: Checking even/odd numbers, creating cycles, validating data

đź’ˇ Key Takeaways

  • Python supports all basic arithmetic operations
  • Use +, -, *, / for basic math
  • // gives you whole number division (floor division)
  • ** raises numbers to powers (exponentiation)
  • % (modulus) gives you the remainder—super useful for many algorithms!
  • Regular division / always returns a float, even if the result is a whole number
  • The math module (imported at the top) provides even more mathematical functions

What's Next?

Tomorrow on Day 6, I'll be diving into if statements - learning how to make decisions in our code! We'll use comparison operators and control the flow of our programs based on conditions. Time to make Python think! đź§ 

Let's Connect!đź’¬

I'd love to hear from you!

  • Which operator surprised you the most?
  • Have you used modulus before? What for?
  • What calculations do you want to build in Python?

Drop a comment below! If you're coding along, try creating your own calculator with these operators and share your results!

Don't forget to follow me for daily updates. Day 6 is all about making decisions! đź’Ş

*Happy Coding! *

Top comments (0)