Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

EXPERIMENT NO- 2.

4
NAME: Archit Sharma UID: 20BCS4862
SECTION/GROUP: 706/A4 SUBJECT: Python LAB
SEMESTER: 4th BRANCH: CSE

QUESTION- 1

AIM:

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

CODE:
#Python program to replace last value of tuples in a list

# Initializing list
test_list = [('gfg', 1), ('was', 2), ('best', 3)]

# printing original list


print("The original list is : " + str(test_list))

# Initializing change record


repl_rec = ('is', 2)

# Initializing N
N = 1

# Replace tuple according to Nth tuple element


# Using list comprehension
res = [repl_rec if sub[N] == repl_rec[N] else sub for sub in test_list]

# printing result
print("The tuple after replacement is : " + str(res))
CODE SCREENSHOT:
OUTPUT SCREENSHOT:

QUESTION- 2

AIM:

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

CODE:
#Write a Python program to remove an empty tuple(s) from a list of
tuples
def Remove(tuples):
    tuples = [t for t in tuples if t]
    return tuples
# Driver Code
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),
        ('krishna', 'akbar', '45'), ('',''),()]
print(Remove(tuples))

CODE SCREENSHOT:

OUTPUT SCREENSHOT:
QUESTION- 3

AIM:

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

CODE:
#Write a Python program calculate the product, multiplying all the
numbers of a given tuple.
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))
CODE SCREENSHOT:

OUTPUT SCREENSHOT:
QUESTION- 4

AIM:

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


integer values

CODE:
#Write a Python program to convert a tuple of string values to a tuple
of integer values
# initializing string
test_str = "(7, 8, 9)"
# printing original string
print("The original string is : " + test_str)

# Convert Tuple String to Integer Tuple


# Using tuple() + int() + replace() + split()
res = tuple(int(num) for num in test_str.replace('(', '').replace(')',
'').replace('...', '').split(', '))

# printing result
print("The tuple after conversion is : " + str(res))

CODE SCREENSHOT:

OUTPUT SCREENSHOT:
QUESTION- 5

AIM:

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


tuples

CODE:
#Write a Python program to check if a specified element presents in a
tuple of tuples
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))

CODE SCREENSHOT:
OUTPUT SCREENSHOT:
Learning outcomes (What I have

learnt): 1. Usage of else, if, elif

statements.

2. Usage of while loop.


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