0% found this document useful (0 votes)
5 views6 pages

Assignment 8

The document contains a series of programming questions and solutions related to tuples in Python. It includes tasks such as finding the product of tuple elements, merging tuples while removing duplicates, zipping and unzipping lists, sorting tuples, and generating lists using comprehensions. Each question is followed by a corresponding solution that demonstrates the implementation in Python.

Uploaded by

patelvikash0117
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Assignment 8

The document contains a series of programming questions and solutions related to tuples in Python. It includes tasks such as finding the product of tuple elements, merging tuples while removing duplicates, zipping and unzipping lists, sorting tuples, and generating lists using comprehensions. Each question is followed by a corresponding solution that demonstrates the implementation in Python.

Uploaded by

patelvikash0117
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#Q 1 Write a program to find the product of all the elements in tuples.

#Solution 1

t=(4,7,9,10,2)

pro=1

for i in t:

pro=pro*i

print(pro)

#Q 2 Write a function to merge two tuples and remove duplicates.

#Solution 2

t1=(3,5,7,9,11)

t2=(4,6,7,9,12,14,7)

t3=(t1+t2)

l1=list(t3)

for i in l1:

if l1.count(i)>1:

for j in range(l1.count(i)-1):

l1.remove(i)

t3=tuple(l1)

print(t3)
#Q 3 and 4 Write a python function that accepts a variable number of arguments and returns a tuple
containing the sum and product of all the arguments.

#Solution 3 and 4

n=int(input("Enter the number of arguments:"))

def func(n):

sum=0

product=1

for i in range(n):

a=int(input(f"Enter argument no.{i+1}:"))

sum+=a

product*=a

t=(sum,product)

return t

x=func(n)

print(x)
#Q 5 Write a python program to zip two lists into a list of tuples and then unzip the tuple back into
separate lists.

#Solution 5

l1=[4,6,8,11,19]

l2=[5,7,13,15,29]

l3=[]

for i in zip(l1,l2):

l3.append(i)

print(l3)

l4,l5=zip(*l3)

print(list(l4))

print(list(l5))
#Q 6 write a program to sort tuple of tuples by second item.

#Solution 6

t=((2,3,4),(5,4,3,1),(2,1),(9,8,3,2))

a=t[0][1]

b=t[1][1]

c=t[2][1]

d=t[3][1]

l=[a,b,c,d]

l.sort()

l[l.index(a)]=t[0]

l[l.index(b)]=t[1]

l[l.index(c)]=t[2]

l[l.index(d)]=t[3]

t=tuple(l)

print(t)

#Q 7 Make a taple of list called sandwich_orders and fill it with the names of various sandwiches.
Then make an empty list called finished sandwiches. Loop through the list of sandwich orders and
print a message for each order, such as I made your turn sandwich. As each sandwich is made, move
it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing
each sandwich that was made.

#Solution 7

sandwich_orders=(["tuna","club","tortas","grilled cheese","bacon"])

finished_sandwiches=[]

for i in sandwich_orders:

finished_sandwiches.append(f"I made you a {i} sandwich")


print(finished_sandwiches)

#Q 8 Use a list comprehension to generate a list of the first 10 cubes.

#Solution 8

l=[i**3 for i in range(1,11)]

print(l)

#Q 9 Write a program to convert a tuple of integers to tuple of strings using list comprehension.

#Solution 9

t=(4,6,9,11,10,89)

l=list(t)

l1=[str(i) for i in l]

t=tuple(l1)

print(t)
#Q 10 Use a nested list comprehension to find all the numbers from 1-100 that are divisible by any
single digit beside 1(2-9).

#Solution 10

l=[i for i in set([i for i in range(1,101) for j in range(2,10) if i%j==0])]

print(l)

You might also like