Categories of Data Types in Python
Python is a powerful and versatile programming language that supports a rich set of data types. In this lecture, we will explore the various categories of data types available in Python and provide a brief introduction to each of them.
Categories of Data Types in Python
Python data types can be broadly categorized into:
Numeric Types
Sequence Types
Set Types
Mapping Types (Dictionary)
Each of these categories plays a crucial role in Python programming, and we will explore them in detail.
1. Numeric Data Types
Numeric data types store numerical values and support mathematical operations. Python provides three main numeric data types:
Integer (int): Represents whole numbers. Example:
x = 10 # Integer
Float (float): Represents decimal or floating-point numbers. Example:
y = 19.25 # Float
Boolean (bool): Represents
True
orFalse
. Example:is_active = True # Boolean
Complex (complex): Represents complex numbers in the form
a + bj
. Example:z = 2 + 3j # Complex number
2. Sequence Data Types
Sequence data types store collections of values. These values can be indexed and accessed efficiently.
a) List
A list is an ordered collection of values that can be modified (mutable). Example:
numbers = [2, 4, 6, 8, 10] # List numbers[0] = 12 # Lists are mutable
{codeBox}
b) Tuple
A tuple is similar to a list but immutable, meaning its values cannot be changed after assignment. Example:
tuple_values = (2, 4, 6, 8, 10) # Tuple
c) String
A string is a sequence of characters enclosed in single or double quotes. Example:
message = "Hello, Python!" # String
d) Bytes and Byte Arrays
Bytes: Immutable sequences of byte values (0-255). Example:
byte_data = bytes([65, 66, 67]) # Immutable
Byte Arrays: Similar to bytes but mutable. Example:
byte_array_data = bytearray([65, 66, 67]) # Mutable
3. Set Data Types
Sets store unique, unordered values and do not support indexing.
a) Set
A set is a collection of unique elements, and it does not allow duplicate values. Example:
unique_numbers = {2, 4, 6, 8, 10} # Set
b) Frozen Set
A frozen set is an immutable version of a set. Example:
immutable_set = frozenset({2, 4, 6, 8, 10}) # Frozen set
4. Mapping Data Type (Dictionary)
A dictionary stores key-value pairs, making data retrieval efficient.
student_info = {"name": "John", "roll": 126, "department": "CS"} # Dictionary
In Python, mutable and immutable refer to whether an object's value can be changed after it is created.
Mutable Objects
- Mutable objects can be changed after creation.
- You can modify their contents without changing their identity (i.e., their memory address remains the same).
- Examples of mutable objects:
- Lists (
list
) - Dictionaries (
dict
) - Sets (
set
) - Bytearray (
bytearray
)
- Lists (
Example of a Mutable Object (List)
However, while integers (int) and strings (str) might seem like they can be changed, they are actually immutable because any modification creates a new object instead of changing the existing one.
Example with Integers (int
)
x = 10
print(id(x)) # Example output: 140728537274128 (memory address of x)
x = x + 5 # This creates a new object
print(id(x)) # Example output: 140728537274288 (new memory address)
🚀 Explanation:
When x = x + 5
is executed, Python does not modify the original 10
. Instead, it creates a new integer 15
and assigns it to x
. The old value 10
is discarded if it is no longer referenced.
Example with Strings (str
)
s = "Hello"
print(id(s)) # Example output: 140728537272432
s = s + " World" # Creates a new string object
print(id(s)) # Example output: 140728537273216 (new memory address)
🚀 Explanation:
Strings are immutable, so modifying s
actually creates a new string "Hello World"
, and s
now refers to this new object. The original "Hello"
remains unchanged.
Mutable vs Immutable Behavior
🔹 Mutable Example (list
):
lst = [1, 2, 3]
print(id(lst)) # Example: 140728537333184
lst.append(4) # Modify the list
print(id(lst)) # Same memory address: 140728537333184
✅ The list is modified without changing its memory address.
🔹 Immutable Example (str
):
text = "Python"
print(id(text)) # Example: 140728537272432
text += " Rocks" # Creates a new string
print(id(text)) # Different memory address: 140728537273216
❌ Strings cannot be modified in place; Python creates a new object instead.
✔️ Even though we can "change" int
and str
, Python actually creates new objects rather than modifying the existing ones.
✔️ Mutable objects (like lists
) modify the same object in memory, while immutable objects (like ints
and strs
) always create new objects when modified.
Summary
In this blog, we introduced the various data types in Python. Each of these data types has its unique characteristics and is used for specific purposes. In the upcoming lectures, we will explore each data type in more detail.
Stay tuned for the next lecture, where we will dive deeper into numeric data types in Python!