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.
// (floor division) and % (modulo)Python's Arithmetic Operators
You already know +, -, *, and /. But Python has three more arithmetic operators that are incredibly useful:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333… |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulo (remainder) | 10 % 3 | 1 |
| ** | Exponent (power) | 10 ** 3 | 1000 |
/ 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.
# 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:
# ** 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
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
Given two numbers, perform all seven arithmetic operations and store each result in the correct variable.
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
Build a restaurant tip calculator. Given a meal price and a tip percentage, calculate the tip amount and the total bill.
• 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
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
Convert a large number of seconds into hours, minutes, and remaining seconds. This is the perfect job for // and %.
• 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
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
Build a two-way temperature converter using the formulas:
°C = (°F - 32) × 5/9 and °F = °C × 9/5 + 32
• 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
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.