LearnPy Hub
BasicsBeginnerLesson 3 of 32

Data Types & Casting

Watch the ideaEvery value has a type
type(42), type(3.14), type("42"), type(True)
Types
int42
float3.14
str'42'
boolTrue

Python has different kinds of values: whole numbers, decimals, text and True/False.

1Level 1 · BasicsBasics

Every value in Python has a type. The type decides what you can do with the value: you can divide numbers, but not words. Knowing the type of your data prevents a lot of confusing errors.

The four basic types

TypeWhat it holdsExamples
intwhole numbers7, -3, 2026
floatnumbers with a decimal point3.14, -0.5, 2.0
strtext (a string of characters)"hello", '42'
boolyes/no valuesTrue, False

There's also None, which means "no value yet". Use type() to ask Python what type something is.

The same symbol can mean different things

+ adds numbers but joins strings. "5" + "5" is "55", while 5 + 5 is 10. Quotes change everything!

Casting: converting between types

To change a value's type, call the type like a function:

int("42")      # 42      text to whole number
float("2.5")   # 2.5     text to decimal
str(99)        # "99"    number to text
int(7.9)       # 7       drops the decimal part (no rounding)
bool(0)        # False   0, "" and empty things are False

Casting matters most when numbers arrive as text. "20" + "5" gives "205", but int("20") + int("5") gives 25. You will use this all the time in the Input lesson, because whatever the user types arrives as a string.

When conversion fails

int("hello") and int("3.7") both raise a ValueError because that text isn't a whole number. Convert "3.7" with float() instead.

Key points: int, float, str and bool are the basic types; type() tells you the type; int(), float(), str() convert; numbers written in quotes are text until you convert them.

See it in actionConverting between types

Notice the quotes: "20" is text until int() turns it into a number.

Step-by-step

Try it yourselfExample 1: Inspect types

type() shows the type of any value. Notice that "10" in quotes is a str, not an int.

example_1.py
Output appears here
Fill in the blanks

What type is it?

Fill the blank so the program prints the type of 3.5: <class 'float'>.

blanks.py
x = 3.5
print((x))

Example 2: Text plus text, number plus number

The same + does two different jobs depending on the types.

example_2.py
Output appears here
Fix the bug

Text plus a number

This should print I am 12 years old, but it crashes. Fix it.

buggy.py

Example 3: int() cuts, round() rounds

int() simply drops the decimal part. Use round() when you want the nearest whole number.

example_3.py
Output appears here
Fill in the blanks

Cut or round?

Fill the blanks so the output is 7 8: first cut off the decimals of 7.6, then round it.

blanks.py
n = 7.6
print((n), (n))
Your turn

Price from text

The price comes in as text: price = "250". Turn it into a number and print the price for 3 items: Total: 750.

my_code.py

Example 4: Truthy and falsy values

bool() turns any value into True or False. Zero and empty values are False; everything else is True.

example_4.py
Output appears here
Your turn

Empty or not?

Print the result of bool() for these three values, one per line, in this order: "", "hi", 0. The output should be False, True, False.

my_code.py

2Level 2 · IntermediateNumbers in depth

Python has more than one kind of number, and each behaves a little differently.

int has no size limit

12345678901234567890 * 98765432109876543210 just works. Python integers grow as large as memory allows. You can write long numbers with underscores: 1_000_000.

float is approximate

Decimal fractions are stored in binary, so 0.1 + 0.2 gives 0.30000000000000004. Tidy a float result with round(x, 2) before you show it.

Other bases and None

bin(10), hex(255) and int("ff", 16) convert between bases. None is the type for "no value yet".

See it in actionConverting step by step

Watch the type of each variable as text becomes numbers and back.

Step-by-step

Example 5: Big integers and float surprises

The huge product is exact, but 0.1 + 0.2 is a tiny bit off. round() tidies it.

example_5.py
Output appears here

Example 6: Binary, hex and back

int() takes a second argument, the base of the text.

example_6.py
Output appears here

3Level 3 · AdvancedExact numbers and conversion rules

For money, approximate floats are not good enough. There is a simple fix that uses only whole numbers.

Count money in the smallest unit

Store 19.99 Tk as 1999 paisa. Whole numbers are always exact, so totals never drift. Divide by 100 only when you show the result. (Later, in the Modules lesson, you will meet Decimal, which does exact decimal maths for you.)

Numeric tower

When types mix, Python widens: bool → int → float → complex. True + True is 2, and 3 + 2j is a complex number.

Rounding rules

round(2.5) gives 2 and round(3.5) gives 4: Python rounds halves to the nearest even number (banker's rounding). When you need the school rule "round half up" for a positive number, use int(x + 0.5).

See it in actionBooleans are numbers too

Watch how True counts as 1 when Python adds things up.

Step-by-step

Example 7: Money that adds up exactly

Compare the float total with the paisa total. Then see the two rounding rules side by side.

example_7.py
Output appears here

Example 8: Mixing types

Each result's type is the "widest" type in the expression.

example_8.py
Output appears here
Rememberint("3.7") fails. Convert with float() first, then int() if you need a whole number: int(float("3.7")) gives 3.
Exercise · predict the output

What does this print?

print("5" * 3)
Quick quiz · 5 questions
1. What type is "42" (with the quotes)?
2. What does int(7.9) give?
3. What is the type of 3.0?
4. Which value is False when converted with bool()?
5. What does "3" + "4" evaluate to?
Ready to practice data types & casting?

50 graded problems are waiting in Topic Practice.

Practice this topic →
Python Data Types & Casting · LearnPy Hub