Learning LibraryCore Coding LibraryTeens

Data Types in Python: int, str, bool, and converting between them

Every value in Python has a type, and the type decides what you can do with it. Mixing them up is behind a huge share of beginner bugs — so it pays to see them clearly.

The big idea

Common types are int (whole numbers), float (decimals), str (text), and bool (True/False); type() reveals one, and functions like int() convert between them.

See it in code

1The basics

Every value has a type, and type() reveals it. A whole number like 240 is an int:

python
x = 240
print(type(x))
Run it — Python names the type:
<class 'int'>

<class 'int'> is Python telling you x holds an integer. Ask type() about any value and it answers the same way.

2A step further

Text that looks like a number is still text. "100" is a str; int() converts it into a real int you can do math with:

python
hp_text = "100"
print(type(hp_text))
hp = int(hp_text)
print(type(hp))
Run it — the type changes after conversion:
<class 'str'>
<class 'int'>

int("100") turned a str into an int. That conversion is exactly what you need when data arrives as text — the move the full example leans on.

3In our world

A game entity carries mixed data: x is an int position, label is a str, alive is a bool. type() reports each — then we convert some incoming text and do the math:

python
x = 240
label = "boss"
alive = True

print(type(x))
print(type(label))
print(type(alive))

hp_text = "100"
hp = int(hp_text)
print(hp + 50)
Run it — three types, then a text-to-number conversion:
<class 'int'>
<class 'str'>
<class 'bool'>
150

int("100") turned the text "100" into the number 100, so hp + 50 gave 150. Skip that conversion and "100" + 50 raises a TypeError — you can't add a string and an int.

The same idea, everywhere

Type awareness runs through every domain: prices arrive as strings and need float(), DNA is a str you index, a sensor flag is a bool. The recurring bug is text that looks like a number — "5" > "12" is True because strings compare alphabetically, not numerically.

Try it yourself

Print type(3.14) to meet float, then convert it with int(3.14) and note it truncates to 3. Next, try str(240) to turn a number into text you can join with other strings.

The common mistake

Comparing or combining across types. "100" == 100 is False (text isn't the number), and "3" + 4 errors. When input comes in as text, convert it with int() or float() before you compute.

What it unlocks

Type sense underpins operators (int vs float division), string methods, and safe conversions guarded by try / except.

Want the simpler version? Read the Kids version →