Module 1 · Lesson 5 of 5

Input & Basic Math

Time to make your programs interactive and computational. You'll learn to accept user input and harness all of Python's arithmetic operators — including a few that might surprise you.

Use all 7 arithmetic operators
Understand operator precedence
Use // (floor division) and % (modulo)
Solve real-world math problems in Python

Python's Arithmetic Operators

You already know +, -, *, and /. But Python has three more arithmetic operators that are incredibly useful:

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333…
//Floor Division10 // 33
%Modulo (remainder)10 % 31
**Exponent (power)10 ** 31000
💡 The Power Trio: / // %

/ gives you the full decimal answer. // gives you just the whole number part (rounds down). % gives you just the remainder. Together, they're incredibly useful for problems like converting units.

operators.py
# Floor division drops the decimal
print(17 // 5)     # → 3   (how many whole 5s fit in 17?)

# Modulo gives the remainder
print(17 % 5)      # → 2   (what's left over?)

# Real-world example: 125 minutes → hours and minutes
total_minutes = 125
hours = total_minutes // 60     # → 2
minutes = total_minutes % 60    # → 5
print(f"{hours}h {minutes}m")  # → 2h 5m

# Exponents
print(2 ** 10)     # → 1024
print(9 ** 0.5)    # → 3.0  (square root!)

Operator Precedence

Just like in math class, Python follows an order of operations. From highest to lowest priority:

precedence.py
# ** first, then * and /, then + and -
print(2 + 3 * 4)      # → 14  (not 20!)
print((2 + 3) * 4)    # → 20  (parentheses override)
print(2 ** 3 ** 2)    # → 512 (** is right-to-left: 3**2=9, then 2**9)

# When in doubt, use parentheses! They make your intent clear.
total = (100 + 50) * 0.08   # Tax on the sum
⚠️ Division always returns a float

10 / 2 returns 5.0, not 5. The / operator always gives a float, even when the result is a whole number. Use // if you need an integer.

🔢

Challenge 1: Calculator

Easy

Given two numbers, perform all seven arithmetic operations and store each result in the correct variable.

Instructions: Using a = 17 and b = 5, calculate and store the result of every operation.
Replace each 0 with the actual operation: addition = a + b, subtraction = a - b, multiplication = a * b, and so on. Use // for floor division, % for remainder, ** for power.
💰

Challenge 2: Tip Calculator

Easy

Build a restaurant tip calculator. Given a meal price and a tip percentage, calculate the tip amount and the total bill.

Instructions:
• Calculate tip_amount by multiplying the meal price by the tip percentage (converted from a whole number to a decimal)
• Calculate total_bill by adding the meal price and tip
• Both should be floats
To convert a percentage to a decimal, divide by 100: tip_amount = meal_price * (tip_percent / 100). Then total_bill = meal_price + tip_amount. The tip should be 9.0 and the total 54.0.
⏱️

Challenge 3: Time Converter

Medium

Convert a large number of seconds into hours, minutes, and remaining seconds. This is the perfect job for // and %.

Instructions: Given total_seconds = 7384, calculate:
hours — whole hours (should be 2)
minutes — remaining whole minutes after hours are removed (should be 3)
seconds — remaining seconds after hours and minutes (should be 4)

The output should be: 2h 3m 4s
Work from largest to smallest. hours = total_seconds // 3600 (3600 seconds in an hour). Then find what's left: remaining = total_seconds % 3600. From the remaining, minutes = remaining // 60 and seconds = remaining % 60.
🌡️

Challenge 4: Temperature Converter

Stretch

Build a two-way temperature converter using the formulas:
°C = (°F - 32) × 5/9 and °F = °C × 9/5 + 32

Instructions:
• Convert temp_f (98.6°F) to Celsius and store in to_celsius
• Convert temp_c (100°C) to Fahrenheit and store in to_fahrenheit
• Use round(value, 1) to round each result to 1 decimal place
• Print both results using f-strings
For F→C: to_celsius = round((temp_f - 32) * 5/9, 1). Parentheses are important — subtract 32 first! For C→F: to_fahrenheit = round(temp_c * 9/5 + 32, 1). Expected: 37.0°C and 212.0°F.