A Beginner's Guide to Python Tuples

By Hemanta Sundaray on 2021-08-18

Like strings and lists, tuple is a sequence structure in Python.

A tuple is immutable. This simply means that we can’t insert or delete elements from a tuple.

Syntax

We can create a tuple with one or more elements simply by separating the elements with commas.

Let’s create a tuple with one element:

name = 'hemanta',

The comma is very important. Without the comma, name is not a tuple.

The tuple syntax that you will frequently come across is where the elements are enclosed by parentheses.

name = ('hemanta', 'kumar', 'sundaray')

Tuple unpacking

We can unpack the elements of a tuple and store them in different variables as shown below:

firstName, lastName = ('hemanta', 'sundaray')

print(firstName)
# hemanta

print(lastName)
# sundaray

tuple() function

We can also create tuples from other sequence structures, for example lists, using the tuple() function.

frameworks = ['React', 'Vue', 'Angular']

frameworks_tuple = tuple(frameworks)

print(frameworks_tuple)
# ('React', 'Vue', 'Angular')

Tuple concatenation

We can combine tuples using the + operator.

name = ('hemanta',) + ('kumar', 'sundaray')

print(name)
# ('hemanta', 'kumar', 'sundaray')

Tuple iteration

We can iterate through the elements of a tuple using the for….in loop.

languages = ('JavaScript', 'Python', 'Java')

for lang in languages:
	print(lang)

# JavaScript
# Python
# Java

Join the Newsletter