📚
Type Inspection
Finding Out a Variable's Type
Every value in Python has a type — it determines what you can do with that value. You can't multiply text! Python needs to know if something is a number, text, or something else.
The
type()x = 42 y = 3.14 z = "hello" print(type(x)) # → <class 'int'> print(type(y)) # → <class 'float'> print(type(z)) # → <class 'str'>
Here's what each type means:
| Type | What it is | Examples |
|---|---|---|
| Whole numbers | |
| Decimal numbers | |
| Text (strings) | |
| True or False | |
Why Does Type Matter?
Because types determine what operations are allowed:
print(5 + 3) # → 8 (number + number = math) print("Hello" + "!") # → Hello! (text + text = join) print("Hello" + 5) # ❌ ERROR! Can't add text and number
💡💡 Debugging tip: If your code crashes unexpectedly, check the types of your variables with
. The problem is often a type mismatch!type()
main.pySandbox
main.py
Ctrl + Enter