Python Data Types Overview
Table of Contents
In the last lesson, we learned f-strings. Now let’s step back and review the main Python data types together.
After learning Python f-Strings, we can format different values inside text. Now let’s step back and review Python’s main data types together. In this tutorial, we’ll learn about Python data types with easy examples.
Let’s Take a Step Back
We have learned about a lot of things so far. Numbers. Strings. Booleans. Variables. Type conversion. Now it is time to put it all together. Let’s look at the big picture.
What types of data does Python have? How many are there? Which ones matter for beginners? Let’s go through them.
What Is a Data Type in Python?
A data type is just a category of data. It tells Python what kind of value we are working with. Is it a number? Is it text? Is it true or false? That is what a data type tells us.
Think of it like this. In a kitchen, we have different types of containers:
- A bottle for water
- A box for cereal
- A bag for vegetables
Each container is made for a specific type of thing. We would not put water in a cereal box. Python is the same. Different data types for different kinds of data.
The Main Data Types in Python
Python has several built-in data types. Here are the ones we will use most.
1 Integer (int)
Whole numbers. No decimal point.
age = 25score = 100temperature = -5Type name: int
Float (float)
Decimal numbers.
height = 5.9price = 19.99pi = 3.14159Type name: float
String (str)
Text. Anything inside quotes.
name = "Alex"message = "Hello, world"address = "123 Main Street"Type name: str
Boolean (bool)
True or False. Nothing else.
is_student = Trueis_raining = FalseType name: bool
List (list)
An ordered collection of items. We can change it.
numbers = [1, 2, 3, 4, 5]names = ["Alex", "Sam", "John"]mixed = [1, "hello", True, 3.14]Type name: list
We will learn about lists in detail later.
Tuple (tuple)
An ordered collection. But we cannot change it.
coordinates = (10, 20)colors = ("red", "green", "blue")Type name: tuple
Notice the parentheses. That is how we tell a tuple from a list. We will learn about tuples later.
Dictionary (dict)
A collection of key-value pairs.
person = {"name": "Alex", "age": 25}prices = {"apple": 1.50, "banana": 0.75}Type name: dict
Notice the curly braces and the colons. That is how we tell a dictionary. We will learn about dictionaries later.
Set (set)
An unordered collection of unique items.
numbers = {1, 2, 3, 4, 5}letters = {"a", "b", "c"}Type name: set
Notice the curly braces. But no colons. That is how we tell a set from a dictionary. We will learn about sets later.
NoneType (None)
This is a special type. It means “nothing” or “no value.”
result = NoneType name: NoneType
It is not zero. It is not empty string. It is nothing.
How to Check the Type in Python
We can always check the type of a value using type().
print(type(25)) # <class 'int'>print(type(3.14)) # <class 'float'>print(type("hello")) # <class 'str'>print(type(True)) # <class 'bool'>print(type([1, 2, 3])) # <class 'list'>print(type((1, 2, 3))) # <class 'tuple'>print(type({"a": 1})) # <class 'dict'>print(type({1, 2, 3})) # <class 'set'>print(type(None)) # <class 'NoneType'>That is how we check. Just use type().
Mutable vs Immutable in Python
Here is an important idea. Some data types can be changed. Some cannot.
Mutable means “can be changed.” Immutable means “cannot be changed.”
Let me show.
Immutable Types
int— cannot change a numberfloat— cannot change a floatstr— cannot change a stringbool— cannot change a booleantuple— cannot change a tuple
If we try to change an immutable type, we get an error.
name = "Alex"name[0] = "B" # Error!Mutable Types
list— can change a listdict— can change a dictionaryset— can change a set
numbers = [1, 2, 3]numbers[0] = 10 # Works!print(numbers) # [10, 2, 3]See? Lists can be changed. Strings cannot. This is important. Let’s remember it.
Python Data Types: A Real-Life Example
Think of it like this. A string is like a printed book. Once it is printed, we cannot change the words. We can only make a new book. A list is like a whiteboard. We can erase and write new things anytime.
That is the difference between immutable and mutable.
Python Data Types Summary Table
Let me show a table.
| Type | Name | Example | Mutable? |
|---|---|---|---|
| Integer | int | 25 | No |
| Float | float | 3.14 | No |
| String | str | "hello" | No |
| Boolean | bool | True | No |
| List | list | [1, 2, 3] | Yes |
| Tuple | tuple | (1, 2, 3) | No |
| Dictionary | dict | {"a": 1} | Yes |
| Set | set | {1, 2, 3} | Yes |
| None | NoneType | None | No |
Which Python Data Types Should We Learn First?
For now, let’s focus on these four:
int— whole numbersfloat— decimal numbersstr— textbool— true or false
These are the building blocks. Everything else builds on top of them. The others — list, tuple, dict, set — we will learn later. They are collections. They hold multiple values.
Converting Between Python Data Types
We learned this in the type conversion article. But let me remind us.
x = 25print(str(x)) # "25"print(float(x)) # 25.0print(bool(x)) # Truey = "25"print(int(y)) # 25print(float(y)) # 25.0We can convert between most types. But not all conversions make sense.
z = "hello"print(int(z)) # Error! Cannot convert "hello" to a numberSo let’s be careful.
A Complete Python Data Types Example
Let’s put it all together.
# Different types of dataname = "Alex" # strage = 25 # intheight = 5.9 # floatis_student = True # bool
# Print themprint(f"Name: {name}")print(f"Age: {age}")print(f"Height: {height}")print(f"Is student: {is_student}")
# Print their typesprint()print(f"Type of name: {type(name)}")print(f"Type of age: {type(age)}")print(f"Type of height: {type(height)}")print(f"Type of is_student: {type(is_student)}")Output
Name: AlexAge: 25Height: 5.9Is student: True
Type of name: <class 'str'>Type of age: <class 'int'>Type of height: <class 'float'>Type of is_student: <class 'bool'>See? Each value has a type. And Python knows what it is.
Why Does This Matter?
We might be thinking — why do we need to know all this? Because different types behave differently. We can do math with numbers. But not with strings. We can concatenate strings. But not with numbers. We can change a list. But not a string. So knowing the type helps us know what we can and cannot do.
Next, we will learn about Arithmetic Operators so we can start doing calculations with values.