Lesson 7 โ Functions and Scope in Python
๐ Introduction

In Python, lists, tuples, and sets are three important ways to store collections of items. Each has unique features and is used in different scenarios.
Understanding these structures is crucial for managing data effectively.
๐น 1. Lists in Python
- A list is an ordered collection that can hold multiple items.
- Lists are mutable (you can change them after creation).
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits) # ['apple', 'banana', 'cherry']
# Adding an item
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
# Changing an item
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 'cherry', 'orange']
๐น 2. Tuples in Python
- A tuple is an ordered collection like a list, but it is immutable (cannot be changed after creation).
- Use tuples when you want data to stay constant.
Example:
colors = ("red", "green", "blue")
print(colors) # ('red', 'green', 'blue')
# Trying to change a value will cause an error
# colors[0] = "yellow" โ Not allowed
๐น 3. Sets in Python
- A set is an unordered collection of unique items.
- Sets automatically remove duplicates.
Example:
numbers = {1, 2, 3, 3, 4, 5}
print(numbers) # {1, 2, 3, 4, 5}
# Adding items
numbers.add(6)
print(numbers) # {1, 2, 3, 4, 5, 6}
# Removing items
numbers.remove(3)
print(numbers) # {1, 2, 4, 5, 6}
๐น 4. Key Differences
Feature | List | Tuple | Set |
---|---|---|---|
Ordered | โ Yes | โ Yes | โ No |
Mutable | โ Yes | โ No | โ Yes |
Duplicates | โ Allowed | โ Allowed | โ Not Allowed |
Use Case | General collections | Fixed data | Unique items collection |
โ Summary
- Lists are flexible, ordered, and changeable.
- Tuples are ordered but fixed (immutable).
- Sets are unordered and unique.