A tuple is a collection of ordered, immutable elements that cannot be altered. They’re used for storing lists of values, which makes them useful for representing arrays of data. In this tutorial, we’ll learn how to use Tuples in Python and their operators and built-in functions with examples.
The tuple is one of the four built-in data types in Python that are used to store groups of data. The other three are List, Set, and Dictionary, and each has its own features and uses.
Table of contents
In order for you to test your Python code provided in this lesson, you must test the code on your code editor like PyCharm. But if you wish to run this code online, we also have an Online Compiler in Python for you to test your Python code for free.
And If you want to learn more about Python or are just starting out, take a look at our Python Tutorial for Beginners.
What is Tuples in Python?
A tuple is an ordered list of values. It is similar to a list except that each element has its own unique identifier. This means that you cannot add or remove elements from a tuple without changing the order of the remaining elements.
In Python, tuples are immutable sequences of objects. You can use them as variables, pass them to functions, return them from functions, and even assign them to other variables.
Creating tuples is as easy as placing values separated by commas between parentheses. For instance,
tuple1 = ("Prince", "Grace", 1999, 1998)
tuple2 = (1, 2, 3, 4, 5)
tuple3 = ('A1', 'B2', 'c3', 'D4')
Note: If you’re creating a tuple with a single element, be sure to put a comma after the element.
Also, tuples with one value must include a trailing comma.
Similar to how the indices of Python strings start at 0, the indices of tuples can be sliced, concatenated, etc.
Before you continue to our tutorial make sure you have downloaded and installed Python on your computer.
Accessing Tuples in Python Values
To access values in a tuple, use the square brackets for slicing in conjunction with the index or indices to acquire the value at that index. When it comes to accessing tuples, the index operator comes in very helpful.
Example:
tuple1 = ('Prince', 'Grace', 1999, 1998)
tuple2 = (1, 2, 3, 4, 5)
print (tuple1[3])
print (tuple2[1:4])
Output:
1998
(2, 3, 4)
Updating Tuples
Tuples are immutable objects. Updating Tuples means that once you’ve defined them, you cannot modify any of their properties. You can only add new elements to them.
Example:
tuple1 = ('Prince', 'Grace', 1999, 1998)
tuple2 = (1, 2, 3, 4, 5)
# The following operation is invalid for tuples.
# tuple1[0] = 'King'
# What we can do is to combine the two tuples by creating a new tuple
tuple3 = tuple1 + tuple2
print (tuple3)
Output:
('Prince', 'Grace', 1999, 1998, 1, 2, 3, 4, 5)
Delete Tuple Elements
To delete tuple elements is not possible in Python, you can only use the del operator to delete the variable. For instance,
Example:
tuple1 = ['Prince', 'Grace', 1999, 1998];
print (tuple1)
del (tuple1)
print ('The output below will prompt an error')
print (tuple1)
Output:
['Prince', 'Grace', 1999, 1998] The output below will prompt an error
Traceback (most recent call last): NameError: name 'tuple1' is not defined. Did you mean: 'tuple'?
The exception was thrown because, after deleting the tuple, the variable tuple1 no longer exists.
Indexing, Slicing, and Matrixes
Due to the fact that tuples are sequences, Indexing, Slicing, and Matrixes operate identically for tuples and strings. Considering the following input:
Example:
tuple1 = ('code', 'Code', 'CODE!')
# Offsets start at zero
print( tuple1[2] )
# Negative: count from the right
print( tuple1[-2] )
# Slicing fetches sections
print( tuple1[1:] )
Output:
CODE!
Code
('Code', 'CODE!')
Python is different from other languages because it lets you use positive index and negative indexing. From the right, you can count the negative indices. Below is an illustration of how the slicing works in Tuples.
No Enclosing Delimiters
In Python, there are no enclosing delimiters needed when accessing an element of a tuple. Any comma-separated list of multiple objects written without identifying symbols, such as brackets for lists, parentheses for tuples, etc., will be treated as a tuple, as shown in these short examples:
Example:
print ('Prince', 3.14, 55+6.6j, 'ITSC')
a, b = 'Hello', 'Python'
print ("Values of a and b:", a,b)
Output:
Prince 3.14 (55+6.6j) ITSC
Values of a and b: Hello Python
Basic Tuple Operators
There are several basic tuple operators that can be performed in Python. These include adding new elements, removing existing elements, and changing the order of the elements.
Example:
print(len(('a', 'b', 'c')))
# result is 3
print((1, 2, 3) + (4, 5, 6))
# result is (1, 2, 3, 4, 5, 6)
print(('Hey!') * 4)
# result is Hey!Hey!Hey!Hey!
print(3 in (1, 2, 3))
# result is True
for x in (1, 2, 3): print (x)
# result is 1 2 3
Output:
3
(1, 2, 3, 4, 5, 6)
Hey!Hey!Hey!Hey!
True
1
2
3
Tuple Built-in Functions
There are several built-in functions that operate on tuples. These functions are used to extract values from tuples. They’re also used to check whether a tuple contains certain values.
Function | Description |
---|---|
cmp(tuple1, tuple2) | Elements from both tuples are compared. |
len(tuple) | This function returns the overall length of the tuple. |
max(tuple) | Returns the item from the tuples with the maximum value. |
min(tuple) | Returns the item from the tuples with the minimum value. |
list(seq) | A list is converted into a tuple. |
Example:
tuple1 = ('Prince', 'Grace', 'Mario', 'Ludy', 'Mc Quinn')
# cmp is not support in Python 3 but it is still workiin in Python 2
# len
print (len(tuple1))
# max
print (max(tuple1))
# min
print (min(tuple1))
list1 = ['Prince', 'Grace', 'Mario', 'Ludy', 'Mc Quinn']
# tuple
print (tuple(list1))
Output:
5
Prince
Grace
('Prince', 'Grace', 'Mario', 'Ludy', 'Mc Quinn')
Summary
In summary, you have learned about tuples in Python. This tutorial has covered the following topics: accessing, updating, deleting of tuple elements, the basic operators and built-in functions of tuples.
I hope this lesson has helped you learn what is tuples in Python and how to use them. Check out our list of Python Tutorial Topics if you missed any of our previous lessons.
In the next post, “Python Sets“, you’ll learn about what is set in Python, the built-in functions, methods, and how to use them.
Python Lists
Python Sets