Home » Programming » Python » Python Tuple

What is a Tuple in the Python Programming Language?

Tuple is a type of variable in Python – a data structure containing multiple parts. It’s a collection (or array) of data – but a specific kind of collection.

It’s Ordered (Indexed)

Elements in a tuple are stored in order – stored with an index or position – their position in the collection is fixed, and they can be found by their position.

It’s Unchangeable (Or Immutable)

Once a tuple is created, elements in it cannot be moved, removed, added, or altered.

Duplicates are Allowed

Duplicate values are allowed as tuples are indexed, so multiple elements with the same value can be stored at different indexes without conflict.

It’s Used for Storing an Unchanging Collection of Data

Tuples are defined using a comma-separated list of values in brackets.

Here’s an example of a tuple in use:

zoo = ("ape", "parrot", "giraffe","penguin", "penguin")

The variable zoo is assigned the value of a tuple containing the zoo animals – there are two penguins, as duplicates are allowed.

Other Collection Types

Python has several built-in variable types for handling collections to cover different scenarios:

  • Tuple (you’re reading about it here!)
  • List
  • Set
  • Dictionary

…which one you will use for any given variable will depend on what kind of variables the collection will hold, whether it will be changing and what you’ll be using it for.

Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment