Professional Documents
Culture Documents
I037 - Manas Patel Experiment05
I037 - Manas Patel Experiment05
I037 - Manas Patel Experiment05
PRACTICAL 5
PRACTICAL 5
Tuple slicing is used to obtain a range of items. It also follows same format for slicing operation
as [start, stop, step]. If we do not specify step then compiler takes it by-default as 1. If we do
not specify start index then by default it starts from 0 and if stop index not specify then will
run up to last element of tuple.
Example:
Tup1= (2,4,6,8,0,1,3,5,7,9)
print (Tup1[1:4])
1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
This will print output as (4,6,8) as 1 will be starting index and 4 is ending index so it will
take one less then end index.
print (Tup1[:4])
Output: (2,4,6,8)
print (Tup1[4:])
Output: (0,1,3,5,7,9)
print (Tup1[0:10:2])
Output: (2,6,0,3,7)
print (Tup1[-4:-1])
Output: (3,5,7)
print (Tup1[-1:-6:-1])
Output: (9,7,5,3,1)
2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
tupl=([2,3],"abc",0,9)
tupl[0][1]=1
print(tupl) c. ([2,1],”abc”,0,9)
a. ([2,3],"abc",0,9)
b. ([1,3],"abc",0,9)
c. ([2,1],"abc",0,9)
d. Error
2. What is the output of the following code?
tp1 = (2,4,3)
tp3 = tp1*2
print(tp3) b. (2,4,3,2,4,3)
a. (4.8,6)
b. (2,4,3,2,4,3)
c. (2,2,4,4,3,3)
d. Error
3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
t = (1, 2)
2*t a. (1,2,1,2)
a. (1, 2, 1, 2)
b. [1, 2, 1, 2]
c. (1, 1, 2, 2)
d. [1, 1, 2, 2]
1. CODE
#Tuple: Operations
colors=("White","Red","Blue","Green")
print(colors)
T1=("Hello",)
T2=("Hello")
print(T1)
print(T2)
T1=T1+("World","Code")
print(T1)
'''T2=T2+("World","Code")
print(T2)''' #We cannot add tuple with more than 1 value in already created string
if "Red" in colors:
print("Color Red is in the tuple")
for x in colors:
if x.lower() in "red":
print("Color red is in the tuple")
4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
i=0
while i<len(colors):
print(colors[i])
i=i+1
print(T1)
print(T1*4)
OUTPUT
2. CODE
#Tuple using user input and find min and max
i=0
while i<n:
num=int(input("Enter element: "))
T1=T1+(num,)
i=i+1
print(T1)
print("Maximum: ",max(T1))
print("Minimum: ",min(T1))
MAX=T1[0]
for x in T1:
if x>MAX:
MAX=x
5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
print("Maximum: ",MAX)
MIN=T1[0]
for x in T1:
if x<MIN:
MIN=x
print("Minimum: ",MIN)
OUTPUT
3. CODE
#Find unique and repeated element from tuple
i=0
while i<n:
num=int(input("Enter element: "))
T1=T1+(num,)
i=i+1
print(T1)
Tr=()
for n in T1:
6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
if T1.count(n)>1:
if n not in Tr:
Tr=Tr+(n,)
print("Repeated: ",Tr)
for n in T1:
if T1.count(n)==1:
print(n)
OUTPUT
4. CODE
#Number to words
TW=('Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine')
num=int(input("Enter a number: "))
'''words=()
while num>0:
d=num%10
num=num//10
words=(TW[d],)+words
print("Number in words: ",words)''' #Output will be in tuple
words=()
while num>0:
7|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
d=num%10
num=num//10
words=words+(TW[d],)
OUTPUT
3. Conclusion (Learning Outcomes): On completion of the practical the learning output was
to learn about tuple, how it is declared and user-inputted tuple. Then in-built functions like
min, max and len. Also, some operations like addition to add tuples and make a new tuple,
multiplication to replicate already made tuple. And slicing and indexing tuple in for and
while loop with if-else conditions.
8|Page