I037 - Manas Patel Experiment05

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

PRACTICAL 5

Sequence: Tuple (create, access, update, delete)


Problem Statement: Write Python program to
1. demonstrate working with tuples (create, length, in).
2. create a tuple, and find the minimum and maximum number from it.
3. find the repeated items of a tuple.
4. print the number in words for Example: 1234 => One Two Three Four

PRACTICAL 5

Sequence: Tuple (create, access, update, delete)


Roll No.: I037 Name: Manas Hiteshbhai Patel
Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:

1. Questions based on Experiment Scenario:


a. Describe slicing and indexing in tuple with example.
Ans. Tuple indexing refers to accessing any element in a tuple and index in tuple starts from
zero. We can also start indexing from right to left by using -1, -2, ….
Example:
Tup1 = (10,20,30,40,50)

Tup1[0] will return output as 10


Tup1[3] will return output as 40
Tup1[-2] will return output as 40
Tup1[1+3] will return output as 50

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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)

b. Describe built-in tuple functions.


Ans.
len(Tup) is in-built tuple function to return the total length of the tuple.
max(Tup) will return the maximum value from tuple
min(Tup) will return the minimum value from tuple

c. Compare Tuple with List (any 4 points).


Ans.
Tuple List
• Tuple is immutable • List is mutable
• It has less built-in function compare to list • It has several built-in functions
• It takes less memory • It consumes more memory
• Tuple is appropriate for accessing the • List has better performing operations
elements. such as insertion and deletion.

2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III
d. Multiple Choice
1. What will be the output of below Python
code?

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?

t = (10, 20, 30, 40, 50, 50, 70)


print (t[5:-1])
d. (50,)
a. Blank output()
b. (50,70)
c. (50, 50, 70)
d. (50,)
3. What will be the output of following Python
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

4. What will be the output of the following


Python code?
t=(1,2,4,3)
t[1:-1]
c. (2,4)
a. (1, 2)
b. (1, 2, 4)
c. (2, 4)
d. (2, 4, 3)

3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

5. What will be the output of the following


Python code?

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]

2. Program Code along with Sample Output:

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")

print("Number of elements in tuple: ",len(colors))

4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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

n=int(input("Enter number of elements: "))


T1=()

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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

n=int(input("Enter number of elements: "))


T1=()

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

d=num%10
num=num//10
words=words+(TW[d],)

print("Number in words: ",words)


for i in range(len(words)-1,-1,-1):
print(words[i],end=" ")

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

You might also like