All 24 modules are open from the start — nothing here is
locked, and nothing costs anything. Sign in so your progress, titles and
credentials stay with you, on every device you use.
Free forever, with your Google account. No password, no payment.
Python cares a great deal about what type a value is. "7" and
7 look identical on screen and behave completely differently the
moment you do anything with them. This module covers the four types you will
meet constantly, how to check which one you have, and how to convert on
purpose rather than by accident.
Ready?
1
Two Kinds of Number
An int is a whole number:
12, 0, -400. A
float is a number with a decimal point:
4.5, 0.0, -12.75. The decimal point
is the entire difference in how you write them.
type(value) tells you which you have. Python ints have no
maximum size — you can multiply them until you run out of memory — which
is unusual among programming languages and occasionally very convenient.
Try 0.1 + 0.2 and Python answers
0.30000000000000004. That is not a bug, and it is not
Python's fault: a computer stores decimals in binary, and 0.1 has no
exact binary form, the same way 1/3 has no exact decimal form. The
practical consequences are two. Never compare floats with
== when the values came from arithmetic. And for money,
either work in whole pence as an int, or format the output
to two decimal places and accept the tiny error underneath.
Quick check
What type is the result of 10 / 5?
2
Text That Looks Like a Number Is Still Text
A str is text, written in matching quotes.
"42" is a str whose two characters happen to be
digits. Python will not quietly treat it as the number 42, and that
refusal saves you from a whole family of bugs that other languages let
through.
The clearest demonstration is +, which means two different
things depending on what it is given:
"7" + "7" # "77" — text joined end to end
7 + 7 # 14 — numbers added
"7" + 7 # TypeError: can only concatenate str (not "int") to str
That TypeError is Python telling you the two values disagree
about what kind of thing they are. The fix is never to force it — it is to
convert one of them.
Anything typed by a person
Arrives as a str. Every time, with no exceptions.
Anything read from a file
Same. Files hold characters, not numbers.
Anything from a form or the web
Usually the same. Convert at the edge of your program.
Quick check
What does "3" * 2 produce?
3
Converting on Purpose
Each type has a function named after it that converts a value into it.
This is usually called casting.
That difference between int() and round() is
worth stopping on. int()truncates: it
removes the decimal part and keeps what is left, so 5.9 becomes 5.
round() goes to the nearest whole number, so 5.9 becomes 6.
Choosing the wrong one is how a calculation quietly loses a penny on every
transaction.
Convert at the edge
Do the conversion at the point the value enters your program, not
fifteen lines later where you happen to need a number. Then everything
downstream is working with real types, and the one place that can fail
is the one place you were expecting it to.
int("seven") does not return anything sensible — it raises
a ValueError. That is the right behaviour: guessing would
be worse.
Quick check
A price of 19.99 goes through int(price). What comes out?
4
True, False, and What Counts as Nothing
A bool holds one of exactly two values:
True or False. Capital letter, no quotes —
"True" in quotes is a five-letter word and behaves like one.
You rarely type True yourself. Bools mostly arrive as the
answer to a comparison, and you store one when the question it answers has
a good name:
is_adult = age >= 18
has_items = len(cart) > 0
Python will also give a True/False reading of any value, which it
calls truthiness. The rule is short: things that are empty
or zero are False, and everything else is True.
Falsy
Empty or zero
0, 0.0, "", [], {}, None
Truthy
Everything else
Any other number, any non-empty text — including "0" and "False", which are non-empty strings.
Why this matters next
Every if statement in Part 2 asks exactly this question of
whatever you give it. Knowing that an empty string is False and the
string "0" is True saves you from a bug that is genuinely
hard to spot by staring at the code.
Empty and zero are False. Everything else is True.
Quick check
What is bool("False")?
0 of 9 completed
Real Python runs right here in your browser — nothing to install, nothing
sent to a server. The interpreter downloads once the first time you press
Run, then stays cached.
01
To do
Almost everything you handle early on is one of four types.
int is a whole number. float is a number with a
decimal point. str is text in quotes. bool is
True or False — capital letter, no quotes.
type(value) tells you which one you have, and
type(value).__name__ gives you the bare name as text.
Your task: create four variables —
count (the whole number 12), price (the decimal
number 4.5), label (the text Tea), and
in_stock (True). Then print the type name of each, one per
line, giving int, float, str,
bool.
your_code.py
PythonCtrl↵ to run
Hint
12 has no decimal point so it is an int; 4.5 has one so it is a float. True is written with a capital T and no quotes.
Output
02
To do
"42" is text. It cannot be added to, doubled, or compared as
a quantity — as far as Python is concerned it is a pair of characters
that happen to be digits.
int("42") converts it into the number 42, and
float("4.5") does the same for decimals. Going the other way,
str(42) turns a number into text.
"7" + "7" → "77" (text joined end to end) 7 + 7 → 14 (numbers added)
Your task:raw_age holds the text
"29". Convert it to a whole number in a variable called
age, then print age plus one. Output:
30.
your_code.py
PythonCtrl↵ to run
Hint
age = int(raw_age) converts it. Then print(age + 1).
Output
03
To do
A dashboard went out this morning saying the product got
1015 signups yesterday. It got 25. Nobody typed anything
wrong — the two counts arrived from the reporting API as
text, and + on text does not add, it joins.
"10" + "15" → "1015" 10 + 15 → 25
This is the single most common type bug there is, and it never announces
itself: no error, no warning, just a number that is quietly wrong.
Your task: fix it. Convert both counts to whole numbers
before adding them, so the output reads:
Signups: 25
Leave web_signups and app_signups as they are — the fix belongs on the line that adds them.
your_code.py
PythonCtrl↵ to run
Hint
int() around each one: total = int(web_signups) + int(app_signups).
Output
04
To do
int("12") works. int("12.5") does not — it
raises a ValueError, because int() parses whole
numbers and a decimal point is not one.
A quantity field on a web form will happily accept 12.5, so
this crashes in production and never in testing.
The fix is two steps: read it as a decimal first, then chop it to a whole
number. int(float("12.5")) gives 12.
Your task: run the code, read the error, then fix it so
it prints 12. Leave raw as it is.
your_code.py
PythonCtrl↵ to run
Hint
float(raw) reads the decimal, and int() around that chops it to a whole number. Remember int() cuts the decimal off rather than rounding, so 12.5 becomes 12.
Output
05
To do
Plain division with / always produces a
float, even when the answer is exact. 10 / 2 is
5.0, not 5.
Sometimes you want the whole number back. int(5.9) gives
5 — note that it chops the decimal off
rather than rounding, so 5.9 becomes 5 and not 6. When you do want
rounding, that is round()'s job.
Your task: divide total by
people into share, then make
whole_share the same value with the decimal chopped off, and
rounded_share the same value rounded. Print all three, one
per line.
your_code.py
PythonCtrl↵ to run
Hint
share = total / people gives 11.75. int(share) chops to 11; round(share) rounds to 12.
Output
06
To do
A float is stored in binary, and most decimal fractions have
no exact binary form — the same way a third has no exact decimal form.
So the value kept is very slightly off, and the error shows up when you
print it.
0.1 + 0.2 → 0.30000000000000004
This is not a Python quirk. Nearly every language does it, because nearly
every language uses the same hardware floats. It matters the moment money
is involved: a total of 3.3000000000000003 on an invoice is
a support ticket.
The everyday fix is round(value, 2) — round at the point you
display it.
Your task: print these three lines:
0.30000000000000004 3.3000000000000003 3.3
The first is 0.1 + 0.2. The second is three items at
1.1 each, stored in raw_total. The third is the
same total rounded to two decimal places, stored in total.
your_code.py
PythonCtrl↵ to run
Hint
raw_total = 1.1 * 3, and total = round(raw_total, 2). The 2 is the number of decimal places you want to keep.
Output
07
To do
A bool holds one of exactly two values: True or
False. They are written with a capital letter and without
quotes — "True" in quotes is just a five-letter word.
bool(value) asks "does this count as something?". Python's
answer is False for the empty and zero cases —
0, 0.0, "", None — and
True for everything else. This becomes very useful in the
next part, where if statements ask exactly that question.
Your task: print the result of bool() on
each of these four values, one per line, in this order: 0,
7, the empty text "", and the text
"no".
your_code.py
PythonCtrl↵ to run
Hint
Zero and empty text are False; any other number and any non-empty text are True — including the word "no", because Python is checking for emptiness, not meaning.
Output
08
To do
None is Python's word for "no value at all". It is not
0, and it is not the empty text "".
The distinction is real work, not trivia. An API sending
null for a field it was never given is saying something
different from one sending "" for a field the user left
blank — the first is "we do not know", the second is "they told us
nothing". Treat them the same and you cannot tell an unanswered question
from an empty answer.
Confusingly, bool() says False to all three. So
checking "is it falsy" cannot tell them apart, and
is None is the check that can.
Your task: print exactly these four lines:
missing is None: True blank is None: False zero is None: False all three are falsy: False False False
your_code.py
PythonCtrl↵ to run
Hint
print("missing is None:", missing is None) — the expression after the comma produces True or False on its own. The last line takes three values: bool(missing), bool(blank), bool(zero).
Output
09
To do
This is what a record off a web form or a CSV actually looks like: four
fields, all of them text, including the two that are not.
Your task: convert each one to the type it should be,
keeping the same names:
1
user_id
a whole number
2
score
a decimal number
3
is_active
a real bool — the text says "False", so the answer is False
4
name
already text; leave it
Then print two lines:
int float bool str Active: False
The trap
bool("False") is True. bool() on
text asks only "is there anything in it", and there are five characters
in it. Converting text to a boolean means comparing it to something.
your_code.py
PythonCtrl↵ to run
Hint
user_id = int(user_id) and score = float(score). For the boolean, is_active = is_active == "True" — the comparison produces True or False by itself. The type line is print(type(user_id).__name__, type(score).__name__, ...).
Output
Clean up an imported order
To do
An order has come in from a supplier's export file. Every field arrived as
text, one of them is a percentage, and one of them is a boolean pretending
to be a word. Convert the lot, work out what is owed, and print the
summary.
The four raw values are in the starter code. From them create:
quantity — a whole number, from raw_quantity
unit_price — a decimal number, from raw_price
discount_pct — a whole number, from raw_discount
express — a bool, True only when raw_express is the text "yes"
subtotal — quantity times unit_price
total — the subtotal less the discount, rounded to two decimal places
int() and float() handle the first three. For express, raw_express == "yes" produces True or False on its own. The discount is subtotal * discount_pct / 100, and total = round(subtotal - discount, 2).