Python objects are classified into Mutable and Immutable based on whether their values can be changed after creation.
Mutable Objects:
Mutable objects can be modified after they are created.
Examples
- List
- Dictionary
- Set
- Bytearray
Example
numbers = [10, 20, 30]
numbers[1] = 50
print(numbers)
Output
[10, 50, 30]
Here, the list is modified without creating a new object.
Characteristics
- Values can be changed.
- Memory location remains the same after modification.
- Suitable for dynamic data.
Immutable Objects:
Immutable objects cannot be changed after creation.
Examples
- String
- Integer
- Float
- Tuple
- Boolean
Example
name = "Python"
name = name + " Programming"
print(name)
Output
Python Programming
A new string object is created instead of modifying the existing one.
Characteristics
- Values cannot be modified.
- New object is created when changes are made.
- More secure and memory efficient.
Difference Between Mutable and Immutable Objects
| Basis | Mutable | Immutable |
|---|---|---|
| Meaning | Can be modified | Cannot be modified |
| Memory | Same object remains | New object created |
| Performance | Faster modifications | Safer data handling |
| Examples | List, Set, Dictionary | String, Tuple, Integer |
List in Python:
A List is an ordered collection of elements enclosed within square brackets [ ].
Features of List
- Ordered collection.
- Mutable in nature.
- Allows duplicate values.
- Can store different data types.
- Supports indexing and slicing.
Example
student = ["Kaif", 20, 85.5, "Commerce"]
Creating a List:
Lists can be created using square brackets.
Example
fruits = ["Apple", "Mango", "Orange"]
print(fruits)
Output
['Apple', 'Mango', 'Orange']
Initializing a List:
Initialization means assigning values while creating the list.
Example 1
numbers = [10, 20, 30, 40]
Example 2
mixed = ["Python", 100, 45.5, True]
Example 3
empty_list = []
Accessing List Elements:
List elements are accessed using indexes.
Positive Indexing
fruits = ["Apple", "Mango", "Orange"]
print(fruits[0])
print(fruits[1])
Output
Apple
Mango
Negative Indexing
fruits = ["Apple", "Mango", "Orange"]
print(fruits[-1])
print(fruits[-2])
Output
Orange
Mango
Index Representation
| Element | Apple | Mango | Orange |
|---|---|---|---|
| Positive Index | 0 | 1 | 2 |
| Negative Index | -3 | -2 | -1 |
Slicing a List:
Slicing extracts a portion of a list.
Syntax
list[start:end:step]
Example
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4])
Output
[20, 30, 40]
More Examples
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[:3])
print(numbers[2:])
print(numbers[::2])
print(numbers[::-1])
Output
[10, 20, 30]
[30, 40, 50, 60]
[10, 30, 50]
[60, 50, 40, 30, 20, 10]
Slicing Rules
- Start index is inclusive.
- End index is exclusive.
- Step determines interval.
- Negative indexing is supported.
Traversing a List:
Traversing means accessing each element of a list one by one.
Method 1: Using for Loop
fruits = ["Apple", "Mango", "Orange"]
for fruit in fruits:
print(fruit)
Output
Apple
Mango
Orange
Method 2: Using Index
fruits = ["Apple", "Mango", "Orange"]
for i in range(len(fruits)):
print(fruits[i])
Output
Apple
Mango
Orange
Method 3: Using while Loop
fruits = ["Apple", "Mango", "Orange"]
i = 0
while i < len(fruits):
print(fruits[i])
i += 1
Output
Apple
Mango
Orange
Advantages of Lists
- Easy to create and use.
- Can store multiple values in a single variable.
- Supports indexing and slicing.
- Allows modification of elements.
- Supports various built in methods.