loop
Tuples and dictionaries

Module 4 · Tuples and dictionaries · Lesson 1 of 6

Sequence types and mutability

Lists, tuples, strings — many of Python's types are sequences: ordered collections you can index and loop over. But sequences split along one crucial line — whether they can be changed after they're created. That property is called mutability.

Can it change after it's made?
🔓 list — mutable
102030
nums = [10, 20, 30]
🔒 tuple — immutable
102030
t = (10, 20, 30)
TypeError: 'tuple' object does not support item assignment

A mutable value, like a list, can be edited in place — swap an element, add one, remove one. An immutable value, like a tuple or a string, is frozen the moment it's made: you can read it all you like, but any attempt to change it is a TypeError.

Run it yourselfruns in your browser · Python
main.py

Why would you ever want something you can't change? Because immutability is a guarantee. It keeps data safe from accidental edits, and — as you'll see — it's what lets tuples do a few things lists simply can't. Let's meet tuples properly.