Worksheet Experiment 2.4

You might also like

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

WORKSHEET

EXPERIMENT 2.4

Student Name: Harsh Chigal UID: 20BCS5155


Branch: BE-CSE Section/Group: 704 B
Semester:4TH
Subject Name: PYTHON LAB

Question 1:

Write a Python program to replace last value of tuples in a list

CODE :-

print("NAME - HARSH CHIGAL / UID - 20BCS5155")


t1 = [(5,6,7,8,9,10)]
print([t[:-1] + (100,) for t in t1])
OUTPUT:-

Question 2:

Write a Python program to remove an empty tuple(s) from a list of tuples


CODE:-

print("NAME - HARSH CHIGAL / UID - 20BCS5155")


def Remove(tuples):
tuples = [t for t in tuples if t]
return tuples

tuples = [(), ('ram', '15', '8'), (), ('laxman',


'sita'),
('krishna', 'akbar', '45'), ('', ''), ()]
print(Remove(tuples))

OUTPUT :-
Question 3:

Write a Python program calculate the product, multiplying all the numbers of a
given tuple.

CODE:-

print("NAME - HARSH CHIGAL / UID - 20BCS5155")


def mutiple_tuple(nums):
temp = list(nums)
product = 1
for x in temp:
product *= x
return product

nums = (4, 3, 2, 2, -1, 18)


print ("Original Tuple: ")
print(nums)
print("Product - multiplying all the numbers of the said
tuple:",mutiple_tuple(nums))

nums = (2, 4, 8, 8, 3, 2, 9)
print ("\nOriginal Tuple: ")
print(nums)
print("Product - multiplying all the numbers of the said
tuple:",mutiple_tuple(nums))
OUTPUT :-
Question 4:

Write a Python program to convert a tuple of string values to a tuple of integer


values

CODE:-

print("NAME - HARSH CHIGAL / UID - 20BCS5155")


tupleString = "(6,7,8,9,10)"
print("The string tuple is : " + tupleString)
intTuple = eval(tupleString)
print("The integer Tuple is : " + str(intTuple))

OUTPUT :-
Question 5:

Write a Python program to check if a specified element presents in a tuple of


tuples

CODE:-

print("NAME - HARSH CHIGAL / UID - 20BCS5155")


def check_in_tuples(colors, c):
result = any(c in tu for tu in colors)
return result

colors = (
('Red', 'White', 'Blue'),
('Green', 'Pink', 'Purple'),
('Orange', 'Yellow', 'Lime'),
)
print("Original list:")
print(colors)
c1 = 'White'
print("\nCheck if",c1,"presenet in said tuple of
tuples!")
print(check_in_tuples(colors, c1))
c1 = 'White'
print("\nCheck if",c1,"presenet in said tuple of
tuples!")
print(check_in_tuples(colors, c1))
c1 = 'Olive'
print("\nCheck if",c1,"presenet in said tuple of
tuples!")
print(check_in_tuples(colors, c1))
OUTPUT :-
Learning outcomes (What I have learnt):

1. Control statements in Python

2. Conditional statements of Python

3. Looping statements in Python

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like