Skip to content

Python! Tuples Sets And Dictionaries | Lectures 10

Lecture 10 Tuples Sets Dictionaries

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:

TypeDescription
TupleLike a list, butcannot be changed(immutable)
SetStoresunique items only
DictionaryStoreskey-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!")
Lecture 10 Tuples Sets Dictionaries
Lecture 10 Tuples Sets Dictionaries

πŸ“– 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

TypeCan Change?Duplicates Allowed?Best For
Listβœ… Yesβœ… YesOrdered collections
Tuple❌ Noβœ… YesFixed data
Setβœ… Yes❌ NoUnique items
Dictβœ… YesKeys must be uniqueLabeling 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.