0% found this document useful (0 votes)
22 views3 pages

TUPLES

Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets and can contain duplicate values. The document discusses creating tuples, tuple operations like joining and slicing, and built-in tuple methods like count and index.

Uploaded by

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

TUPLES

Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets and can contain duplicate values. The document discusses creating tuples, tuple operations like joining and slicing, and built-in tuple methods like count and index.

Uploaded by

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

TUPLES

 Tuples are used to store multiple items in a single variable.

 A tuple is a collection which is ordered and unchangeable.


 Tuples are written with round brackets ().

thistuple = ("apple", "banana", "cherry")

print(thistuple)

('apple', 'banana', 'cherry')

 Tuple items are ordered, unchangeable, and allow duplicate values.

Create Tuple With One Item

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

thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
<class 'tuple'>
<class 'str'>
The tuple() Constructor

use the tuple() constructor to make a tuple.

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


print(thistuple)
('apple', 'banana', 'cherry')

thistuple = tuple("apple", "banana", "cherry")


print(thistuple)

Traceback (most recent call last):


File "./prog.py", line 1, in <module>
TypeError: tuple expected at most 1 argument, got 3

CREATING TUPLE
T1=eval(input("Enter tuple to be added :"))
Enter tuple to be added :(2,5,6,7,8,9)
print(T1)
(2, 5, 6, 7, 8, 9)

TUPLE OPERATIONS

1.Traversing
Tuple1 = (1, 2, 3, 4, 5, 6)
print(Tuple1)

for i in Tuple1:
print(i)

for i in range(len(Tuple1)):
print(Tuple1[i])
(1, 2, 3, 4, 5, 6)
1
2
3
4
5
6
1
2
3
4
5
6

2.Joining
Tuple1 = (1, 2, 3, 4, 5, 6)
Tuple2=(11,22,33,44,55)
print(Tuple1+Tuple2)
(1, 2, 3, 4, 5, 6, 11, 22, 33, 44, 55)
3.Repeating or Replicating
print(Tuple1*3)
(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)
4.Slicing
tuple= ('a','b','c','d','e','f','g','h','i','j')
print(tuple[0:6])
print(tuple[1:9:2])
print(tuple[-1:-5:-2])

('a', 'b', 'c', 'd', 'e', 'f')


('b', 'd', 'f', 'h')
('j', 'h')

5.UnPacking Tuples
When we create a tuple, we normally assign values to it. This is called "packing" a tuple:
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)

apple
banana
cherry

TUPLES FUNCTION AND METHODS


1 Len() tuple1= ('a','b','c','d','e','f','g','h','i','j')
. print(len(tuple1))
10
2 Max() Tuple = ( 1, 3, 4, 2, 5, 6 )
.
res = max(Tuple)
print('Maximum of Tuple is', res)
Maximum of Tuple is 6
3 Min() Tuple = ( -1, 3, 4, -2, 5, 6 )
.
res = min(Tuple)
print('Minimum of Tuple is', res)
Minimum of Tuple is -2
4 Index() my_tuple = ( 4, 2, 5, 6, 7, 5)
. print(my_tuple.index(5))
2
5 Count() Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
. Tuple2 = ('python', 'geek', 'python',
'for', 'GFG', 'python', 'geeks')

res = Tuple1.count(3)
print('Count of 3 in Tuple1 is:', res)

res = Tuple2.count('python')
print('Count of Python in Tuple2 is:', res)

Count of 3 in Tuple1 is: 3


Count of Python in Tuple2 is: 3

You might also like