computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

🔢

🎓

⏱ 25 min

Free

Data Types & Conversions

Strings, integers, floats and booleans — learn what kind of data you are working with.

🎯 What you will learn

  • Recognize str, int, float and bool
  • Check types with type()
  • Convert between types

👋 Let’s get started!

Every value in Python has a TYPE. “42” in quotes is text, 42 without quotes is a number — and Python treats them very differently. Knowing types prevents tons of bugs! 🐛

🪜 Step by step

1. 🔤 The big four

str is text, int is a whole number, float has a decimal point, bool is True or False.

name = "Kai"      # str
level = 7         # int
speed = 3.5       # float
is_ready = True   # bool
2. 🕵️ type()

Ask Python what type something is.

print(type(7))
print(type("7"))
print(type(7.0))
3. 🔄 Converting

Use int(), float(), str() and bool() to change types.

age = int("12")
print(age + 1)
print("Level " + str(5))

🧪 Try it!

Change the code and press ▶ Run to see what happens. You can’t break anything!

Type detective

values = [42, "42", 4.2, True]
for v in values:
    print(v, "is a", type(v).__name__)

Fix the type mix-up

coins = input("Coins? ")
coins = int(coins)
coins = coins * 2
print("Doubled:", coins)

📖 Key words

Tap a card to flip it over!

str
Text, inside quotes 🔤
int
Whole number 🔢
float
Number with a decimal point 🌊
bool
True or False ✅❎

🏆 Challenge

Fix the bug so the program prints: Next year I will be 13

Challenge

age = "12"
print("Next year I will be " + (age + 1))

Knowledge Check Quiz

  1. What type is 3.14?
  2. What prints?
  3. What prints?
← Previous lesson
Next lesson →