So far, you’ve learned how to use variables, how to name them properly, and how to leave clear comments. Now let’s add another handy tool to your Python toolbox — the type() function.
In Python, every piece of data has a type. It could be a number, a string of text, or something more complex like a list. The type() function tells you what kind of data you’re working with — and that’s a big help when you’re writing or debugging your code.
If you ever wonder, “What exactly is inside this variable?” — type() has your answer.
x = 10
print(type(x)) # Output: <class 'int'>
y = "Hello"
print(type(y)) # Output: <class 'str'>
Here, Python tells us that x is an int (short for integer), and y is a str (string — which means text).
Common Data Types in Python
Here are some of the basic data types you’ll see a lot while coding:
int— whole numbers like5,-3, or2025float— numbers with decimals like3.14or-2.5str— strings of text, like"Hello"or"42"bool— short for boolean, which meansTrueorFalse
a = 3.14
print(type(a)) # <class 'float'>
b = True
print(type(b)) # <class 'bool'>
Knowing these types will help you write smarter code that works the way you expect. And when something breaks? type() is often your first clue about what went wrong.
Interactive Exercise: Check Variable Types
Try running the code below. Then change the values and see how the type() output changes. Test out numbers, text, booleans — whatever you want.
# Try changing the values and check their types
x = 42
y = "Python is fun!"
z = 3.5
print(type(x))
print(type(y))
print(type(z))
Interactive Exercise: Guess the Type
Before running this one, take a guess — what type will each variable be? Then use type() to find out if you were right.
# Guess the type before running!
a = 100
b = "100"
c = 100.0
d = False
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Using type() is a great habit. It gives you more control over your code, helps you catch bugs early, and makes debugging way easier. Plus, once you understand data types, you’ll start thinking like a developer — and that’s exactly what you’re becoming.