Variables & Data Types
This lesson builds the app's price formatter — the piece that turns a raw
number like 12.5 into $12.50 everywhere on screen. Getting there means
understanding values, their types, and what each type lets you do.
A variable is a name pointing at a value
Assign with =: name on the left, value on the right.
Python has no separate "declare it first" step — assigning is declaring. And you can re-point a name at any time, even at a completely different type:
That flexibility is called dynamic typing. The value has a type; the name doesn't.
What you may call things
Names must start with a letter or underscore, and can contain letters, digits and underscores — nothing else. These are all rejected:
2age = 30 # can't start with a digit
first-name = "" # a hyphen is a minus sign
@name = "Ada" # @ isn't allowed in a nameDescriptive beats short. subtotal will still make sense next month; s won't.
Every value has a type
type(value) tells you which:
84 and "84" look similar and behave nothing alike. One is a quantity, the
other is two characters:
print("84" + "1")That's the same + doing two different jobs depending on what you hand it —
which is exactly why Python refuses to mix the two.
Numbers: int and float
Integers are whole, floats have a decimal point. Mixing them produces a float,
and / always produces a float even when it divides evenly:
Two conveniences worth knowing: underscores make long numbers readable, and
abs() drops the sign.
Strings come with a toolkit
A string is text, and it arrives loaded with methods — functions you call on it with a dot:
You can measure a string and reach into it by position:
We go much deeper on strings in lesson 4 — this is just enough to work with.
Booleans and None
bool is a yes/no value. Comparisons produce them:
None means "no value". It's what a function hands back when it has no return,
which makes it worth recognising early:
Converting between types
The type names double as converter functions:
One of these does something people don't expect:
print(int(3.9))This matters because anything a user types arrives as text, and text can't do maths:
TypeErrorThis should print 25. Run it, read the error, then fix the conversion.
Type hints describe intent
You'll see functions annotated with the types they expect. These type hints don't change how anything runs — they're documentation for humans and tools:
def format_money(amount: float) -> str:
return f"${amount:.2f}"Read it as: "amount should be a float, and this gives back a str."
Formatting money
Money needs exactly two decimals — 12.5 should display as $12.50. A plain
f-string won't do that:
Step through the formatter to see the value stay a number while the text built from it changes:
Notice amount is still 12.5 at the end. Formatting produced new text; it never
touched the number.
🎯 Your turn
Write format_money(amount) — it turns a number into a price string with a $
and exactly two decimals:
format_money(12.5)→"$12.50"format_money(84)→"$84.00"
Hint — use the format spec f"${amount:.2f}" — it pads and rounds for you.
Then press ▶ Run, tap the Live App try chips to call it with different inputs, and hit ✓ Check. Green = this piece of the app is built. ✅
