๐ Data Structures in Python โ Complete Study Notes
Python provides several built-in data structures that help organize, store, and manipulate data efficiently. The four core ones are Lists, Tuples, Dictionaries, and Sets. Each serves a unique purpose depending on the situation.
๐น 1. Lists in Python
โ Definition
A list is an ordered, mutable (changeable) collection of elements. It allows duplicate values and can store different data types.
๐ Key Features
- Ordered (maintains insertion order)
- Mutable (can be modified)
- Allows duplicates
- Can store mixed data types
๐ป Example Code
# Creating a list
my_list = [10, 20, 30, 40, 50]
# Accessing elements
print(my_list[0]) # First element
print(my_list[-1]) # Last element
# Modifying elements
my_list[1] = 25
# Adding elements
my_list.append(60)
# Removing elements
my_list.remove(30)
print(my_list)
๐งพ Output
10
50
[10, 25, 40, 50, 60]
๐ Common List Operations
numbers = [1, 2, 3]
numbers.append(4) # Add at end
numbers.insert(1, 10) # Insert at index
numbers.pop() # Remove last
numbers.sort() # Sort list
print(numbers)
๐งพ Output
[1, 2, 3, 10]
๐ฏ Use Cases
- Storing sequences (marks, names)
- Iterating data
- Dynamic collections
๐น 2. Tuples in Python
โ Definition
A tuple is an ordered, immutable collection of elements. Once created, it cannot be changed.
๐ Key Features
- Ordered
- Immutable (cannot modify)
- Allows duplicates
- Faster than lists (in many cases)
๐ป Example Code
# Creating a tuple
my_tuple = (10, 20, 30, 40)
# Accessing elements
print(my_tuple[0])
print(my_tuple[-1])
# Tuple with mixed data types
mixed_tuple = (1, "Hello", 3.5)
print(mixed_tuple)
๐งพ Output
10
40
(1, 'Hello', 3.5)
โ ๏ธ Immutability Example
my_tuple = (1, 2, 3)
# This will cause an error
# my_tuple[0] = 10
๐งพ Output
TypeError: 'tuple' object does not support item assignment
๐ฏ Use Cases
- Fixed data (coordinates, configuration)
- Faster read-only operations
- Returning multiple values from functions
๐น 3. Dictionaries in Python
โ Definition
A dictionary stores data in key-value pairs. It is unordered (before Python 3.7), but now maintains insertion order.
๐ Key Features
- Key-value structure
- Keys must be unique
- Mutable
- Fast lookup using keys
๐ป Example Code
# Creating a dictionary
student = {
"name": "Rahul",
"age": 21,
"course": "BCA"
}
# Accessing values
print(student["name"])
# Adding new key-value pair
student["marks"] = 85
# Updating value
student["age"] = 22
# Removing element
del student["course"]
print(student)
๐งพ Output
Rahul
{'name': 'Rahul', 'age': 22, 'marks': 85}
๐ Dictionary Methods
data = {"a": 1, "b": 2}
print(data.keys())
print(data.values())
print(data.items())
๐งพ Output
dict_keys(['a', 'b'])
dict_values([1, 2])
dict_items([('a', 1), ('b', 2)])
๐ฏ Use Cases
- Storing structured data (like JSON)
- Fast searching using keys
- Representing real-world objects
๐น 4. Sets in Python
โ Definition
A set is an unordered collection of unique elements. It does not allow duplicates.
๐ Key Features
- Unordered
- No duplicate values
- Mutable
- Useful for mathematical operations
๐ป Example Code
# Creating a set
my_set = {1, 2, 3, 4}
# Adding elements
my_set.add(5)
# Removing elements
my_set.remove(2)
print(my_set)
๐งพ Output
{1, 3, 4, 5}
๐ Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
print(A.union(B)) # Union
print(A.intersection(B)) # Intersection
print(A.difference(B)) # Difference
๐งพ Output
{1, 2, 3, 4, 5}
{3}
{1, 2}
๐ฏ Use Cases
- Removing duplicates
- Membership testing
- Mathematical operations
๐ Comparison Table
| Feature | List | Tuple | Dictionary | Set |
|---|---|---|---|---|
| Ordered | Yes | Yes | Yes (3.7+) | No |
| Mutable | Yes | No | Yes | Yes |
| Duplicates | Allowed | Allowed | Keys: No | Not Allowed |
| Indexing | Yes | Yes | No | No |
๐ง Final Summary
- Lists โ Flexible, most commonly used
- Tuples โ Fixed data, faster
- Dictionaries โ Key-value mapping
- Sets โ Unique elements, mathematical operations
๐ Practice Question
# Write a program to remove duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers)
๐งพ Output
[1, 2, 3, 4, 5]
If you want, I can convert these notes into PDF, handwritten-style notes, or exam revision sheets.
๐ Great progress! Back to Learning Path โฌ