Part 1 — Tuples
What is a Tuple?
A tuple is exactly like a list — it stores multiple values. But with one big difference:
Lists are mutable — you can change them after creation Tuples are immutable — once created, you cannot change them
# List — can be changed
fruits_list = ["apple", "banana", "mango"]
# Tuple — cannot be changed
fruits_tuple = ("apple", "banana", "mango")
Only difference in syntax — list uses [], tuple uses ().
Creating a Tuple
coordinates = (28.6, 77.2) # latitude, longitude
rgb = (255, 0, 128) # color values
days = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
person = ("Gagan", 22, "Delhi") # mixed types
# Single item tuple — needs a trailing comma
single = (42,) # this is a tuple
not_tuple = (42) # this is just a number in brackets — NOT a tuple
Accessing Items — Same as List
person = ("Gagan", 22, "Delhi")
print(person[0]) # Gagan
print(person[1]) # 22
print(person[-1]) # Delhi — negative indexing works too
Slicing works too:
days = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
print(days[0:5]) # ('Mon', 'Tue', 'Wed', 'Thu', 'Fri')
print(days[-2:]) # ('Sat', 'Sun')
Tuples are Immutable — Cannot Change
colors = ("red", "green", "blue")
colors[0] = "yellow" # ERROR! TypeError: tuple does not support item assignment
Once a tuple is created, its values are locked. No adding, removing, or changing.
Why Use Tuple If You Can't Change It?
Great question. Here's why tuples exist:
1. Safety — When data should never change, use a tuple. Like days of the week, months of the year, coordinates of a fixed location. Using a tuple signals to anyone reading your code "this data is not supposed to change."
2. Speed — Tuples are slightly faster than lists because Python knows they won't change.
3. Dictionary keys — Tuples can be used as dictionary keys, lists cannot. (We'll see this soon.)
Real world examples of tuple data:
months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
directions = ("North", "South", "East", "West")
status_codes = (200, 301, 404, 500)
These values will never change — tuple is perfect.
Tuple Methods
Tuples have only 2 methods (because you can't modify them):
numbers = (1, 2, 3, 2, 4, 2, 5)
print(numbers.count(2)) # 3 — how many times 2 appears
print(numbers.index(4)) # 4 — index of value 4
print(len(numbers)) # 7 — total items
Tuple Unpacking — Very Useful
You can unpack tuple values directly into variables:
person = ("Gagan", 22, "Delhi")
name, age, city = person # unpacking
print(name) # Gagan
print(age) # 22
print(city) # Delhi
This works with lists too. You've seen a version of this before with enumerate().
Looping Through Tuple
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
for day in days:
print(day)
Exactly same as looping through a list.
Part 2 — Dictionaries
What is a Dictionary?
A list stores items by index number — 0, 1, 2, 3...
A dictionary stores items by custom keys — like a real dictionary where you look up words.
# List — access by number
person_list = ["Gagan", 22, "Delhi"]
print(person_list[0]) # Gagan — but what does index 0 mean?
# Dictionary — access by name
person_dict = {"name": "Gagan", "age": 22, "city": "Delhi"}
print(person_dict["name"]) # Gagan — clear and readable!
Dictionary is made of key-value pairs:
"name"is the key"Gagan"is the value
Creating a Dictionary
student = {
"name": "Gagan",
"age": 22,
"city": "Delhi",
"is_student": True,
"marks": 88.5
}
print(student)
Output:
{'name': 'Gagan', 'age': 22, 'city': 'Delhi', 'is_student': True, 'marks': 88.5}
Accessing Values
student = {
"name": "Gagan",
"age": 22,
"city": "Delhi"
}
print(student["name"]) # Gagan
print(student["age"]) # 22
print(student["city"]) # Delhi
If key doesn't exist — you get a KeyError:
print(student["phone"]) # KeyError: 'phone'
Safe way using .get() — returns None instead of error:
print(student.get("phone")) # None — no error
print(student.get("phone", "N/A")) # N/A — custom default value
Always prefer .get() when you're not sure if the key exists.
Adding and Updating Values
student = {"name": "Gagan", "age": 22}
# Adding new key
student["city"] = "Delhi"
student["marks"] = 88.5
# Updating existing key
student["age"] = 23
print(student)
Output:
{'name': 'Gagan', 'age': 23, 'city': 'Delhi', 'marks': 88.5}
Removing Items
student = {"name": "Gagan", "age": 22, "city": "Delhi", "marks": 88.5}
del student["marks"] # delete by key
print(student)
removed = student.pop("city") # remove and return value
print(removed) # Delhi
print(student)
Output:
{'name': 'Gagan', 'age': 22, 'city': 'Delhi'}
Delhi
{'name': 'Gagan', 'age': 22}
Dictionary Methods
student = {"name": "Gagan", "age": 22, "city": "Delhi"}
print(student.keys()) # dict_keys(['name', 'age', 'city'])
print(student.values()) # dict_values(['Gagan', 22, 'Delhi'])
print(student.items()) # dict_items([('name', 'Gagan'), ('age', 22), ('city', 'Delhi')])
print(len(student)) # 3 — number of key-value pairs
print("name" in student) # True — check if key exists
Looping Through a Dictionary
student = {"name": "Gagan", "age": 22, "city": "Delhi"}
# Loop through keys only
for key in student:
print(key)
# Loop through keys and values together — most common
for key, value in student.items():
print(f"{key}: {value}")
Output:
name: Gagan
age: 22
city: Delhi
Dictionary Inside a List — Very Common Pattern
This is how real-world data looks — a list of dictionaries:
students = [
{"name": "Rahul", "age": 20, "marks": 85},
{"name": "Priya", "age": 21, "marks": 92},
{"name": "Gagan", "age": 22, "marks": 78}
]
# Access individual student
print(students[0]["name"]) # Rahul
print(students[1]["marks"]) # 92
# Loop through all students
for student in students:
print(f"Name: {student['name']}, Marks: {student['marks']}")
Output:
Name: Rahul, Marks: 85
Name: Priya, Marks: 92
Name: Gagan, Marks: 78
This pattern is everywhere in real apps — when you fetch data from a database or API, it usually comes back in this format.
Nested Dictionary — Dictionary Inside Dictionary
company = {
"name": "TechCorp",
"location": "Delhi",
"ceo": {
"name": "Amit Shah",
"age": 45,
"email": "amit@techcorp.com"
}
}
print(company["name"]) # TechCorp
print(company["ceo"]["name"]) # Amit Shah
print(company["ceo"]["email"]) # amit@techcorp.com
Real World Example — Contact Book
contacts = {}
def add_contact():
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email: ")
contacts[name] = {"phone": phone, "email": email}
print(f"{name} added to contacts!")
def view_contacts():
if len(contacts) == 0:
print("No contacts saved")
else:
print("\n=== All Contacts ===")
for name, info in contacts.items():
print(f"Name: {name}")
print(f" Phone: {info['phone']}")
print(f" Email: {info['email']}")
print()
def search_contact():
name = input("Enter name to search: ")
if name in contacts:
info = contacts[name]
print(f"\nName: {name}")
print(f"Phone: {info['phone']}")
print(f"Email: {info['email']}")
else:
print("Contact not found")
def delete_contact():
name = input("Enter name to delete: ")
if name in contacts:
del contacts[name]
print(f"{name} deleted!")
else:
print("Contact not found")
while True:
print("\n=== Contact Book ===")
print("1. Add contact")
print("2. View all contacts")
print("3. Search contact")
print("4. Delete contact")
print("5. Exit")
choice = input("Enter choice: ")
if choice == "1":
add_contact()
elif choice == "2":
view_contacts()
elif choice == "3":
search_contact()
elif choice == "4":
delete_contact()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice")
This is a proper mini application — using dictionaries, lists, functions, loops, and conditions all together.
List vs Tuple vs Dictionary — When to Use What
| List | Tuple | Dictionary | |
|---|---|---|---|
| Syntax | [] |
() |
{} |
| Can change? | Yes | No | Yes |
| Access by | Index number | Index number | Key name |
| Use when | Order matters, data changes | Data is fixed | Key-value pairs, lookup by name |
| Example | Shopping cart, student marks | Coordinates, RGB colors, months | User profile, contact info |
Exercise 🏋️
Build a Simple Inventory System:
- Start with this inventory already in a dictionary:
inventory = {
"apple": {"price": 20, "quantity": 50},
"banana": {"price": 10, "quantity": 100},
"mango": {"price": 40, "quantity": 30}
}
- Build a menu with these options:
- View all items (show name, price, quantity)
- Add new item (ask name, price, quantity)
- Update quantity of existing item
- Remove item
- Exit
Expected view output:
=== Inventory ===
apple - Price: Rs.20, Quantity: 50
banana - Price: Rs.10, Quantity: 100
mango - Price: Rs.40, Quantity: 30
This exercise combines lists, dictionaries, functions, loops, and conditions — basically everything from Stage 1 to Stage 4!
No comments:
Post a Comment