Foundations
Variables
Naming Values
You're tracking your monthly budget. Instead of typing 2500.00 every time you
calculate savings, rent, and groceries, you store it in a variable called income.
Now when your income changes, you update one number and all calculations adjust.
Store and use a value
Give a value a name, then use that name in calculations.
x = 10
print(x + 5)
print(x * x)
x = 5
print(x + 5)
print(x * x)
x = 20
print(x + 5)
print(x * x)
x ← 10
1x→ 10 = 10 #@x=5, 202print(x10 + 5)3print(x10 * x)output15 100
x ← 5
1x→ 5 = 52print(x5 + 5)3print(x5 * x)output10 25
x ← 20
1x→ 20 = 202print(x20 + 5)3print(x20 * x)output25 400
A variable holds a value. Use it anywhere you'd use the value itself.
Building expressions step by step
Break a complex calculation into named pieces. Each piece has meaning.
width = 7
height = 5
area = width * height
perimeter = 2 * (width + height)
width = 3
height = 5
area = width * height
perimeter = 2 * (width + height)
width = 10
height = 5
area = width * height
perimeter = 2 * (width + height)
width = 7
height = 4
area = width * height
perimeter = 2 * (width + height)
width = 7
height = 8
area = width * height
perimeter = 2 * (width + height)
width ← 7, height ← 5, area ← 35, perimeter ← 24
1width→ 7 = 7 #@width=3, 102height→ 5 = 5 #@height=4, 83area→ 35 = width7 * height54perimeter→ 24 = 2 * (width7 + height5)
width ← 3, height ← 5, area ← 15, perimeter ← 16
1width→ 3 = 32height→ 5 = 53area→ 15 = width3 * height54perimeter→ 16 = 2 * (width3 + height5)
width ← 10, height ← 5, area ← 50, perimeter ← 30
1width→ 10 = 102height→ 5 = 53area→ 50 = width10 * height54perimeter→ 30 = 2 * (width10 + height5)
width ← 7, height ← 4, area ← 28, perimeter ← 22
1width→ 7 = 72height→ 4 = 43area→ 28 = width7 * height44perimeter→ 22 = 2 * (width7 + height4)
width ← 7, height ← 8, area ← 56, perimeter ← 30
1width→ 7 = 72height→ 8 = 83area→ 56 = width7 * height84perimeter→ 30 = 2 * (width7 + height8)
Reading area tells you more than reading 7 * 5. Variables make code self-documenting.
Temperature conversion (Fahrenheit to Celsius)
Convert 98.6°F (body temperature) to Celsius using the formula: C = (F - 32) × 5 / 9
fahrenheit = 98.6
celsius = (fahrenheit - 32) * 5 / 9
fahrenheit = 32
celsius = (fahrenheit - 32) * 5 / 9
fahrenheit = 212
celsius = (fahrenheit - 32) * 5 / 9
fahrenheit = 0
celsius = (fahrenheit - 32) * 5 / 9
fahrenheit ← 98.6, celsius ← 37.0
1fahrenheit→ 98.6 = 98.6 #@fahrenheit=32, 212, 02celsius→ 37.0 = (fahrenheit98.6 - 32) * 5 / 9
fahrenheit ← 32, celsius ← 0.0
1fahrenheit→ 32 = 322celsius→ 0.0 = (fahrenheit32 - 32) * 5 / 9
fahrenheit ← 212, celsius ← 100.0
1fahrenheit→ 212 = 2122celsius→ 100.0 = (fahrenheit212 - 32) * 5 / 9
fahrenheit ← 0, celsius ← -17.77777777777778
1fahrenheit→ 0 = 02celsius→ -17.77777777777778 = (fahrenheit0 - 32) * 5 / 9
Variables make the formula readable: fahrenheit, celsius - you know what they mean.
Exercise: temperature_ex01.py
Write the reverse conversion: Celsius to Fahrenheit using F = C × 9/5 + 32
Fibonacci sequence (without loops)
Each Fibonacci number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8...
c0 = 0
c1 = 1
c2 = c0 + c1
c3 = c1 + c2
c4 = c2 + c3
c5 = c3 + c4
c6 = c4 + c5
c7 = c5 + c6
c0 ← 0, c1 ← 1, c2 ← 1, c3 ← 2, c4 ← 3, c5 ← 5, c6 ← 8, c7 ← 13
1c0→ 0 = 02c1→ 1 = 13c2→ 1 = c00 + c114c3→ 2 = c11 + c215c4→ 3 = c21 + c326c5→ 5 = c32 + c437c6→ 8 = c43 + c558c7→ 13 = c55 + c68
Watch how each variable depends on the previous ones. This pattern is called building up from base cases.
Circle calculations
Given a radius, calculate circumference and area.
pi = 3.14159
radius = 5
circumference = 2 * pi * radius
area = pi * radius * radius
pi = 3.14159
radius = 1
circumference = 2 * pi * radius
area = pi * radius * radius
pi = 3.14159
radius = 10
circumference = 2 * pi * radius
area = pi * radius * radius
pi = 3.14159
radius = 2.5
circumference = 2 * pi * radius
area = pi * radius * radius
pi ← 3.14159, radius ← 5, circumference ← 31.4159, area ← 78.53975
1pi→ 3.14159 = 3.141592radius→ 5 = 5 #@radius=1, 10, 2.534circumference→ 31.4159 = 2 * pi3.14159 * radius55area→ 78.53975 = pi3.14159 * radius5 * radius
pi ← 3.14159, radius ← 1, circumference ← 6.28318, area ← 3.14159
1pi→ 3.14159 = 3.141592radius→ 1 = 134circumference→ 6.28318 = 2 * pi3.14159 * radius15area→ 3.14159 = pi3.14159 * radius1 * radius
pi ← 3.14159, radius ← 10, circumference ← 62.8318, area ← 314.159
1pi→ 3.14159 = 3.141592radius→ 10 = 1034circumference→ 62.8318 = 2 * pi3.14159 * radius105area→ 314.159 = pi3.14159 * radius10 * radius
pi ← 3.14159, radius ← 2.5, circumference ← 15.70795, area ← 19.6349375
1pi→ 3.14159 = 3.141592radius→ 2.5 = 2.534circumference→ 15.70795 = 2 * pi3.14159 * radius2.55area→ 19.6349375 = pi3.14159 * radius2.5 * radius
pi and radius as named values make the formulas clear.
Exercise: circle_ex01.py
Extend to sphere: calculate volume (4/3 × π × r³) and surface area (4 × π × r²)