python tutorial

Python Tuple

Learn more about Python, one of the world’s most versatile and popular programming languages.

In this tutorial, we’ll take a closer look at Python tuples, including what a Python tuple is, how to create them (including zero or one item), and how to access items from a tuple.


What is a Python Tuple?

A Python tuple is a collection of items or values. Some key differences between a Python tuple and Python List are:

  • Python tuple are created by adding comma separated values inside parentheses ( )
  • Python tuple are immutable wherein once values are added to tuple, they can’t be changed

Let’s dive in and learn more about how to use a Python tuple.


Creating a Python Tuple

Python tuple can be created by specifying comma separated values inside of parentheses ( ). Values inside of a tuple cannot be modified once created. Let’s create a tuple of the first 5 odd numbers and then try to change one of them to be a number that is not odd. As you can see below, changing values inside of a tuple throws an error as tuples are immutable.

Code Example
# create a tuple of first five odd numbers
odd_numbers = (1, 3, 5, 7, 9)

print(odd_numbers)
(1, 3, 5, 7, 9)

odd_numbers[1] = 2
Example Output
TypeError           Traceback (most recent call last)
<ipython-input-2-c99900bfc26b> in <module>
-> 1 odd_numbers[1] = 2
TypeError: ‘tuple' object does not support item assignment

Python tuples follow the idea of packing and unpacking values i.e. while creating a tuple parentheses ( ) are optional and just providing a comma-separated value will create a tuple as well (also known as packing). Similarly, when trying to access each value in the tuple, the values can be assigned to individual variables (also called unpacking).

Let’s create a tuple called student with name, age, course, phone number using PACKING and then UNPACK those values from the student tuple into individual variables.

Code Example
# create a tuple called student while PACKING name, age, course and 
# phone number
# note: as you can see parantheses are optional while packing values 
# into tuple

student = 'John Doe', 27, 'Python v3', 1234567890

print (student)
('John Doe', 27, 'Python v3', 1234567890)

# let's unpack the values from student tuple into individual variables
name, age, course, phone = student

print (name)
print (age)
print (course)
print (phone)

John Doe
27
Python v3
1234567890


Creating a Python Tuple With Zero or One Item

Creating a python tuple with either zero or one element is a bit tricky. Let’s take a look.

  • Python tuple with zero item can be created by using empty parentheses ()
  • Python tuple with exactly one item can be created by ending the tuple item with a comma ,
Code Example
# create a tuple with zero elements

empty_tuple = ()

print(empty_tuple)

()

# create a tuple with exactly one element
# note: pay attention to how the value ends with a comma ,
singleton_v1 = ('first value',)

print(singleton _v1)

singleton_v2 = 'first value’,

print(singleton_v2)

('first value',)
(‘first value’,)

Accessing Items From the Tuple

Items from the tuple can be accessed just like a list using

  • Indexing – returns the item
  • Negative Indexing – returns the item
  • Slicing – returns a tuple with the items in it

Let’s access course from student tuple using above mentioned techniques

Code Example
# create a tuple called student while PACKING name, age, course and 
# phone number
# note: as you can see parantheses are optional while packing values 
# into tuple

student = 'John Doe', 27, 'Python v3', 1234567890

print(student)

('John Doe', 27, 'Python v3', 1234567890)

# let's unpack the values from student tuple into individual variables
name, age, course, phone = student

# get course from student tuple using indexing
print(student[-2])

Python v3

# get course from student tuple using negative indexing, -1 is the last element
print(student [-2))

Python v3

# get course from student tuple using slicing
print(student [2:3])
('Python v3’,)

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required