Pebellslearn by building
0%
RoadmapMatrixCatalogSandboxSign in
Fundamentals  ›  Lesson

Variables & Data Types

Variables & Data Types 12 minStoring values and knowing what type they are
You're building a piece ofBill Splitter & Tip Calculator
This piece — format_money(): Turns every number into a clean $ price on screen.
Scenario The soup is stored as 12.5 in the database, but the receipt must read $12.50. This function formats every amount on screen.
Your task
Build format_money(amount). It turns a number into a price string with a $ and exactly two decimals. Example: format_money(12.5) → "$12.50".

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

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:

python

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 name

Descriptive beats short. subtotal will still make sense next month; s won't.


Every value has a type

type(value) tells you which:

python

84 and "84" look similar and behave nothing alike. One is a quantity, the other is two characters:

What does this print?
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:

python

Two conveniences worth knowing: underscores make long numbers readable, and abs() drops the sign.

python

Strings come with a toolkit

A string is text, and it arrives loaded with methods — functions you call on it with a dot:

python

You can measure a string and reach into it by position:

python

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:

python

None means "no value". It's what a function hands back when it has no return, which makes it worth recognising early:

python

Converting between types

The type names double as converter functions:

python

One of these does something people don't expect:

What does this print?
print(int(3.9))

This matters because anything a user types arrives as text, and text can't do maths:

broken — fix itTypeError

This 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:

python
python

Step through the formatter to see the value stay a number while the text built from it changes:

step through it
1amount = 12.5
2plain = f"${amount}"
3padded = f"${amount:.2f}"
4print(plain, padded, amount)

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. ✅

format_money.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
format_money(amount) → strFormat a number as a $ price with 2 decimals.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.