0% found this document useful (0 votes)
1 views12 pages

Tuples in Python

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

Tuples in Python

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Tuples in Python

A tuple is a data-structure, that can store multiple data. A tuple is immutable type, i.e., we
cannot add and delete elements from the tuple. The values that make up a tuple are called
its elements. The elements are indexed according to a sequence and the indexing is done
with 0 as the first index.

To create a Tuple, you separate the elements with a comma and enclose them with a
bracket “()”.

Syntax:
<name of Tuple> = ( <value1>, <value2>, <value3> )

For example:
1. tuple1 = (67, 82, 98, 92, 78, 87) - numeric tuple
2. tuple2 = ('Pen', 'Pencil', 'Rubber’) - a character/string tuple
3. tuple3 = ('A V Raman', 35,'TGT Computer’) - a tuple with multiple type values
4. tuple4 = () - an empty tuple

1
We can access individual tuple elements using its index value and a range of elements using
slicing. A tuple index starts from 0.

Python indexes the tuple element from left to right and from right to left. From left to right,
the first element of a tuple has the index 0 and from right end to left, the extreme right
index element of a tuple is –1. Individual elements in a tuple can be accessed by specifying
the tuple name followed by a number in round brackets (( )).
t1= (10,20,30,40,50,60)
Tuple 10 20 30 40 50 60
Left to Right 0 1 2 3 4 5
Right to Left -6 -5 -4 -3 -2 -1

Tuples are immutable which means that after initializing a tuple, it is impossible to update
individual items in a tuple.

2
Traversing a List
#1:Using for loop #2:For loop and range()
tuple1 = (1, 3, 5, 7, 9) tuple1 = (1, 3, 5, 7, 9)
for i in tuple1 : length = len(tuple1)
print(i) for i in range(length):
print(tuple1[i])

Adding elements in a tuple


#3: Using while loop
tuple1 = [1, 3, 5, 7, 9]
length = len(tuple1)
i=0
while i < length:
print(tuple1 [i])
Single
i += 1
element
tuple

3
Slicing tuple Elements
Selecting a slice is similar to selecting an element(s) of a tuple. Subsets of tuples can be
taken using the slice operator with two indices in square brackets separated by a colon.

<tuple name> [start:stop:step]

start: Starting index where the slicing of object starts.


stop: Ending index where the slicing of object stops. (index-1)
step: It is an optional argument that determines the increment between each index
for slicing.

For example:

4
Operators in Tuples
Membership Operators:
in - This operator tests if an element is present in tuple or not. If an element exists in the
tuple, it returns True, otherwise False.
a = (1, 5, 12,10)
print(5 in a)
o/p: True

not in - The not in operator evaluates True if it does not find a element in the specified
tuple, otherwise False.
a = (1, 5, 12,100)
print(1 not in a)
o/p: False

Concatenation Operator(+) – This operator concatenates two tuples with each other and
produces a third tuple.
a = (2, 3,4)
b = (1, 5, 6)
c=a+b
print (c)
o/p: (2, 3, 4,1, 5, 6)

Replicating Operator(*) -This operator repeatedly concatenates the tuples.

a = (2, 3,4)
print (a * 2)
o/p: (2, 3, 4,2, 3, 4)

6
Tuple Functions and Methods
Python has a complete list of public methods that may be called on any tuple object. The
dot operator (.) is used along with the tuple to access tuple functions.

1.tuple() - This function returns a tuple of elements. The iterable argument is optional. You
can provide any sequence or collection (such as a string, list, tuple, set, dictionary, etc). If
no argument is supplied, an empty tuple is returned.

Syntax
tuple([iterable])

2. len() - This method returns the length of the tuple.

Syntax:
len(<tuple>)

Num = (23, 54, 34,69)


print (len(Num)) # o/p: 4
7
3. count() - This method returns count of how many times a given element occurs in a tuple.
Syntax:
<tuple>.count(element)

4. index() - This method searches for given element from start of the tuple and returns the
index of first occurrence of the element.

Syntax:
<tuple>.index(element, start, end)

8
5. reversed() - The method is used to reverse a tuple and returns an iterator. The method gets
a return value and they will leave the original tuple unmodified.
Syntax:
reversed(tuple)

6. sorted() - This function is used to return a sorted list of a tuple. Since tuples are arrays that
you cannot modify, they do not have an in-place sort function that can be called directly on
them.
Syntax:
sorted(tuple,reverse=False/True)

By default, reverse
takes False value

9
7. max() - This function returns the item with the highest value in the tuple. If the values
are strings, an alphabetically comparison is done.
Syntax
max(<tuple>)

8. min() - This function returns the item with the lowest value in the tuple. If the values
are strings, an alphabetically comparison is done.
Syntax
min(<tuple>)

10
9. sum() – This function is used to display sum of the numbers in the tuple.

Syntax
sum(iterable,add)

iterable : iterable can be a list , tuple or dictionary , but most importantly it should be
numbers.

add : It is added to the sum of numbers in the iterable. If start is not given in the syntax , it
is assumed to be 0.

11
Nested Lists:

• A nested tuple is a tuple that appears as an element in another tuple.


• In the t1, the element with index 3 is a nested tuple. If we print(t1[3]), we get (10, 20).
To extract an element from the nested tuple, we can proceed in two steps. First, extract
the nested tuple, then extract the item of interest.
t1= ("hello", 2.0, 5, (10, 20))
innertuple = t1[3]
print(innertuple)
item = innertuple[1]
print(item)

• It is also possible to combine those steps using bracket operators that evaluate from left
to right.
print(t1[3][1])

12

You might also like