0% found this document useful (0 votes)
2 views

Tuple

A tuple is an ordered and immutable collection of objects in Python, created using parentheses. Unlike lists, tuples cannot be modified after creation, making them suitable for read-only operations. Tuples can contain duplicate values, support indexing, and can be created using the tuple() constructor or by simply separating values with commas.

Uploaded by

Anjali Negi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tuple

A tuple is an ordered and immutable collection of objects in Python, created using parentheses. Unlike lists, tuples cannot be modified after creation, making them suitable for read-only operations. Tuples can contain duplicate values, support indexing, and can be created using the tuple() constructor or by simply separating values with commas.

Uploaded by

Anjali Negi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Tuple

A collection of ordered and immutable objects is known as a tuple.


Tuples and lists are similar as they both are sequences. Though, tuples and
lists are different because we cannot modify tuples, although we can
modify lists after creating them, and also because we use parentheses to
create tuples while we use square brackets to create lists.
A tuple is a collection of objects which ordered and immutable.
Tuples are sequences, just like lists. The differences between tuples and lists
are, the tuples cannot be changed unlike lists and tuples use parentheses,
whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated
values. Optionally you can put these comma-separated values between
parentheses also. For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing −
tup1 = ();
A collection of ordered and immutable objects is known as a tuple. Tuples
and lists are similar as they both are sequences. Though, tuples and lists are
different because we cannot modify tuples, although we can modify lists
after creating them, and also because we use parentheses to create tuples
while we use square brackets to create lists.
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store
collections of data, the other 3 are List, Set, and Dictionary, all with
different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Creating a tuple is as simple as putting different comma-separated values.

Example:
tup1=("delhi","mumbai","pune","goa")
OR
tup1="delhi","mumbai","pune","goa"
OR
tup1=('delhi','mumbai','pune','goa')
OR
tup1='delhi','mumbai','pune','goa'

print(tup1)
Output:
('delhi', 'mumbai', 'pune', 'goa')

Note: The empty tuple is written as two parentheses containing nothing −


tup1 = ();

List Vs Tuple:
List Tuple

List is mutable. Tuple is immutable.

items in lists are surrounded by the items in tuples are surrounded by


square brackets[ ] parentheses( )

List iteration is slower and is time Tuple iteration is faster.


consuming.
List is useful for insertion and Tuple is useful for read only
deletion operations. operations like accessing elements

List consumes more memory. Tuples consumes less memory.

List provides many in-built methods. Tuples have less in-built methods.

List operations are more error prone. Tuples operations are safe
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.

Ordered
When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.
Example:
tup1="delhi","mumbai","pune","goa"
print(tup1[0])
print(tup1[2])
Output:
delhi
pune

Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created.
Example:
tup1="delhi","mumbai","pune","goa"
print(tup1[1])="Punjab"

Output:
Error

Allow Duplicates
Since tuples are indexed, they can have items with the same value.
Example:
tup1="delhi","delhi","pune","goa"
print(tup1)
Output:
('delhi', 'delhi', 'pune', 'goa')

Tuple Length
To determine how many items a tuple has, use the len() function:
Example:
tup1=("delhi","delhi","pune","goa")
print(len(tup1))
Output:
4

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.

Example:
tup1=("delhi")
print(type(tup1))
tup1=("delhi",)
print(type(tup1))
Output:
<class 'str'>
<class 'tuple'>

Tuple Items - Data Types


Tuple items can be of any data type, but type of the tuple will always be
tuple.
Example:
tup1=("delhi","delhi","pune","goa")
tup2=(1,2,3,4,5)
tup3=("a",5,"True","45.7")
print(type(tup1))
print(type(tup2))
print(type(tup3))
Output:
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

The tuple() Constructor


It is also possible to use the tuple() constructor to make a tuple. You can also
use the tuple() class constructor to create tuple objects from an iterable,
such as a list, set, dictionary, or string. If you call the constructor without
arguments, then it’ll build an empty tuple.
Example:

