Python Programming Language Tuples Tutorial

Jesse L
4 min readAug 17, 2021

Hi everyone, welcome back. In this tutorial, I will show how to perform basic tasks involving tuples in Python. A tuple is a data structure in the Python language and it is capable of storing several items in a singular tuple variable. Tuples are a built in data type in Python. We will go over tuple related functions and how tuples can be manipulated in this tutorial.

Tuples

We can create an empty tuple by using parenthesis:

tuple = ()

We can create a tuple and populate it by adding values within the parenthesis:

tuple = ("item1", "item2", "item3")

As you can see, I added three string items into my tuple and each item in my tuple is separated by a comma. We can print our tuple to see what it looks like:

tuple = ("item1", "item2", "item3")
print(tuple)
Output
('item1', 'item2', 'item3')

Python has 4 different basic built in data structures. They are lists, tuples, dictionaries, and sets. Each one has different uses for different situations. Tuples are immutable, ordered, and allows duplicate values.

How do you get values from within a tuple? A tuple uses indexing which allows us to refer to items by index number. Take note that the first value is index 0, not 1. Let’s see an example:

tuple = ("item1", "item2", "item3")
print(tuple[0])
Output
item1

Not only can we get one value at a time, but we can also get a range of values:

tuple = ("item1", "item2", "item3", "item4", "item5")
print(tuple[1:3])
Output
('item2', 'item3')

In the example above, we are telling the tuple to print values from index 1 to index 3, but notice that it does not include index 3(“item4”). By giving it a range, it returns a new tuple with the items included in the range.

Python supports the use of negative indexing. By giving it an index of -1, the tuple will refer to the last value. An index of -2 will refer to the second to last item. For example:

tuple = ("item1", "item2", "item3", "item4", "item5")
print(tuple[-1])

--

--

Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.