You are on page 1of 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 2.4
Student Name: Gaurav Kumar UID: 21BCS5875
Branch: CSE Section/Group: 812-A
Semester:4th Date of Performance:24/4/23
Subject Name: Programming in Python Subject Code: 21CSP-259

Aim: Program to demonstrate creation and accessing of tuples and applydifferent kinds of
operations on them.

Source Code:
1. Write a Python program to replace last value of tuples in a list.
my_list = [(1, 2), (3, 4), (5, 6)]
new_value = 10
new_list = [(x[0], new_value) for x in my_list]
print(new_list)

Output:

2. Write a Python program to remove an empty tuple(s) from a list oftuples.


my_list = [(1, 2), (), (3, 4), (), (5, 6), ()]
new_list = []
for tup in my_list:
if tup:
new_list.append(tup)
print(new_list)

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Write a Python program calculate the product, multiplying all thenumbers of a given
tuple.
my_tuple = (1, 2, 3, 4, 5)
product = 1
for num in my_tuple:
product *= num
print(product)
OUTPUT:

4. Write a Python program to convert a tuple of string values to atuple of integer


values.
str_tuple = ('1', '2', '3', '4', '5')
int_list = []
for str_val in str_tuple:
int_val = int(str_val)
int_list.append(int_val)
int_tuple = tuple(int_list)
print(int_tuple)
Output:

5. Write a Python program to check if a specified element presents in atuple of tuples.

my_tuple = ((1, 2), (3, 4), (5, 6), (7, 8))


element = 5
for tup in my_tuple:
if element in tup:
print(f"{element} is present in the tuple of tuples.")
break
else:
print(f"{element} is not present in the tuple of tuples.")

Output:

You might also like