Learn how to organize and store data! Lists, Tuples, Sets, and Dictionaries - explained with real-life examples.
Imagine you have different types of containers at home:
| Type | Symbol | Ordered? | Changeable? | Duplicates? | Example |
|---|---|---|---|---|---|
| List | [ ] |
✅ Yes | ✅ Yes | ✅ Yes | [1, 2, 2, "a"] |
| Tuple | ( ) |
✅ Yes | ❌ No | ✅ Yes | (1, 2, 2, "a") |
| Set | { } |
❌ No | ✅ Yes | ❌ No | {1, 2, "a"} |
| Dict | {k:v} |
✅ Yes* | ✅ Yes | ❌ No keys | {"name": "Ali"} |
A list is like a shopping list - you can add items, remove items, and change them anytime!
Items stay in the order you put them. You can have the same item twice!
# Creating a list - use square brackets [] fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] mixed = [1, "hello", 3.14, True] # Can mix different types! print(fruits) # Output: ['apple', 'banana', 'cherry']
fruits = ["apple", "banana", "cherry", "date"]
Index: 0 1 2 3
+-------+--------+--------+------+
| apple | banana | cherry | date |
+-------+--------+--------+------+
Negative: -4 -3 -2 -1
fruits = ["apple", "banana", "cherry", "date"] # Access by index print(fruits[0]) # Output: apple (first item) print(fruits[2]) # Output: cherry (third item) print(fruits[-1]) # Output: date (last item) # Slicing - get multiple items print(fruits[1:3]) # Output: ['banana', 'cherry'] print(fruits[::2]) # Output: ['apple', 'cherry'] (every 2nd)
fruits = ["apple", "banana"] # ADD items fruits.append("cherry") # Add to end print(fruits) # ['apple', 'banana', 'cherry'] fruits.insert(1, "mango") # Add at specific position print(fruits) # ['apple', 'mango', 'banana', 'cherry'] # REMOVE items fruits.remove("mango") # Remove by value fruits.pop() # Remove last item fruits.pop(0) # Remove by index # MODIFY items fruits[0] = "orange" # Change first item # OTHER useful operations print(len(fruits)) # Count items fruits.sort() # Sort alphabetically fruits.reverse() # Reverse order print("apple" in fruits) # Check if item exists: True/False
landing_queue = [] # Flights arrive and enter queue landing_queue.append("AI202") landing_queue.append("BA450") landing_queue.append("EK713") print("Queue:", landing_queue) # ['AI202', 'BA450', 'EK713'] # Emergency flight gets priority (insert at front!) landing_queue.insert(0, "EM999") print("After emergency:", landing_queue) # ['EM999', 'AI202', 'BA450', 'EK713'] # First flight lands (remove from front) landed = landing_queue.pop(0) print(f"{landed} has landed!") # EM999 has landed!
A tuple is like a locked display case - once you put items in, you CAN'T change them!
Use tuples when data should NEVER change (like days of the week, coordinates, etc.)
# Creating a tuple - use parentheses () days = ("Monday", "Tuesday", "Wednesday") coordinates = (10.5, 20.3) single_item = ("only one",) # Note the comma for single item! print(days) # Output: ('Monday', 'Tuesday', 'Wednesday') # Accessing items - same as lists! print(days[0]) # Output: Monday print(days[-1]) # Output: Wednesday print(days[1:3]) # Output: ('Tuesday', 'Wednesday') # TRY to change? ERROR! days[0] = "Sunday" # ❌ TypeError: tuple doesn't support assignment
| Feature | List [ ] | Tuple ( ) |
|---|---|---|
| Can change items? | ✅ Yes | ❌ No |
| Can add/remove? | ✅ Yes | ❌ No |
| Speed | Slower | Faster |
| Use when | Data may change | Data is fixed |
A set is like a bag of unique marbles - no duplicates allowed!
If you try to add the same thing twice, it just ignores it.
Items have NO order (can't access by index!)
# Creating a set - use curly braces {} fruits = {"apple", "banana", "cherry"} # Duplicates are automatically removed! numbers = {1, 2, 2, 3, 3, 3} print(numbers) # Output: {1, 2, 3} - duplicates gone! # Convert list with duplicates to set my_list = [1, 2, 2, 3, 3, 3] unique = set(my_list) print(unique) # Output: {1, 2, 3} # CAN'T access by index! print(fruits[0]) # ❌ TypeError: set doesn't support indexing
fruits = {"apple", "banana"}
# ADD items
fruits.add("cherry")
print(fruits) # {'apple', 'banana', 'cherry'}
# REMOVE items
fruits.remove("banana") # Raises error if not found
fruits.discard("mango") # No error if not found
# Check membership (VERY fast!)
print("apple" in fruits) # True
# Set math operations
a = {1, 2, 3}
b = {2, 3, 4}
print(a.union(b)) # {1, 2, 3, 4} - ALL items
print(a.intersection(b)) # {2, 3} - COMMON items
print(a.difference(b)) # {1} - In a but NOT in b
# Track unique IP addresses (no duplicates!) unique_visitors = set() unique_visitors.add("192.168.1.1") unique_visitors.add("192.168.1.2") unique_visitors.add("192.168.1.1") # Same person, ignored! print("Unique visitors:", len(unique_visitors)) # Output: Unique visitors: 2
A dictionary is like a phone book or real dictionary!
You look up a key (name/word) to find its value (phone number/definition).
Each key must be UNIQUE - you can't have two "John"s with different numbers!
person = {
"name": "Alice", ← KEY: VALUE
"age": 25, ← KEY: VALUE
"city": "New York" ← KEY: VALUE
}
Access: person["name"] → "Alice"
# Creating a dictionary - use curly braces with key:value pairs person = { "name": "Alice", "age": 25, "city": "New York" } # Access values by KEY (not index!) print(person["name"]) # Output: Alice print(person.get("age")) # Output: 25 # person[0] would give an ERROR! Must use key.
person = {"name": "Alice", "age": 25}
# ADD or UPDATE items
person["city"] = "New York" # Add new key
person["age"] = 26 # Update existing key
print(person)
# {'name': 'Alice', 'age': 26, 'city': 'New York'}
# REMOVE items
person.pop("city") # Remove by key
del person["age"] # Alternative way
# GET all keys, values, or both
print(person.keys()) # dict_keys(['name'])
print(person.values()) # dict_values(['Alice'])
print(person.items()) # dict_items([('name', 'Alice')])
# LOOP through dictionary
person = {"name": "Alice", "age": 25}
for key, value in person.items():
print(f"{key}: {value}")
# name: Alice
# age: 25
# Track which customer is at which table bookings = {} # Customer books table 3 bookings[3] = "Arjun" bookings[1] = "Priya" print("Bookings:", bookings) # {3: 'Arjun', 1: 'Priya'} # Who is at table 3? print(f"Table 3: {bookings[3]}") # Table 3: Arjun # Customer leaves del bookings[3] print("After checkout:", bookings) # {1: 'Priya'}
In one sentence: when would you choose a tuple over a list, and why?
| Situation | Use This | Why? |
|---|---|---|
| Shopping list that changes | List | Need to add/remove items |
| Days of the week | Tuple | Never changes, faster |
| Track unique visitors | Set | No duplicates needed |
| Student grades by name | Dict | Look up by key (name) |
| Ordered collection, duplicates OK | List | Most flexible |
| GPS coordinates | Tuple | Shouldn't change |
These are the building blocks of ALL Python programs!