In Python, everything is an object—and every object has a data type. Understanding data types is the foundation of writing clean, effective, and bug-free code. Whether you’re storing a name, a list of numbers, or a True/False value—knowing the right data type helps Python (and you!) handle the data properly.
In this blog, we’ll explore Python’s built-in data types and understand a key concept: mutability—what it means for a data type to be mutable or immutable.
🧮 Python’s Built-in Data Types.
Here’s a breakdown of the most commonly used data types in Python, grouped for easy understanding:
1. Numeric Types
int – Integer numbers
age = 25
float – Decimal numbers
pi = 3.14
complex – Complex numbers
z = 2 + 3j
2. Text Type
str – Strings of characters
name = "Ajay"
3. Boolean Type
bool – Represents truth values
is_smart = True
4. Sequence Types
list – Ordered, mutable sequence
fruits = ["apple", "banana", "cherry"]
tuple – Ordered, immutable sequence
coordinates = (10, 20)
range – Represents a sequence of numbers
numbers = range(1, 5)
5. Set Types
set – Unordered collection of unique items
colors = {"red", "green", "blue"}
frozenset – Immutable version of a set
frozen_colors = frozenset(["red", "green", "blue"])
6. Mapping Type
student = {"name": "Ajay", "age": 20}
7. Binary Types
Used for handling binary data like files or images:
bytes
bytearray
memoryview
b = bytes([65, 66, 67])
ba = bytearray([65, 66, 67])
🔁 Mutable vs Immutable Data Types
✅ Mutable Data Types
These can be changed after creation.
Type | Example |
---|---|
list | fruits[0] = "mango" |
dict | student["age"] = 21 |
set | colors.add("yellow") |
bytearray | ba[0] = 70 |
Example:
my_list = [1, 2, 3]
my_list[0] = 100
print(my_list) # Output: [100, 2, 3]
❌ Immutable Data Types
These cannot be changed once created.
Type | Example |
---|---|
int | a = 5 |
float | b = 3.14 |
str | "Ajay"[0] = "a" ❌ not allowed |
tuple | t[1] = 10 ❌ not allowed |
frozenset | cannot add/remove elements |
bytes | cannot modify byte data |
Example:
name = "Ajay"
# name[0] = "a" ❌ This will raise an error
🧠 Why It Matters
- Mutable types are ideal for data structures that need frequent updates.
- Immutable types are great for security, performance, and using as dictionary keys.
🔍 Quick Recap
Data Type | Mutable | Description |
---|---|---|
int | ❌ | Numbers |
float | ❌ | Decimal numbers |
str | ❌ | Text |
bool | ❌ | True/False |
list | ✅ | Ordered, changeable |
tuple | ❌ | Ordered, unchangeable |
dict | ✅ | Key-value pairs |
set | ✅ | Unordered unique items |
frozenset | ❌ | Immutable set |
bytes | ❌ | Immutable binary data |
bytearray | ✅ | Mutable binary data |
✅ Conclusion
Mastering Python’s data types is like learning the alphabet of coding. The better you understand how each type works—and whether it’s mutable or not—the more control and clarity you’ll have in your programs.
In the next blog, we’ll apply this knowledge and start manipulating these data types in real examples. Stay tuned! 🧑💻