runtric-logo
Python Basic 2 - DataTuples & Immutability (2/5)
Chapter 2
Python Basic 2 - Data

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.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Access items by index just like a list:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Tuple vs List → Mutability showdown

Lists use square brackets and can change:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Tuples lock their content:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Why does immutability matter?

  1. Safety – functions can’t secretly edit your data.
  2. Hashability – immutable objects work as dictionary keys or set items.
  3. 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 minutes

Choose 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

Interactive Code Editor
📝 Your Task:

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.

python

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.

Lesson Complete!
100%
Progress
What You've Learned:
  • 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.

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.