Python Lesson 12: Tuples

🐍 Python CourseLesson 12 of 26 · 46% complete

A tuple is like a list — but immutable (cannot be changed). Once created, the values are locked. Use tuples for data that should never change.

Creating Tuples

# With parentheses
coordinates = (10.5, 20.3)
colors = ("red", "green", "blue")
person = ("Alice", 30, "London")

# Single-item tuple — comma is required!
single = (42,)     # this IS a tuple
not_tuple = (42)   # this is just int 42

# Without parentheses also works
point = 3, 4
print(type(point))  # tuple

Accessing Tuple Items

person = ("Alice", 30, "London")

print(person[0])    # Alice
print(person[-1])   # London
print(person[1:])   # (30, "London") — slicing works
print(len(person))  # 3

Why Use Tuples?

# 1. Data that must not change
SCREEN_SIZE = (1920, 1080)
PARIS = (48.8566, 2.3522)  # GPS coordinates

# 2. Multiple return values from a function
def get_min_max(numbers):
    return min(numbers), max(numbers)  # returns tuple!

low, high = get_min_max([3, 7, 1, 9, 4])
print(f"Min: {low}, Max: {high}")  # Min: 1, Max: 9

# 3. Unpacking
name, age, city = ("Bob", 25, "NYC")
print(name, age, city)

🏋️ Practice Task

Create a tuple of 3 (name, age, skill) tuples for your dream coding team. Loop through and print each person: “Name: X | Age: Y | Skill: Z”

💡 Hint: team = ((“Alice”,28,”Python”), (“Bob”,25,”React”), …). Use: for person in team: name,age,skill = person

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *