Lesson 26 of 60
โฎPrev Nextโฏ

PY-101 ยท Collections

Tuples

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

26of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A tuple is an ordered group of values in round brackets, such as (12.97, 77.59), that cannot be changed after it is made. Use it for things that belong together and should stay fixed, like coordinates or an RGB colour. You read it like a list, but you cannot append, remove or reassign items.

    • Create a tuple
    • Read items like a list
    • Know when fixed is better

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Tuples, running
    1. office = (12.97, 77.59)
    2. print("Latitude:", office[0])
    3. lat, lng = office
    4. print(lat, lng)
    5. try:
    6. office[0] = 0
    7. except TypeError as e:
    8. print("Cannot change:", e)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA delivery app stores each shop's location as latitude and longitude. If some code accidentally swapped one number, the rider would go to the wrong place. A tuple makes that impossible.
    2. The Python ideat = (a, b, c)A tuple is an ordered group of values in round brackets, such as (12.97, 77.59), that cannot be changed after it is made.
    3. In real lifeDatabase libraries return each row as a tuple, so the row you read stays exactly as it came from the database.

    Start with a question

    A delivery app stores each shop's location as latitude and longitude. If some code accidentally swapped one number, the rider would go to the wrong place. A tuple makes that impossible.

    Fixed data is safer data. Functions also use tuples to return several values at once.

    Explain it like I'm 10

    A tuple is a sealed lunchbox: you can see what is inside and take a look at each item, but you cannot swap anything once it is packed.

    The technical explanation

    point = (3, 4) makes a tuple. point[0] reads 3. point[0] = 9 raises TypeError.

    Tuples can be dictionary keys and set members because they never change; lists cannot.

    return name, age in a function returns a tuple, which you can unpack: n, a = get_user().

    Syntax

    t = (a, b, c)
    x, y = (3, 4)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Database libraries return each row as a tuple, so the row you read stays exactly as it came from the database.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Can you change an item in a tuple?
    2. 2. Which makes a one-item tuple?
    3. 3. t = (1, 2, 3). What is t[1]?
    4. 4. What does return a, b give the caller?
    5. 5. Which can be a dictionary key?

    Questions people ask

    When should I use a tuple instead of a list?

    Use a tuple for a fixed group whose size and meaning do not change, like (x, y) or (red, green, blue). Use a list for a growing collection of similar items.

    Are tuples faster than lists?

    Slightly, and they use a little less memory, but choose them for meaning and safety, not speed.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner When should I use a tuple instead of a list?

    Use a tuple for a fixed group whose size and meaning do not change, like (x, y) or (red, green, blue). Use a list for a growing collection of similar items.

    beginner Are tuples faster than lists?

    Slightly, and they use a little less memory, but choose them for meaning and safety, not speed.

    intermediate Can you change an item in a tuple?

    No. Tuples are immutable.

    intermediate Which makes a one-item tuple?

    (5,). The comma makes it a tuple.

    Quick revision

    Remember

    • Create a tuple
    • Read items like a list
    • Know when fixed is better
    • A tuple with one item needs a comma: (5,). Without it, (5) is just the number 5 in brackets.

    Syntax

    t = (a, b, c)
    x, y = (3, 4)

    Official documentation