Project Proposal
Project Proposal
08.02.2024
─
Tanya
What is Tuple?
Creating tuples
T = () # empty tuple
T = (value1, value2, value3,….)
This construct is known as tuple display
construct
1. Empty Tuple
T = () Or
T = tuple()
4
Creating tuples
2. Single element Tuple
>>> T = (20)
>>> T
20
>>> T = 5,
>>> T
(5,)
>>> T = (100,)
>>> T
(100,)
5
Creating tuples
3. Creating long tuples
roots =
(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
4. Nested tuples
>>> T1 = (10,20,30,(40,50,60),100)
>>> len(T1) #5
>>> T1[1] # 20
>>> T1[3][1] # 50
6
>>> T = tuple('python')
>>> T
('p', 'y', 't', 'h', 'o', 'n')
>>> items=[100,200,300,400]
>>> T2 = tuple(items)
>>> T2
(100, 200, 300, 400)
>>> t1 = tuple(input('enter elements'))
enter elementsabcde
>>> t1
('a', 'b', 'c', 'd', 'e')
7
>>> mytuple
● For example
● Fruits = (“mango”,”apple”,”guaua”,”pomegranate”,”cherry”)
0 1 2 3 4
Mango Apple Guaua Pomegranate cherry
-5 -4 -3 -2 -1
9
● Tuple elements are accessed just like string like str[2] means
character at 2nd index tuple1[2] means elements at 2nd
index and tuple1[1:3] means all items between index 1 to 2
● Indexing and Slicing : T[i] will return item at index i and T[i:j]
means all items between index i to j-1 and T[i:j:n] means
every nth item between index i to j-1
>>> L1 = [10,20,30]
>>> T1 = (100,200,300)
Traversing tuple
qualifications=("B.A.","M.A.","B.Sc","M.Sc","MCA","M.Com","B.Tech")
for q in qualifications:
print(q)
Or
qualifications=("B.A.","M.A.","B.Sc","M.Sc","MCA","M.Com","B.Tech")
for i in range(len(qualifications)):
print("Index :“, i, „ „, qualifications[i])
12
Tuple operations
1. Joining Tuple
>>> t1=(10,20,30)
>>> t2=('a','b','c')
>>> t3 = t1 + t2
>>> t3
(10, 20, 30, 'a', 'b', 'c')
Note: you can add tuple with only another tuple and not
with int, complex number, string or list
>>> t1 + 20 #Error
If you want to add a tuple with another tuple with one value
only and if you write statement as:
>>> t1 + (20) # Error, because (20) will be treated as number
13
Tuple operations
To add single value tuple just add comma(,) after the value
as:
>>> t1 = (10,20,30)
>>> t1 + (50,)
(10,20,30,50)
Replicating Tuple:
>>> t1=("do","it")
>>> t1*3
('do', 'it', 'do', 'it', 'do', 'it')
14
Slicing Tuples
data=(10,20,30,1,7,9,100,51,75,80)
data2 = data[4:-4]
print(data2)
print(data[1:6])
print(data[4:-2])
print(data[-40:4])
print(data[::-1])
print(data[::-2])
print(data[2:10:2])
15
Slicing Tuples
data=(10,20,30,1,7,9,100,51,75,80)
data2 = data[4:-4]
print(data2)
print(data[1:6])
print(data[4:-2])
print(data[-40:4])
print(data[::-1])
print(data[::-2])
print(data[2:10:2])
Output
(7, 9)
(20, 30, 1, 7, 9)
(7, 9, 100, 51)
(10, 20, 30, 1)
(80, 75, 51, 100, 9, 7, 1, 30,
20, 10)
(80, 51, 9, 1, 20)
(30, 7, 100, 75)
16
Slicing Tuples
>>> tp1[2:5]*3
(15,20,8,500,1000)
17
Comparing tuples
>>> a=(10,20)
>>> b=(10,20)
>>> c=(20,10)
>>> a==b
True
>>> a==c
False
>>> d=(20.0,10.0)
>>> c==d
True
>>> a<c
True
18
Unpacking tuples
Creating a tuple from a set of values is called packing and its
reverse i.e. creating individual values from tuple‟s elements is
called unpacking.
Note: Tuple unpacking requires that the list of variables on the left
side must be same as the length of tuple.
19
Deleting tuples
>>>t1 = ( 10,20,30)
>>> print(t1)
(10,20,30)
>>> del t1
>>> print(t1) # Error t1 is not defined
20
Note: max() function will return maximum value only if all the elements
in tuple is of same type. If elements are of different type then python
will raise an exception.
>>> t1 = (10,20,30,(40,50),90)
>>> max(t1) # Error
21
5. count() : it return the count of any element in the tuple i.e. how
many times the given element is in the tuple. If given element not in
the tuple it return 0.
>>> val=(10,20,30,20,10,60,80,20)
>>> val.count(20)
3
>>> val.count(80)
1
>>> val.count(100)
0
6. tuple(): this method is actually a constructor used to create tuples
from different type of values.
Creating empty tuple
tup = tuple()
23
>>> t = tuple(10)
Error: „int‟ object is not iterable
24
(b) Using constructor function of lists and tuples i.e. list() and
tuple()
>>> foods=("rice","dosa","idli","mushroom","paneer")
>>> myfood = list(foods)
>>> myfood[2]="biryani"
>>> foods = tuple(myfood)
>>> foods
('rice', 'dosa', 'biryani', 'mushroom', 'paneer')