Python Course in 4 to 6 Weeks – Lessons 8 & 9: Lists, Tuples, Dictionaries & Sets

📦 Python Course in 4 to 6 Weeks – Lessons 8 & 9: Lists, Tuples, Dictionaries & Sets

Welcome to Lessons 8 and 9 of our Python Course in 4 to 6 Weeks! In this combined lesson, you'll master four of Python's most powerful collection types: lists, tuples, dictionaries, and sets. These data structures allow you to group and manage related data efficiently.

✅ What You'll Learn

  • How and when to use list, tuple, dict, and set
  • Key operations for each type
  • Real-world examples and best practices
  • Differences between them and when to choose each

📋 Lists

Lists are ordered, changeable, and allow duplicate elements.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # apple
fruits.append("orange")
print(fruits)

Common operations: append(), remove(), pop(), len(), sort()

🔒 Tuples

Tuples are ordered and unchangeable (immutable).

dimensions = (800, 600)
print(dimensions[1])  # 600

Tuples use less memory and are faster than lists in many cases.

📑 Dictionaries

Dictionaries store data in key-value pairs. They're unordered (in Python <3 .7="" and="" mutable.="" p="">

user = {"name": "Amr", "age": 25}
print(user["name"])
user["age"] = 26

Common methods: .get(), .keys(), .values(), .items()

📂 Sets

Sets are unordered, unindexed, and contain only unique values.

colors = {"red", "green", "blue", "red"}
print(colors)  # duplicates removed automatically

Useful for checking membership and removing duplicates.

📊 Comparison Table

  • List: Ordered, mutable, allows duplicates
  • Tuple: Ordered, immutable, allows duplicates
  • Dictionary: Key-value pairs, mutable
  • Set: Unordered, unique elements only

⚠️ Common Mistakes

  • ❌ Trying to modify a tuple → TypeError
  • ❌ Accessing dictionary keys that don’t exist without using .get()
  • ❌ Using list methods on sets (e.g., append() on a set)

🧪 Practice Challenge

Create a Python file named data_collections.py that:

  • Creates a list of favorite books
  • Creates a tuple of screen resolutions
  • Stores user info in a dictionary
  • Creates a set of unique programming languages

📥 Tools You'll Need

📌 Final Words

Now you know how to use Python’s most important collection types to organize and work with data effectively. Up next, we’ll dive into functions, where we learn how to write reusable blocks of code.

🔗 Coming Next:

Lesson 10: Functions in Python – Reusable Code the Smart Way 🔁

Post a Comment