Lesson 18 of 34

Tuples: Lists that Don't Change

7 min 20 XP

A tuple is like a list written with ( ), but it cannot be changed after it's created. Great for data that shouldn't change, like a date of birth or a GPS location.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Unpacking: give a tuple's values to several variables in one line. It also makes swapping two variables easy.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

A one-item tuple needs a comma: (5,). Plain (5) is just the number 5.

Challenge

+20 XP

point = (3, 4). Unpack it into x and y. Print x and y on the first line (separated by a space), then x*x + y*y on the second line.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.