Lets Start Python Programming For Beginners to advanced. Tuples, Sets & Dictionaries, Lectures 10
π Lecture 10: Tuples, Sets & Dictionaries
π― Goal
By the end of this lecture, you will:
- Understand tuples , sets , and dictionaries .
- Know when to use each one.
- Be able to create and work with them in Python.
- Use dictionaries like a phonebook to look up information.
π§ What Are These Data Types?
So far, we’ve learned about lists β which are great for storing multiple items in order.
Now letβs learn about three more powerful tools:
Type | Description |
---|---|
Tuple | Like a list, butcannot be changed(immutable) |
Set | Storesunique items only |
Dictionary | Storeskey-value pairs, like a real dictionary or phonebook |
Letβs explore each one!
π 1. Tuples β Immutable Lists
A tuple is like a list that you cannot change after it’s created.
How to Create a Tuple:
coordinates = (10, 20)
You can access items just like in a list:
print(coordinates[0]) # Output: 10
print(coordinates[1]) # Output: 20
But if you try to change an item:
coordinates[0] = 5 # β Error!
π Youβll get an error because tuples cannot be changed.
When to Use Tuples:
- When you want to store data that should not change (like coordinates, dates).
- They’re faster than lists and safer from accidental changes.
π 2. Sets β Unique Items Only
A set stores items like a list, but each item must be unique β no duplicates allowed.
How to Create a Set:
fruits = {"apple", "banana", "cherry"}
If you try to add something already there, it wonβt appear twice:
fruits.add("apple")
print(fruits) # Still only one "apple"
Common Uses:
- Removing duplicates from a list:
names = ["Alice", "Bob", "Alice", "Charlie"]
unique_names = set(names)
print(unique_names) # {'Alice', 'Bob', 'Charlie'}
Checking fast if something exists:
if "apple" in fruits:
print("Apple is in the set!")

π 3. Dictionaries β Key-Value Pairs
A dictionary stores data as pairs : a key and a value .
Think of it like a real phonebook :
- The key is the person’s name.
- The value is their phone number.
How to Create a Dictionary:
phonebook = {
"Alice": "123",
"Bob": "456",
"Charlie": "789"
}
Accessing Values:
print(phonebook["Alice"]) # Output: 123
Adding or Updating:
phonebook["David"] = "000"
phonebook["Bob"] = "999" # Bob's number changes
Deleting:
del phonebook["Charlie"]
Looping Through a Dictionary:
for name, number in phonebook.items():
print(f"{name}: {number}")
π§ͺ Try It Yourself!
Task 1: Store Your Friendsβ Favorite Colors
Use a dictionary:
favorites = {
"Emma": "blue",
"Liam": "green",
"Noah": "red"
}
print(favorites["Liam"]) # Output: green
Task 2: Remove Duplicate Numbers from a List
Use a set:
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers) # {1, 2, 3, 4, 5}
π Challenge (Optional)
Build a Simple Phonebook App
Allow users to:
- Add contacts
- Look up numbers
- Delete contacts
Example:
phonebook = {}
while True:
action = input("What do you want to do? (add, lookup, delete, quit): ")
if action == "add":
name = input("Enter name: ")
number = input("Enter number: ")
phonebook[name] = number
elif action == "lookup":
name = input("Who are you looking for? ")
print(phonebook.get(name, "Not found!"))
elif action == "delete":
name = input("Who do you want to delete? ")
if name in phonebook:
del phonebook[name]
print("Deleted.")
else:
print("Name not found.")
elif action == "quit":
print("Goodbye!")
break
π§ Kids Corner
π§ Imagine you have a robot friend who has a backpack:
- One pocket holds a tuple β he can look inside but never change whatβs there.
- Another has a set β he keeps only one of each toy (no copies!).
- The last one is a dictionary β itβs like a sticker book where every sticker has a label.
π€ Robot says:
I know how to keep things safe and organized now!
π Summary
Type | Can Change? | Duplicates Allowed? | Best For |
---|---|---|---|
List | β Yes | β Yes | Ordered collections |
Tuple | β No | β Yes | Fixed data |
Set | β Yes | β No | Unique items |
Dict | β Yes | Keys must be unique | Labeling values |
- Use tuples for data that shouldnβt change.
- Use sets to remove duplicates or check membership fast.
- Use dictionaries to store labeled data like names and phone numbers.
Python Programming For Beginners To Advanced
Python Programming For Beginners! Lecture 1
Python Programming! Variables and Data Types | Lectures 2
Python Programming! Input And Output | Lectures 3
Python Programming! Operators in Python Lectures 4
Python Programming! Conditional Statements Lectures 5
Python! Loops For and While | Lectures 6
Python! Building A Mini Game | Lectures 7
Python! Functions | Lectures 8
Python! Storing Multiple Values | Lectures 9
Python! Tuples Sets And Dictionaries | Lectures 10
Stay Updated
If you found this information useful, donβt forget to bookmark this page and Share and leave your feedback in the comment section below.