Tuples in Python

A smiling blacksmith hammers words into metal blocks on an anvil, labelled “cat,” “7,” and “apple,” symbolising immutable Python tuples.

Tuples are another way of storing collections of items in Python, much like lists. The big difference is that tuples cannot be changed once you’ve created them. That makes them immutable — a fancy way of saying “set in stone”. Tuples are handy when you want to group pieces of information together and make sure they stay the same throughout your programme.

Creating Tuples

Tuples are made using round brackets (). You can put numbers, words, or even a mix of different types inside.

If you only need a tuple with one item, remember to include a comma, otherwise Python just sees it as a normal value:

Things to try:

  • Create a tuple with three of your favourite animals.
  • Make a tuple with one item and print it.
  • Try mixing numbers and strings in the same tuple.

Why Tuples Are Useful

Because tuples are immutable, they’re perfect for storing data that shouldn’t change. For example, the x and y coordinates of a location, or the days of the week.

You can read items from a tuple using their index, just like with lists:

Things to try:

  • Make a tuple of four numbers and print the last one using its index.
  • Create a tuple of months in a season (e.g., winter months).

Tuples Are Immutable

The important thing to remember is that you cannot change a tuple once it’s created.

This means you can’t add, remove, or replace items in a tuple directly.

Things to try:

  • Try to change an item in a tuple and see what error Python gives you.
  • Attempt to append something to a tuple and check the result.

Tuples in Lists

While tuples themselves can’t be changed, you can store them inside a list. This gives you the best of both worlds: the list can be modified, but the individual tuples inside remain fixed.

Here, each student’s details are kept safe in a tuple, but you can still add or remove students from the list.

Things to try:

  • Create a list of two tuples, each with a fruit and its price. Add a third fruit and price.
  • Remove one of the tuples from your list.

Sorting Tuples in Lists

Although you can’t sort a tuple itself, you can sort a list of tuples. Python will look at the first item in each tuple when deciding the order, and if those are the same, it will check the second.

This sorts by name because the name comes first in each tuple. You can also sort by age by telling Python to look at the second item:

Things to try:

  • Create a list of three tuples, each containing a person’s name and age. Sort them alphabetically.
  • Sort the same list by age instead of by name.

Wrapping Up

Tuples are like lists that can’t be changed. They’re brilliant for storing fixed data, and when combined with lists, they become very powerful. You’ll often use them when you want information grouped neatly together and kept safe from accidental changes.

Main Topic

Introducing Python Lists

A cartoon frog smiles while writing “Python Lists” with items flies, worms, insects on a chalkboard in a classroom setting.

This is a Python series introducing lists, their importance, and everyday uses. Light-hearted explanations make coding concepts simple, clear, and fun.

Other Tutorials in this Topic

A person’s hands holding pen and eraser above a notepad on a desk, crossing out “bread” from a shopping list.

Beginning Python Lists

A practical Python tutorial introducing lists, showing how to access, add, modify, and remove items with clear…

A cheerful goose sorts colourful books by size on a shelf, with bold text “Sorting Python Lists” against a pale magenta background.

Sorting Python Lists

A practical Python tutorial explaining how to sort lists in ascending and descending order, comparing `sort()` with…

A person in an orange shirt works along a conveyor belt with boxes, apple, and envelope under text “Iterating Lists” on pale magenta.

Iterating Lists in python

A clear Python tutorial explaining how to loop through lists with for, while, and enumerate, introducing iterators…

A cheerful cartoon monkey sorts bananas from a mixed fruit box into another box on a conveyor belt, with “Filtering Lists” text.

Maps and Filters in python

This tutorial explains how to use Python’s `map()` and `filter()` functions to transform and select list items,…