Using Calculators, Finding Remainders & Order of Operations
At its core, every computer is an incredibly fast calculator. Python makes mathematical calculations natural and intuitive. You have the standard operations you already know from everyday life: addition (+), subtraction (-), multiplication (*), and division (/). Notice that regular division with a single slash (/) always gives you a decimal number (a float), so 10 / 2 gives 5.0, not 5.
Python also provides two special math tools that are incredibly useful in programming: floor division (//) and the modulo operator (%). Floor division divides two numbers and chops off any decimal remainder, rounding down to the nearest whole integer. For example, 7 // 2 equals 3. The modulo operator (%) gives you only the remainder left over after dividing. For example, 7 % 2 equals 1, because 2 goes into 7 three times with 1 left over.
When would you ever use modulo (%) in real life? All the time! Modulo is the secret tool for checking if a number is even or odd (number % 2 == 0 means even), creating repeating cycles (like days of the week), or converting time. If you have 125 seconds, 125 // 60 tells you how many full minutes you have (2 minutes), and 125 % 60 tells you how many seconds are left over (5 seconds).
When you combine multiple math operations on a single line, Python follows the standard mathematical order of operations: Parentheses first, Exponents second, Multiplication and Division third, and Addition and Subtraction last. If you want a specific calculation to happen first, always wrap it inside parentheses () so your intentions are 100% clear.
Real-World Analogy
Floor division and modulo are like packing 25 donuts into boxes that hold 6 donuts each: floor division (25 // 6) tells you that you can fill 4 complete boxes, while modulo (25 % 6) tells you that you have 1 delicious loose donut left over!
Code ExamplePython 3
# Python handles everyday math naturally:# Addition and Subtraction
subtotal = 50 + 20 - 5print("Subtotal:", subtotal)
# Multiplication and Division# Division always produces a float (decimal number)
average = 100 / 4print("Average:", average)
# Integer division // drops the decimal portion
items_per_box = 17 // 5print("Full boxes:", items_per_box)
# Modulo % gives the remainder left over
leftover_items = 17 % 5print("Leftover loose items:", leftover_items)
# Exponentiation ** raises a number to a power
squared = 3 ** 2print("3 squared:", squared)
Key Rules & Concepts
Standard division (/) always returns a decimal number (float).
Floor division (//) divides and rounds down to the nearest whole number (integer).
The modulo operator (%) calculates the leftover remainder after integer division.
Modulo is perfect for time conversions (seconds into minutes) and checking if numbers are even or odd.
Python follows standard order of operations: use parentheses () to group calculations.
Challenge
Beginner
Convert a raw duration in total seconds into hours, minutes, and remaining seconds using floor division and modulo.
What to do:
Given total_seconds = 7384
Calculate hours using integer floor division: total_seconds // 3600
Calculate remaining seconds using modulo: total_seconds % 3600
Calculate minutes by floor-dividing remaining seconds by 60
Calculate seconds using remaining seconds modulo 60
Print the result formatted as: f"{hours}h {minutes}m {seconds}s"
Reference Solution Locked
0 Attempts
Solve the challenge above and run your code to verify it. If you are stuck, you can unlock the reference solution after running your code at least once.