tup1=(["delhi","delhi","pune","goa"],"a",1)
print(tup1)
Output:
(['del 'delhi', 'pune', 'goa'], 'a', 1)

Note: To Access “Pune”, we should write: tup1[0][2].

Access Tuple Items


You can access tuple items by referring to the index number, inside square
brackets.
Example:
tup1=(("delhi","delhi","pune","goa"))
print(tup1[0])
print(tup1[1])
print(tup1[2])
print(tup1[3])
Output:
delhi
delhi
pune
goa

Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
Example 1:
tup1=(("delhi","delhi","pune","goa"))
print(tup1[-1])
print(tup1[-2])
print(tup1[-3])
print(tup1[-4])

Output:
goa
pune
delhi
delhi

Example 2:
tup1=("delhi","mumbai","pune","goa","punjab","hydrabad")
print(tup1[0:6])
print(tup1[:4])
print(tup1[2:])
print(tup1[0:8])
print(tup1[0:-4])
print(tup1[:-3])
print(tup1[-3:])
print(tup1[-4:-1])

Output:
('delhi', 'mumbai', 'pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai', 'pune', 'goa')
('pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai', 'pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai')
('delhi', 'mumbai', 'pune')
('goa', 'punjab', 'hydrabad')
('pune', 'goa', 'punjab')

Check- if Item Exists


We can determine whether a particular element exist in the tuple or not. To
determine if a specified item is present in a tuple use the in keyword.
Example:
tup1=("delhi","mumbai","pune","goa","punjab","hydrabad")
if "goa" in tup1:
print("Element is present in tuple")
else:
print("Element is not present in the tuple")
Output:
Element is present in tuple

To add elements in tuple


As we know tuples are immutable, so we can not add elements. In case we
wish to add elements, we need to convert tuple into list first.
Example:
tup1=("delhi","mumbai","pune","goa","punjab","hydrabad")
list1=list(tup1)
list1.append("Jaipur")
print(list1)
Output:
['delhi', 'mumbai', 'pune', 'goa', 'punjab', 'hydrabad', 'Jaipur']

Join Two Tuples


We already know that it is not possible to add element in the tuple, but we
can append one tuple after another. In this case elements of both the tuples
will be merged into one.

Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
tup2=("Jaipur",)
tup3=tup1+tup2
print(tup3)
Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad', 'Jaipur')

Deleting a tuple
Del method is used to delete the tuple completely.
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
print(tup1)
del tup1
print(tup1)

Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad')

Example:
tup1=("a","b","c")
tup1.pop() #OR tup1.remove()
print(tup1)
Output:
Error
Note: Pop and remove method does not exist for tuple

Unpacking a Tuple
When we create a tuple, we normally assign values (index) to all the
elements it. This is called "packing" a tuple.
But, in Python, we are also allowed to extract the values back into variables.
This is called "unpacking".
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
(a,b,c,d,e)=tup1
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
delhi
mumbai
pune
goa
hydrabad
Note: While unpacking, number of variables used should be equal to number
of elements present in tuple. Othrwise Error will come.
Printing the elements using loop
We can print the elements of tuple using loops.
Example 1:
tup1=("delhi","mumbai","pune","goa","hydrabad")
for a in tup1:
print(a)
Output:
delhi
mumbai
pune
goa
hydrabad
Example 2:
tup1=("delhi","mumbai","pune","goa","hydrabad")
for a in range(len(tup1)):
print(tup1[a])
Output:
delhi
mumbai
pune
goa
hydrabad
Example 3:
tup1=("delhi","mumbai","pune","goa","hydrabad")
a=0
while a<len(tup1):
print(tup1[a])
a=a+1
Output:
delhi
mumbai
pune
goa
hydrabad
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you
can use the * operator.
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
newtup1=tup1*2
print(newtup1)
Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad', 'delhi', 'mumbai', 'pune', 'goa',
'hydrabad')

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

Count() method
This method is used to count number of occurrences of an element in a
tuple.
Example:
tup1=("pune","mumbai","pune","goa","pune")
print(tup1.count("pune"))
Output:
3
index() method
This method is used to find index number of any particular element of the
tuple.
Example:
tup1=("delhi","mumbai","pune","goa","jaipur")
print(tup1.index("mumbai"))
Output:
1
Finding maximum and minimum element from tuple
We can find maximum and minimum element in numric as ell as text data
Example:
tup1=(34,67,43,12,58,93,8)
print(max(tup1))
print(min(tup1))
Output:
93
8
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
print(min(tup1))
print(max(tup1))
Output:
delhi
pune

You might also like