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. Numeric data types store numerical values and support mathematical operations. Python provides three main numeric data types: Integer (int): Represents whole numbers. Example: Float (float): Represents decimal or floating-point numbers. Example: Boolean (bool): Represents Complex (complex): Represents complex numbers in the form Sequence data types store collections of values. These values can be indexed and accessed efficiently. A list is an ordered collection of values that can be modified (mutable). Example: A tuple is similar to a list but immutable, meaning its values cannot be changed after assignment. Example: A string is a sequence of characters enclosed in single or double quotes. Example: Bytes: Immutable sequences of byte values (0-255). Example: Byte Arrays: Similar to bytes but mutable. Example: Sets store unique, unordered values and do not support indexing. A set is a collection of unique elements, and it does not allow duplicate values. Example: A frozen set is an immutable version of a set. Example: A dictionary stores key-value pairs, making data retrieval efficient. In Python, mutable and immutable refer to whether an object's value can be changed after it is created. 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 ( x = 10 🚀 Explanation: Example with Strings ( s = "Hello" 🚀 Explanation: 🔹 Mutable Example ( lst = [1, 2, 3] ✅ The list is modified without changing its memory address. 🔹 Immutable Example ( text = "Python" ❌ Strings cannot be modified in place; Python creates a new object instead. ✔️ Even though we can "change" 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!Categories of Data Types in Python
1. Numeric Data Types
x = 10 # Integery = 19.25 # FloatTrue or False. Example:is_active = True # Booleana + bj. Example:z = 2 + 3j # Complex number2. Sequence Data Types
a) List
numbers = [2, 4, 6, 8, 10] # List
numbers[0] = 12 # Lists are mutable{codeBox}b) Tuple
tuple_values = (2, 4, 6, 8, 10) # Tuplec) String
message = "Hello, Python!" # Stringd) Bytes and Byte Arrays
byte_data = bytes([65, 66, 67]) # Immutablebyte_array_data = bytearray([65, 66, 67]) # Mutable3. Set Data Types
a) Set
unique_numbers = {2, 4, 6, 8, 10} # Setb) Frozen Set
immutable_set = frozenset({2, 4, 6, 8, 10}) # Frozen set4. Mapping Data Type (Dictionary)
student_info = {"name": "John", "roll": 126, "department": "CS"} # DictionaryMutable Objects
list)dict)set)bytearray)int)
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)
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.str)
print(id(s)) # Example output: 140728537272432
s = s + " World" # Creates a new string object
print(id(s)) # Example output: 140728537273216 (new memory address)
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
list):
print(id(lst)) # Example: 140728537333184
lst.append(4) # Modify the list
print(id(lst)) # Same memory address: 140728537333184str):
print(id(text)) # Example: 140728537272432
text += " Rocks" # Creates a new string
print(id(text)) # Different memory address: 140728537273216int 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
.jpg)
Comments
Post a Comment