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.