Python Tutorial

Python HOME Python Intro Python Get Started Python Syntax Python Comments Python Variables Python Data Types Python Numbers Python Casting Python Strings Python Booleans Python Operators Python Lists Python Tuples Python Sets Python Dictionaries Python If...Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Scope Python Modules Python Dates Python Math Python JSON Python RegEx Python PIP Python Try...Except Python User Input Python String Formatting

File Handling

Python File Handling Python Read Files Python Write/Create Files Python Delete Files

Python NumPy

NumPy Intro NumPy Getting Started NumPy Creating Arrays NumPy Array Indexing NumPy Array Slicing NumPy Data Types NumPy Copy vs View NumPy Array Shape NumPy Array Reshape NumPy Array Iterating NumPy Array Join NumPy Array Split NumPy Array Search NumPy Array Sort NumPy Array Filter NumPy Random NumPy ufunc

Machine Learning

Getting Started Mean Median Mode Standard Deviation Percentile Data Distribution Normal Data Distribution Scatter Plot Linear Regression Polynomial Regression Multiple Regression Scale Train/Test Decision Tree

Python MySQL

MySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL Join

Python MongoDB

MongoDB Get Started MongoDB Create Database MongoDB Create Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit

Python Reference

Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary

Module Reference

Random Module Requests Module Math Module cMath Module

Python How To

Remove List Duplicates Reverse a String Add Two Numbers

Python Examples

Python Examples Python Compiler Python Exercises Python Quiz Python Certificate

Python Tuples


A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.


Create a Tuple:

thistuple = ("apple", "banana", "cherry")
Try it Yourself »

Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets:


Print the second item in the tuple:

thistuple = ("apple", "banana", "cherry")
Try it Yourself »

Negative Indexing

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.


Print the last item of the tuple:

thistuple = ("apple", "banana", "cherry")
Try it Yourself »

Range of Indexes

You can specify a range of indexes by specifying where to start and where to end the range.

When specifying a range, the return value will be a new tuple with the specified items.


Return the third, fourth, and fifth item:

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
Try it Yourself »

Note: The search will start at index 2 (included) and end at index 5 (not included).

Remember that the first item has index 0.

Range of Negative Indexes

Specify negative indexes if you want to start the search from the end of the tuple:


This example returns the items from index -4 (included) to index -1 (excluded)

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
Try it Yourself »

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.


Convert the tuple into a list to be able to change it:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

Try it Yourself »

Loop Through a Tuple

You can loop through the tuple items by using a for loop.


Iterate through the items and print the values:

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
Try it Yourself »

You will learn more about for loops in our Python For Loops Chapter.

Check if Item Exists

To determine if a specified item is present in a tuple use the in keyword:


Check if "apple" is present in the tuple:

thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")
Try it Yourself »

Tuple Length

To determine how many items a tuple has, use the len() method:


Print the number of items in the tuple:

thistuple = ("apple", "banana", "cherry")
Try it Yourself »

Add Items

Once a tuple is created, you cannot add items to it. Tuples are unchangeable.


You cannot add items to a tuple:

thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
Try it Yourself »

Create Tuple With One Item

To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.


One item tuple, remember the commma:

thistuple = ("apple",)

#NOT a tuple
thistuple = ("apple")
Try it Yourself »

Remove Items

Note: You cannot remove items in a tuple.

Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:


The del keyword can delete the tuple completely:

thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
Try it Yourself »

Join Two Tuples

To join two or more tuples you can use the + operator:


Join two tuples:

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
Try it Yourself »

The tuple() Constructor

It is also possible to use the tuple() constructor to make a tuple.


Using the tuple() method to make a tuple:

thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
Try it Yourself »

Tuple Methods

Python has two built-in methods that you can use on tuples.

Method Description
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found

Test Yourself With Exercises


Print the first item in the fruits tuple.

fruits = ("apple", 
"cherry") print()

Start the Exercise