Tuples & Immutability
6 minutes read
Beginner friendly
Includes practice
Why learn this? Tuples give you a safe “don’t-touch” container. They protect data from accidental edits and let you use them as dictionary keys—handy in many real-world programs.
Learning goals
• Understand what a tuple is and how to create one
• See how immutability differs from a list’s mutability
• Practice the “change-by-rebuilding” pattern with tuples
Runtic's Learning Tips
Mistakes are signals, not stop signs—each error message helps you grow.
Tackle one small idea at a time; tiny wins stack into big skills.
Every expert once typed their very first
()—keep pressing those keys!
What exactly is a tuple?
A tuple is an ordered collection written with parentheses.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Access items by index just like a list:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Tuple vs List → Mutability showdown
Lists use square brackets and can change:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Tuples lock their content:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Why does immutability matter?
- Safety – functions can’t secretly edit your data.
- Hashability – immutable objects work as dictionary keys or set items.
- Performance hints – Python may store immutables more efficiently.
Quick Summary
Tuples use
( ), lists use[ ].Tuples can’t be altered once created—no item reassignment or
.append().To “change” a tuple, build a new one from pieces of the old.
Immutability locked in—time for a quick knowledge check!
Quick Practice
3 minutesChoose the best answer for each question.
Which brackets define a tuple?
Can you call .append() on a tuple?
Which literal is a tuple?
Great job! Let’s practice “modifying” by rebuilding.
Hands-on: extend a tuple by creating a new one
Practice Challenge
Create a tuple named extended_fruitscontaining "apple","banana",and "orange".Then, make a string variable extended_fruits_keyby joining all the items in the tuple with the "|"symbol. Finally, print both variables to verify your result.
Create a tuple of fruits and join them into a string using "|".join().
Recap & next steps
Tuples shine when you need fixed, trustworthy data like coordinates, dates, or constant lookup tables. For flexible collections, stick with lists.
- Tuples use parentheses and stay immutable after creation.
- Direct edits (reassign, append) raise errors—rebuild a new tuple instead.
- Tuples’ immutability makes them safe, hashable, and often more efficient.
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.