Table of Contents
Why `type()` Is Useful
When you write Python programs, you often need to know what kind of data you are working with:
- Is this a number I can add?
- Is this text I should join together?
- Is this a list I should loop over?
Python can tell you the data type of any value or variable using the built‑in function type().
type() is especially helpful when:
- You’re learning and want to see what type something is.
- You’re debugging and something doesn’t behave as expected.
- You want to confirm that an input was converted to the right type.
The Basic Use of `type()`
The type() function takes one value (or variable) and returns its type.
General pattern:
$$\text{result} = \text{type}(\text{value\_or\_variable})$$
Example with values directly:
print(type(5)) # integer
print(type(3.14)) # float
print(type("hello")) # string
print(type(True)) # bool (boolean)Example with variables:
age = 25
price = 19.99
name = "Alice"
is_member = True
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(is_member)) # <class 'bool'>The output is usually shown as something like:
<class 'int'><class 'float'><class 'str'><class 'bool'>
For now, you can read these as “this value is of type int/float/str/bool”.
Using `type()` to Explore Different Types
As you meet new types in later chapters (lists, dictionaries, etc.), you can use type() to see what they are:
numbers = [1, 2, 3]
person = {"name": "Bob", "age": 30}
unique_numbers = {1, 2, 3}
point = (10, 20)
print(type(numbers)) # <class 'list'>
print(type(person)) # <class 'dict'>
print(type(unique_numbers)) # <class 'set'>
print(type(point)) # <class 'tuple'>This is a quick way to become familiar with new data structures: create one, then check its type.
Checking Types After Conversions
When you convert between types (like turning text into a number), type() helps confirm that the conversion worked.
Example with user input and conversion:
user_input = input("Enter a number: ") # always a string
print(type(user_input)) # <class 'str'>
number = int(user_input) # convert to int
print(type(number)) # <class 'int'>
This makes it clear that input() gives you a str, and after int(), you have an int.
Storing and Comparing Types
Because type() returns a value, you can:
- Store it in a variable.
- Compare it to another type.
Storing a type
x = 42
x_type = type(x)
print(x_type) # <class 'int'>Comparing types directly
You can compare the result of type() with a type like int, float, etc.
Pattern:
$$\text{type}(\text{value}) == \text{SomeType}$$
Example:
x = 3.14
if type(x) == float:
print("x is a float")
else:
print("x is not a float")For beginners, this kind of check is often enough when you really need it.
A Common Use: Debugging Surprises
If something in your program behaves strangely, checking the type often reveals the problem.
Example: adding numbers vs. joining strings
a = "10"
b = "20"
print(a + b) # What do you expect?
print(type(a), type(b))Output:
a + bgives1020(string concatenation)type(a)andtype(b)are both<class 'str'>
After inspecting the type, you know you need to convert:
a = int(a)
b = int(b)
print(a + b) # 30
print(type(a), type(b)) # <class 'int'> <class 'int'>
Using type() like this is a simple but powerful debugging step: “What type is this really?”
Printing Types Clearly
You’ll often want to print both the value and its type in a readable way.
Example:
value = "hello"
print("Value:", value, "| Type:", type(value))Sample outputs for different values:
x = 100
print("x:", x, "| type:", type(x)) # x: 100 | type: <class 'int'>
x = 100.0
print("x:", x, "| type:", type(x)) # x: 100.0 | type: <class 'float'>
x = "100"
print("x:", x, "| type:", type(x)) # x: 100 | type: <class 'str'>Seeing these side by side helps you understand both the value and its data type.
Using `type()` Interactively
In interactive mode (REPL) or a notebook, you can quickly experiment:
>>> type(1)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> type("1")
<class 'str'>
>>> type(True)
<class 'bool'>Try:
type([])type(())type({})type(set())
to see the types of different collection objects as you learn them later.
When to Use `type()` (and When Not To)
Use type() when:
- You are learning and want to inspect values.
- You are debugging and something doesn’t act as you expect.
- You want to verify that a conversion or operation gave you the type you intended.
In many cases, you don’t need to check types in your final code all the time—Python is designed so you can usually just use the values. But while you’re learning, type() is like an “X‑ray” that lets you see what’s really going on.
Small Practice Ideas
You can practice with type() using simple mini‑tasks:
- Create one variable of each basic type you know (
int,float,str,bool) and print its type. - Use
input()to read something, print its type, convert it to a number, and print the new type. - Make a small list (e.g.,
[1, "two", 3.0, False]) and usetype()on each element inside a loop (once you learn loops).
In all these cases, the goal is the same: get comfortable seeing and understanding what type your data is in Python.