Python Tuple [Easy Examples]

Filed Under: Python

Today we’re talking about another built-in data type in Python – Tuples. A tuple in Python is used to store multiple elements in an ordered and unchangeable (immutable) manner.

If you need the data to be changeable, a Python list is what you need. Now, without any further delay, let’s get right into our Tuple datatype.

By the end of this tutorial, you’ll know:

  • What is a Python tuple?
  • How to define tuples?
  • Accessing tuple elements
  • Different functions for tuples

Python Tuple – Syntax and Examples

Tuples are defined with simple brackets. Here’s a sample definition of a tuple:

SampleTuple = ("Element1", "Element2", "Element3")

Here are some more examples of tuple definitions:

#an empty tuple

#tuple of integers

Lastly, you don’t have to stick to a single data type when adding elements to tuples:

#Tuples with multiple datatypes
multiTuple = ("This is a string", 1, True)

If you run the above code, you’ll have no issues getting the output tuple:

('This is a string', 1, True)

Accessing a Value in Python Tuple

You can access tuples with indexes. The index of the first element is 0 and the last element has an index of n-1.

#tuple of string

#accessing first element

#accessing second element

#accessing fourth element

The output of the above code will be like below-

python tuple example, accessing a value in python tuple example

Tuples also support negative indexing where the last element of a tuple is indexed -1

#tuple of string

#accessing first element from the right

#accessing second element from the right

#accessing second element from the right

The output will be like below.

python tutorial, tuple accessing a value with negative index example output

Update and Delete Elements in Tuples

Since tuples are immutable objects, the defined elements cannot be changed or updated. But you can combine two tuples into a third one when required.

Similarly, deleting individual elements is not possible, but with the del keyword, you can remove the tuple from your program.

#tuple 1

#tuple 2

#tuple 3


#to delete tuple 1
del tup1
#this will show a traceback as tup1 is deleted. So it is not defined now

The above code will give you the output as show in the screenshot here:

python tuple tutorial, tuple update and delete example output

Some of the built-in functions for Tuples in Python

Tuples offer a variety of built-in functions which reduce the lines of code required. Let’s look at some of the functionality offered by tuples.

#a string tuple

#len(tuple) gives total length of a tuple

#max(tuple) gives maximum element of a tuple

#min(tuple) gives minimum element of a tuple

#count(x) gives number of occurances of x in the tuple

#index(x) gives index of first occurances of x in the tuple

You should get the following output if you run the above code.

python tuple, some built-in tuple functions example


And that brings us to the end of this tutorial on the Python tuple. Go over each piece of code thoroughly to get an understanding of the functions.

You can also refer to the Python tutorial if you want a roadmap to learning all the functions on your coding journey. #happy_coding 馃檪



  1. Jay says:

    Great and very clear explanations! Thank you!

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors