0% found this document useful (0 votes)
80 views

Minor Assignment-7 (Mutable and Immutable Objects)

The document describes a minor assignment on mutable and immutable objects in Python. It includes questions to write functions that remove duplicates from a list, calculate cumulative sums, count letter frequencies in a string, and identify outputs of code snippets involving lists, tuples, dictionaries and sets. It also includes questions on operations like indexing, slicing, sorting, mapping, joining, zipping, updating and performing set operations on the various data types.

Uploaded by

Notes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Minor Assignment-7 (Mutable and Immutable Objects)

The document describes a minor assignment on mutable and immutable objects in Python. It includes questions to write functions that remove duplicates from a list, calculate cumulative sums, count letter frequencies in a string, and identify outputs of code snippets involving lists, tuples, dictionaries and sets. It also includes questions on operations like indexing, slicing, sorting, mapping, joining, zipping, updating and performing set operations on the various data types.

Uploaded by

Notes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University

Programming in Python (CSE 3142)


M INOR A SSIGNMENT-7: M UTABLE AND I MMUTABLE OBJECTS

1. Write a function that takes a list of values as input parameter and returns another list without any
duplicates.

2. Write a function that takes a list of numbers as input from the user and produces the corresponding
cumulative list where each element in the list at index i is the sum of elements at index j <= i.

3. Write a program that takes a sentence as input from the user and computes the frequency of each
letter. Use a variable of dictionary type to maintain the count.

4. Identify the output produced when the following functions are invoked.
1.
def func():
l1 = list()
l2 = list()
for i in range(0,5):
l1.append(i)
l2.append(i+3)
print(l1)
print(l2)

2.
def func():
l1 = list()
l2 = list()
for i in range(0,5):
l1.append(i)
l2.append(i+3)
l1, l2 = l2, l1
print(l1)
print(l2)

5. Determine the output of the following code snippets:


1.
c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = 0
for i in range(0, 10):
if (c[i]%2 == 0):
result += c[i]
print(result)

2.
c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = 0
for i in range(0, 10):
if (c[i]%2 != 0):
result += c[i]
print(result)

1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

3.
subject = ’computer’
subject = list(subject)
ch = subject[0]
for i in range(0, len(subject)-1):
subject[i] = subject[i+1]
subject[len(subject)-1]=ch
print(’’.join(subject))

4.
quantity = [15, 30, 12, 34, 56, 99]
total = 0
for i in range(0, len(quantity)):
if (quantity[i] > 15):
total += quantity[i]
print(total)

5.
x = [1, 2, 4, 6, 9, 10, 14, 15, 17]
for i in range(0, len(x)):
if (x[i]%2 == 0):
x[i] = 4*i
elif (x[i]%3 == 0):
x[i] = 9*i
else:
x[i] *= 2
print(x)

6. Write a function that takes n as an input and creates a list of n lists such that ith list contains first five
multiples of i.

7. Write a function that takes a number as an input parameter and returns the correspond text in words,
for example, on input 452, the function should return ’Four Five Two’. Use a dictionary for mapping
digits to their string representation.

8. Given the following inputs, indicate in each case (a) to (w), whether the statements will execute
successfully. If, so, give what will be the outcome of execution? Also give the output of print
statements (where applicable):
address = ’B-6, Lodhi road, Delhi’
list1 = [1, 2, 3]
list2 = [’a’, 1, ’z’, 26, ’d’, 4]
tuple1 = (’a’, ’e’, ’i’, ’o’, ’u’)
tuple2 = ([2,4,6,8], [3,6,9], [4,8], 5)
dict1 = {’apple’: ’red’, ’mango’: ’yellow’, ’orange’: ’orange’}
dict2 = {’X’: [’eng’, ’hindi’, ’maths’,’science’], ’XII’: [’english’, ’physics’,
’chemistry’, ’maths’]}

a.
list1[3] = 4

b.
print(list1 * 2)

2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

c.
print(min(list2))

d.
print(max(list1))

e.
print(list(address))

f.
list2.extend([’e’, 5])
print(list2)

g.
list2.append([’e’, 5])
print(list2)

h.
names = [’rohan’, ’mohan’, ’gita’]
names.sort(key= len)
print(names)

i.
list3 = [(x * 2) for x in range(1, 11)]
print(list3)

j.
del list3[1:]
print(list3)

k.
list4 = [ x+y for x in range(1,5) for y in range(1,5)]
print(list4)

l.
tuple2[3] = 6

m.
tuple2.append(5)

n.
t1 = tuple2 +(5)

o.
’,’.join(tuple1)

p.
list(zip([’apple’, ’orange’], (’red’,’orange’)))

q.
dict2[’XII’]

3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

r.
dict2[’XII’].append(’computer science’),dict2

s.
’red’ in dict1

t.
list(dict1.items())

u.
list(dict2.keys())

v.
dict2.get(’XI’, ’None’)

w.
dict1.update({’kiwi’:’green’})
print(dict1)

9. Consider the following three sets, namely vehicles, heavyVehicles, and lightVehicles:
iii vehicles = {’Bicycle’, ’Scooter’, ’Car’, ’Bike’, ’Truck’, ’Bus’, ’Rickshaw’}
iii heavyVehicles = {’Truck’, ’Bus’}
iii lightVehicles = {’Rickshaw’, ’Scooter’, ’Bike’, ’Bicycle’}
Determine the output on executing the following statements:
1.
lytVehicles = vehicles - heavyVehicles
print(lytVehicles)

2.
hvyVehicles = vehicles - lightVehicles
print(hvyVehicles)

3.
averageWeightVehicles = lytVehicles & hvyVehicles
print(averageWeightVehicles)

4.
transport = lightVehicles | heavyVehicles
print(transport)

5.
transport.add(’Car’)
print(transport)

6.
for i in vehicles:
print(i)

7.
len(vehicles)
4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

8.
min(vehicles)

9.
set.union(vehicles, lightVehicles, heavyVehicles)

You might also like