Unit 1 ยท Lesson 4

Number Operations & f-Strings

Calculate with variables and turn results into clear output.

๐Ÿ›’ Calculate with Variables

A useful program turns stored data into a result.

shopping_total.py
Console Output
37.5

โž• Add, Subtract, Multiply

Python uses familiar operations with programming symbols.

+

Addition

5 + 2 โ†’ 7

โˆ’

Subtraction

5 - 2 โ†’ 3

ร—

Multiplication

5 * 2 โ†’ 10

โž— Three Ways to Divide

The operator changes the question Python answers.

/

Division

17 / 5 โ†’ 3.4

Exact quotient

//

Floor Division

17 // 5 โ†’ 3

Complete groups

%

Modulo

17 % 5 โ†’ 2

Remainder

๐Ÿ” Division Check (1/3)

What is printed?

division_check.py

๐Ÿ” Division Check (2/3)

What is printed?

division_check.py

๐Ÿ” Division Check (3/3)

What is printed?

division_check.py

โšก Powers in Python

Use ** to raise a number to a power.

2ยณ

In Mathematics

The exponent tells us how many factors of 2 to multiply.

2 ** 3

In Python

Two asterisks write the same power operation.

๐Ÿ” Power Check

What value is printed?

power_check.py

๐Ÿ“ From Formula to Python

Translate each mathematical operation into a Python operator.

Circle Area
A=ฯ€r2A = \pi r^{2}
circle_area.py
Console Output
50.24
ฯ€

Named Value

ฯ€ becomes the variable pi.

ร—

Write Multiplication

Implicit multiplication becomes *.

ยฒ

Write the Power

rยฒ becomes radius ** 2.

๐Ÿงฎ Precedence

Python evaluates grouped and higher-priority operations first.

1

Grouping

() are evaluated first.
2

Powers

** is evaluated next.
3

Multiply and Divide

* / // % share the next level.
4

Add and Subtract

+ - are evaluated last.
2 + 3 * 4 ** 2

Press Next to evaluate one rule at a time.

0 / 3

๐Ÿ” Precedence Check (1/3)

What is printed?

precedence_check.py

๐Ÿ” Precedence Check (2/3)

What is printed?

precedence_check.py

๐Ÿ” Precedence Check (3/3)

What is printed?

precedence_check.py

๐Ÿ’พ Store a Calculation

Python evaluates the right side before assigning the result.

trip_cost.py
Console Output
38

๐Ÿ” Text Plus a Number

What happens when this program runs?

mixed_output.py

๐Ÿงฌ Anatomy of an f-String

Three small markers turn values into readable text.

f

Prefix

Place f immediately before the opening quote.

{ }

Braces

Put a variable or short expression inside braces.

text

Surrounding Text

Write labels and punctuation normally inside the string.

f"Total: {total}"

๐Ÿงพ Build Clear Output

The calculation stays numeric; the f-string handles presentation.

receipt.py
Console Output
Item: Notebook Quantity: 3 Total: 37.5

๐Ÿ” f-String Check

What exact sentence is printed?

f_string_check.py

๐Ÿงฎ Expressions Inside Braces

Python evaluates a short expression before inserting its result.

updated_values.py
Console Output
Next level: 5 New score: 23

๐Ÿ“ Homework

Complete it on time and submit it before the deadline.

๐Ÿ”– Summary

Key takeaways from this lesson.

Calculate

Use the operator that matches the question you need to answer.

Divide

/ gives a quotient, // gives complete groups, and % gives the remainder.

Order

Parentheses and higher-priority operators are evaluated first.

Format

An f-string embeds values and short expressions inside readable text.

๐Ÿงช Task 1: Operator Lab

Predict each result before running the code.

Your Goal

1. Predict all six outputs.

2. Run the code and correct any prediction that differs.

3. Label which line demonstrates /, //, %, **, or precedence.

Example Code

โฑ๏ธ Task 2: Convert Minutes

Use complete groups and a remainder to split time.

Your Goal

1. Store 137 in a variable named total_minutes.

2. Calculate complete hours with // and remaining minutes with %.

3. Print both results with one f-string.

Target Output
2 hours and 17 minutes

๐Ÿงพ Task 3: Build a Receipt

Calculate a purchase and report every important value.

Your Goal

1. Calculate the total price in a new variable.

2. Print the item, unit price, quantity, and total on four labeled lines.

3. Use f-strings for every labeled line.

Example Code

๐Ÿ“Š Task 4: Performance Report

Translate a multi-step formula and report its result.

Your Goal

1. Create variables named wins, draws, and losses. In the formula, w, d, and l represent those values.

2. Translate the formula below into Python and store the result in rating.

3. Print the total number of games and the rating with f-strings.

4. Test with wins = 6, draws = 2, and losses = 2.

Points Percentage
R=3w+d3(w+d+l)ร—100R = \dfrac{3w + d}{3(w + d + l)} \times 100

๐Ÿ› ๏ธ Task 5: Repair the Output

Keep calculations numeric and use f-strings for display.

Your Goal

1. Run the program and locate the failing line.

2. Replace both output lines with f-strings.

3. Confirm that the program prints Player: Maya and Total: 30.

4. Add one line that displays the next score after another 5-point bonus.

Example Code
01 / 27