Foundations

Python Trainer

Variables · Math · f-strings · Modulo · Decimal formatting

Weight Converter
Use // (integer division) and % (modulo) to break total ounces into tons, pounds, and ounces.
💡
Modulo % returns the remainder after division. So 3200 % 32000 gives you what's left over after removing full tons. Think of it like making change.
Inputs — adjust and watch the code update
3,200 oz
Python code
weight.py

          
Output
live result
// integer div % modulo f"..." f-string
Commute Distance — Per Worker
Each worker lives a different distance from the office. Adjust the number of workers and everyone gets a new random distance. Shows how Python would store each value in a variable and sum them up.
💡
Each worker = their own variable. In real Python you'd use a list for this — but in Foundations we write each one out explicitly so you can see the pattern. Notice how the total uses every individual variable added together.
Inputs
4
5
distances: 2 – 45 mi one-way
Worker roster — random distances
Python code
commute.py

          
Output
live result
* multiply variables f"..." f-string :.1f decimal
Time Breakdown
A video render took N seconds. Break it into hours, minutes, and remaining seconds — like a digital clock.
💡
Chaining // and % — first divide by 3600 to get hours, then take the remainder % 3600 and divide that by 60 to get minutes, then % 60 for leftover seconds.
Inputs
9,754 sec
Python code
time_break.py

          
Output
live result
// chained % chained
Bill Splitter
Split a lunch bill evenly. Use % to find the leftover cents that can't be split perfectly. Introduces .2f formatting.
💡
f"{'{'}value:.2f{'}'}" — the :.2f inside an f-string means "format as a float with exactly 2 decimal places." Essential for money. Without it, you might get 4.166666666.
Inputs
$23.47
4
Python code
bill_split.py

          
Output
live result
% remainder :.2f decimal f"..." f-string
Temperature Converter
Convert between Fahrenheit and Celsius. Shows how f-strings display results to a precise number of decimal places — critical for scientific data.
💡
:.Nf controls precision. :.0f = no decimals, :.1f = one decimal, :.4f = four decimals. Great for showing the difference between a rough estimate vs. a precise measurement.
Inputs
98°F
Python code
temp_convert.py

          
Output — same number, different precision
live result
:.0f no decimals :.2f two decimals :.4f four decimals
Fuel Cost Calculator
How much does a road trip cost in gas? Combine division, multiplication, and f-string decimal formatting for a real-world output.
Inputs
450 mi
28 mpg
$3.49
Python code
fuel_cost.py

          
Output
live result
/ float division * multiply :.2f money
f-string Formatting Reference
A hands-on guide to all the most useful f-string format specs. Adjust the number and see how each format code changes the output.
Base number to format
1234567.891
3.14159
fstring_formats.py

      
Live output
every format, live
📌
Quick reference:
:.2f — 2 decimal places  |  :,.2f — 2 decimals + comma separator  |  :d — integer  |  :e — scientific notation  |  :.0% — percentage  |  :10.2f — right-aligned in 10 chars  |  :+.2f — always show sign