Python Tuples
Python Tuples
Python Tuples
The elements of a list can be changed after assignment, whereas this is not the case with Python Tuples.
Another difference is, Tuple use parenthesis, whereas Lists in Python use Square Brackets. In other
words, different comma-separated values between square brackets in a list, whereas, different comma-
separated values between parenthesis is Tuple.
https://studyopedia.com/python3/python-tuples/ 3/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
#Creating Tuples in Python
mytuple = ("Amit", "Craig", "Ronaldo", "Messi")
print(mytuple)
mytuple = (10, 20, 50, 100)
print(mytuple)
mytuple = (5.4, 8.9, 15.2, 20.5)
print(mytuple)
mytuple = ("Ronaldo", 20, 5.8)
print(mytuple)
#creating Tuple without parentheses
mytuple = "A", "B"
print(mytuple)
https://studyopedia.com/python3/python-tuples/ 4/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
('Amit', 'Craig', 'Ronaldo', 'Messi')
(10, 20, 50, 100)
(5.4, 8.9, 15.2, 20.5)
('Ronaldo', 20, 5.8)
('A', 'B')
#Creating Tuple in Python
mytuple = (10, 20, 50, 100)
print(mytuple)
#Creating empty Tuple in Python
mytuple = ()
print(mytuple)
(10, 20, 50, 100)
()
https://studyopedia.com/python3/python-tuples/ 5/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
#First item
mytuple[0]
#Second item
mytuple[1]
#tuple
mytuple = ("ronaldo", "messi", "neymar", "maradona")
print("Tuple = ",mytuple)
#First Item
print("First Item = ",mytuple[0]);
#Second Item
print("Second Item = ",mytuple[1]);
#Third Item
print("Third Item = ",mytuple[2]);
Tuple = ('ronaldo', 'messi', 'neymar', 'maradona')
First Item = ronaldo
Second Item = messi
Third Item = neymar
#tuple
mytuple = ("eddie", "anthony", "aran", "blake", "alyson", "rechelle")
print("Tuple = ",mytuple)
https://studyopedia.com/python3/python-tuples/ 6/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
print( Tuple ,mytuple)
#range of negative indexes
print(mytuple[-3:-1])
print(mytuple[-5:-2])
#range of indexes
print(mytuple[1:5])
print(mytuple[3:5])
if "anthony" in mytuple:
print("Found!")
Tuple = ('eddie', 'anthony', 'aran', 'blake', 'alyson', 'rechelle')
('blake', 'alyson')
('anthony', 'aran', 'blake')
('anthony', 'aran', 'blake', 'alyson')
('blake', 'alyson')
Found!
Fetch a specific element at 1st position: mytuple[0]
Fetch a specific element at 2nd position: mytuple[1]
https://studyopedia.com/python3/python-tuples/ 7/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
Fetch a specific element at 2nd position: mytuple[1]
Fetch a specific element at 3rd position: mytuple[2]
Fetch a specific element at 4th position: mytuple[3]
#tuple
mytuple = ("ronaldo", "messi", "neymar", "maradona")
print("Tuple = ",mytuple)
print("Tuple element at position 1 (index 0) = ",mytuple[0])
print("Tuple element at position 2 (index 1) = ",mytuple[1])
print("Tuple element at position 3 (index 2) = ",mytuple[2])
print("Tuple element at position 4 (index 3) = ",mytuple[3])
Tuple = ('ronaldo', 'messi', 'neymar', 'maradona')
Tuple element at position 1 (index 0) = ronaldo
Tuple element at position 2 (index 1) = messi
Tuple element at position 3 (index 2) = neymar
Tuple element at position 4 (index 3) = Maradona
#negative indexing in tuple
mytuple = ("ronaldo", "messi", "neymar", "maradona")
print("Tuple = ",mytuple)
print("Tuple element at last position = ",mytuple[-1])
print("Tuple element at 2nd last position = ",mytuple[-2])
print("Tuple element at 3rd last position = ",mytuple[-3])
print("Tuple element at 4th last position " mytuple[ 4])
https://studyopedia.com/python3/python-tuples/ 8/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
print( Tuple element at 4th last position = ,mytuple[-4])
Slicing in Tuple
The slice operator can also be used to fetch a specific set of sub-elements i.e. slicing. This is achieved
with the colon operator and allows to fetch multiple items. Let us see an example:
#tuple slicing
mytuple = ("ronaldo", "messi", "neymar", "maradona")
print("Tuple = ",mytuple)
print("Tuple element at last position = ",mytuple[-1])
print(mytuple[1:])
print(mytuple[::-1])
print(mytuple[1:2])
print(mytuple[1:4])
Tuple = ('ronaldo', 'messi', 'neymar', 'maradona')
Tuple element at last position = maradona
('messi', 'neymar', 'maradona')
('maradona', 'neymar', 'messi', 'ronaldo')
('messi',)
('messi', 'neymar', 'maradona')
Range of indexes
By specifying the start and end range, we can specify the range of indexes in Python. Let us see an
example:
#tuple
https://studyopedia.com/python3/python-tuples/ 9/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
#tuple
mytuple = ("eddie", "anthony", "aran", "blake", "alyson", "rechelle")
print("Tuple = ",mytuple)
#range
print(mytuple[1:2])
print(mytuple[1:4])
print(mytuple[1:5])
print(mytuple[3:5])
Tuple = ('eddie', 'anthony', 'aran', 'blake', 'alyson', 'rechelle')
('anthony',)
('anthony', 'aran', 'blake')
('anthony', 'aran', 'blake', 'alyson')
('blake', 'alyson')
In the above example, [1:2] means the search starts from index 1 (including) and ends at index 2
(excluding). In the same way, [1:4] means the search starts from 1 (including) and ends at index 4
(excluding).
print(mytuple[-3:-1])
#tuple
mytuple = ("eddie", "anthony", "aran", "blake", "alyson", "rechelle")
print("Tuple = ",mytuple)
https://studyopedia.com/python3/python-tuples/ 10/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
#range of negative indexes
print(mytuple[-3:-1])
print(mytuple[-5:-2])
#range of indexes
print(mytuple[1:5])
print(mytuple[3:5])
Tuple = ('eddie', 'anthony', 'aran', 'blake', 'alyson', 'rechelle')
('blake', 'alyson')
('anthony', 'aran', 'blake')
('anthony', 'aran', 'blake', 'alyson')
('blake', 'alyson')
#tuple
mytuple = ("ronaldo", "messi", "neymar", "maradona")
print("Tuple = ",mytuple)
#list
mylist = list(mytuple)
print("List (from Tuple) = ",mylist)
mylist[0] = "Zidane "
mylist[1] = "Beckham"
mylist[2] = "Rooney"
print("Updated List = ",mylist)
mytuple = tuple(mylist)
print("Updated Tuple = ",mytuple)
https://studyopedia.com/python3/python-tuples/ 11/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
Tuple = ('ronaldo', 'messi', 'neymar', 'maradona')
List (from Tuple) = ['ronaldo', 'messi', 'neymar', 'maradona']
Updated List = ['Zidane ', 'Beckham', 'Rooney', 'maradona']
Updated Tuple = ('Zidane ', 'Beckham', 'Rooney', 'maradona')
#tuple1
mytuple1 = (20, 40, 80, 150, 200);
print("Tuple1 = ",mytuple1)
print("Tuple1 Length = ",len(mytuple1))
#tuple2
mytuple2 = (300, 450, 500);
print("Tuple2 = ",mytuple2)
print("Tuple2 Length = ",len(mytuple2))
#tuple3
mytuple3 = (650, 800, 1000);
print("Tuple3 = ",mytuple3)
print("Tuple3 Length = ",len(mytuple3))
print("Joining two tuples = ",mytuple1 + mytuple2)
print("Joining three tuples = ",mytuple1 + mytuple2 + mytuple3)
Tuple1 = (20, 40, 80, 150, 200)
Tuple1 Length = 5
Tuple2 = (300, 450, 500)
Tuple2 Length = 3
Tuple3 = (650, 800, 1000)
https://studyopedia.com/python3/python-tuples/ 12/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
p ( , , )
Tuple3 Length = 3
Joining two tuples = (20, 40, 80, 150, 200, 300, 450, 500)
Joining three tuples = (20, 40, 80, 150, 200, 300, 450, 500, 650, 800, 1000)
#Tuple1
mytuple1 = (10, 20, 50, 100)
print(mytuple1)
#Tuple2
mytuple2 = ("Ronaldo","Neymar")
print(mytuple2)
#Tuple3
mytuple3 = (5.7,8.9)
print(mytuple3)
#Concatenate
print(mytuple1 + mytuple2)
#Concatenate
print(mytuple1 + mytuple2 + mytuple3)
(10, 20, 50, 100)
('Ronaldo', 'Neymar')
(5.7, 8.9)
(10, 20, 50, 100, 'Ronaldo', 'Neymar')
(10, 20, 50, 100, 'Ronaldo', 'Neymar', 5.7, 8.9)
mytuple = (150, 100, 350, 800, 500)
print (max(mytuple));
800
mytuple1 = (150, 100, 350, 800, 500)
print (min(mytuple1));
mytuple2 = (5.9, 7.9, 1.2, 5.8, 9.9, 6.3)
print (min(mytuple2));
100
1.2
mylist = ["Neymar", "Messi", 3.5, 50]
print ("List = ", mylist)
https://studyopedia.com/python3/python-tuples/ 14/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
print ( List , mylist)
mytuple = tuple(mylist)
print ("Tuple = ", mytuple)
List = ['Neymar', 'Messi', 3.5, 50]
Tuple = ('Neymar', 'Messi', 3.5, 50)
mytuple = (20, 40, 80, 150, 200);
del mytuple
#error will occur since we deleted the Tuple above
print(mytuple)
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
NameError: name 'mytuple' is not defined
https://studyopedia.com/python3/python-tuples/ 15/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
#tuple1
mytuple1 = (20, 40, 80, 150, 200);
#tuple2
mytuple2 = (300, 450, 500);
#tuple3
mytuple3 = (650, 800, 1000);
print("Joining two tuples = ",mytuple1 + mytuple2)
print("Joining three tuples = ",mytuple1 + mytuple2 + mytuple3)
Joining two tuples = (20, 40, 80, 150, 200, 300, 450, 500)
Joining three tuples = (20, 40, 80, 150, 200, 300, 450, 500, 650, 800, 1000)
In this tutorial, we saw what are Tuples in Python. How Tuples are different from Lists in Python? We also
saw different operations on Tuples.
Read More
Scope of Variables
Python Operators
Python Numbers
Type Conversion
Python Strings
Loops in Python
Decision Making Statements
Functions in Python
Dictionary in Python
https://studyopedia.com/python3/python-tuples/ 16/18
3/5/22, 10:57 AM Python Tuples Tutorial with Examples - Studyopedia
Python Lists
Python DateTime
} W
Share Print page 0 Likes
NO COMMENTS
https://studyopedia.com/python3/python-tuples/ 17/18