HTTP Schools - Eklavyafocs.com Pdvlpsmain-2024 Worksheet c14 Eleventh - C m10 d18 Computer Science 4
HTTP Schools - Eklavyafocs.com Pdvlpsmain-2024 Worksheet c14 Eleventh - C m10 d18 Computer Science 4
QB Python Lists
1. Write Python statements to create the following lists:
(i) A list of 5 fruit names and name it as fruit.
(ii) A list of 5 city names and name it as City.
(iii) A list containing 3 strings and 2 integers and name it as Mixed.
(iv) A list containing 3 integers and a list of 2 strings and name it as Box.
(v) A list of 10 random integers each in the range 10 to 50 and name it as R10.
(vi) A list of 10 random floating point numbers in the range 10.00 to 49.99 and name it as
F10.
(vii) A list of 10 values of the expression x 2+2x-1, for integer values of x from -4 to 5 and
name it as y.
(viii) A list of 10 random integers in the range -30 to -20 and name it as C.
(ix) A list, named A, containing 3 numbers, and another list, named B, congaing list A and 3
numbers.
(x) A list, named A, containing 3 numbers, and another list, named B, containing list A and
another unnamed list of 3 integers.
2. Assuming that A is a list, differentiate between:
(i) A[2] and A[2:]
(ii) A[2:] and A[:2]
(iii) A[2::] and A[::2]
(iv) A*3 and [A,A,A]
3. Consider list A given below and write the output of the statements that follow:
A=[3,6,7,8,10,12,23,33,16,6,2,1]
(i) print(len(A)) (ii) print(A[3]) (iii) print(A[-3])
(iv) print(A[3:]) (v) print(A[-3:]) (vi) print(A[::3])
(vii) print(A[3::]) (viii) print(A[3::-2]) (ix) print(A[-3::-2])
(x) print(A[::-3]) (xi) print(A[3:-3:3]) (xii) print(A[3:-3:-3])
(xiii) print(len(A[3:])) (xiv) print(len(A[-3:])) (xv) print(len(A[::-3]))
4. Consider the lists A and B given below and state the values (True/False) of the expressions that
follow:
A=[5, 3, 8]
B=[A, 8, 66, 45]
8. Consider the lists A and B given below and write the output of the statements that follow:
A=[5, 3, 8]
B=[A, 8, 66, 45, 'A',['A',A], ["nested", "lists"]]
11. Identify and correct the errors, if any, in the following code segments, and underline each
correction.
(i) a=[1,2,3,2,1]
for x in range(a):
print(x)
(ii) a=["Umang","Usman","Utkarsh","Umedh"]
for x in range(len(a)):
print(a(x))
(iii) A=5*(None)
A=8*[3]
for i in range(5):
A[i]=eval(A[i]*i)
for i in A:
print(A(i))
12. Assuming that nums is a list of integers, write code to find the length of nums without using the
inbuilt function len().
13. Assuming that num is a list of integers, write Python code to divide all those elements of list by
5 which are divisible by 5 and multiply the other list elements by 2. The program should display
the original list and the changed list. No element of the list should be changed to float as a result
of division by 5. The code should display the original list and the changed list.
14. Assuming that num is a list of integers, write code display the list. The code should then replace
each element having even value with its half and each element having odd value with its double,
and then display the changed list.
Example: if list has: 3, 4, 5, 16, 9, then the modified list should have: 6, 2, 10, 8, 18
15. Write a program to input 10 numbers from the user and then display this list of numbers in reverse
order.
16. Write a program to input 10 numbers from the user and find their sum and average. After that
display all those numbers (entered by the user) which are greater than the average.
17. Write a program to create a list of 10 random integers in the range 10 to 99, and display this list.
Then display all the odd elements of the list.
18. Write a program to create a list of 10 random numbers in the range 10.00 to 99.99, and display
this list. Then interchange first half of the list with second half and display the updated list. For
example, if the original list is:
[98.39, 50.84, 42.57, 65.37, 46.78, 37.67, 16.98, 76.22, 54.24, 44.7]
then after interchange, the list should become
[37.67, 16.98, 76.22, 54.24, 44.7, 98.39, 50.84, 42.57, 65.37, 46.78]
19. Write a program to create a list of n random integers in the range 1 to 25, and display the list.
Then exchange the values of first half side elements with the second half side elements of the
list. n is to be input from the user. Example:
If a list of eight elements has initial contents as: 2,4,1,6,7,9,23,10
The function should rearrange the list as: 7,9,23,10,2,4,1,6
20. Write a program to create a list of 10 random numbers in the range 10.00 to 99.99, and display
this list. Then interchange consecutive elements of the list and display the updated list. For
example, if the original list is:
[88.76, 41.43, 35.9, 70.06, 88.77, 53.68, 33.01, 83.42, 35.29, 98.22]
then after interchange, the list should become
[41.43, 88.76, 70.06, 35.9, 53.68, 88.77, 83.42, 33.01, 98.22, 35.29]
21. Write a program to create a list NUMBERS of n random integers and display the list. The
program should then reverse the contents of the list without slicing the list and without using any
second list.
Example: If the list initially contains 2, 15, 3, 14, 7, 9, 19, 6, 1, 10
then after reversal the list should contain 10, 1, 6, 19, 9, 7, 14, 3, 15, 2
22. Write a program to create a list of 10 random integers in the range 100 to 999, and display this
list. Then display all the elements of the list which are prime.
23. Write a program to create a list of 20 random integers in the range 1 to 20 and display this list.
The program should then shorten the remove by removing all the duplicate elements from the
list and display the shortened list. For example, if the original list is:
[16, 15, 20, 6, 9, 12, 18, 16, 2, 19, 9, 20, 20, 14, 17, 2, 19, 12, 10,
13]
Then the shortened list should be:
[16, 15, 20, 6, 9, 12, 18, 2, 19, 14, 17, 10, 13]
(Original sequence of elements should be maintained in the shortened list)
24. Write a program to input two lists A and B, and display these lists. Then from list A delete all the
elements which are present in list B too. Then display the list A.
25. Write a program to create two lists A and B. Then sort A in ascending order and B in descending
order, and merge the contents A and B into a third list C. The resultant list C is required to be in
ascending order. Program should not use the + operator to concatenate the lists.
26. Write a program to create two equi-sized lists A and B, and then combine their contents into a
third list C, by computing C[i]=2*A[i]+3*B[i]; where value i varies from 0 to N-1, N being the size
of each list. Then display the lists A, B, and C.
27. Assuming that two lists first and second already exist, write a program to transfer the content of
first and second to a third list ALL. The even places in ALL should be filled from list first and odd
places of ALL will be filled from list second. Example:
If first is 1,2,3,4,5,6 and second is 10, 20, 30, 40
The resultant ALL is: 10, 1, 20, 2, 30, 3, 40, 4, 5, 6
28. Write a program to input a list from the user and re-position all its elements by shifting each of
them to one position before and by shifting the first element to the last position. For example, If
the entered list is is [22, 25, 70, 32, 12], then the changed list should be [25, 70, 32, 12, 22]
(Do not use slicing in the program)
29. Write a program to input a list, P, from the user and modify its content in such a way that the
elements, which are multiples of 10 swap with the value present in the very next position in the
list. For example
If the content of list P is: 91, 50, 54, 22, 30, 54
Then content of list P should become: 91, 54, 50, 22, 54, 30
30. Write a program to input a list from the user and change all the multiples of 5 in the tuple to 5
and rest of the elements as 0. For example, if the list contains:
[55, 43, 20, 16, 39, 90, 83, 40, 48, 25]
Then after executing the program, the list content should be changed as follows:
[5, 0, 5, 0, 0, 5, 0, 5, 0, 5]
31. Write a program to create a list of random integers and replace each even element of the list
with the sum of its digits and each odd element with the product of its digits. For example, if the
tuple is given as:
[100, 43, 20, 56, 32, 91, 80, 40, 45, 21]
After executing the program, the tuple content should be changed as follows:
[1, 12, 2, 11, 5, 9, 8, 4, 20, 2]
32. To input a list, named Pay, from the user and modify each element of Pay, as per the following
rules:
Existing value of Pay Pay to be changed to
If less than 100000 Increase existing value by 25%
If >= 100000 and <200000 Increase existing value by 20%
If >= 200000 Increase existing value by 15%
33. Write a program to create a list names1 to store the names of n1 students of a class. Also create
a list names2 to store the names of n2 students of another class. n1, n2, and the names are to
be input from the user. Then combine these two lists into a new list and display this new list in
the ascending order of names.
34. Write a menu driven program which creates an empty list and gives the following options to the
user to perform various operations on a list:
(i) Append an element to the list
(ii) Input an element from the user and remove it from the list
(iii) Remove all elements from the list
(iv) Count the number of occurrences of an element in the list
(v) Sort the list
(vi) Reverse the list
(vii) Display the list
The program should terminate when the user chooses the option to exit.
35. Write a menu driven program which creates an empty list and gives the following options to the
user to perform various operations on the list:
(i) Input an element and a location from the user and insert the element at the entered
location in the list.
(ii) Input a location from the user and remove the element from that location in the list.
(iii) Input an element from the user and find its location in the list.
(iv) Display the list in sorted order without sorting the list.
(v) Display the list in reverse order without reversing the list.
(vi) Display the list
The program should terminate when the user chooses the option to exit.
36. Assume that nums is a list in Python and ele is an element to be searched for in the list. Write
Python code to search for ele in the list nums without using any inbuilt method of list class. (This
is equivalent to index() method of list class)
37. The roll numbers, names, and marks of n students of a class have to be stored in a list. Then a
list of all the students having marks more than 75 is to be displayed. Write a Python program to
do this.
38. Write a program to input a two digit number and display it in words. For example, if the input is
58, then the output should be Fifty Eight. If the number is not in the range 10 to 99, the program
should give an error message.
39. 50 students have to take an exam in an examination hall. Roll numbers of these students are
from 91041 to 91090. The invigilator in the examination hall has to maintain a list of roll numbers
of all the students present in the hall at any moment. For this we have to write a program for the
invigilator which will allow him/her to:
(i) Enter roll numbers of students as they enter the hall (one student at a time). While entry:
a. Only valid roll numbers should be allowed
b. No duplicate roll numbers should be allowed
(ii) Delete the roll number of any student who leaves the hall
(iii) Display the number of students present in the hall at any moment
(iv) Display the smallest and the largest roll number present in the hall
(v) Display a list of absentees’ roll numbers
(vi) Display a sorted list of roll numbers present
ANSWERS
1.
(i) fruit=['banana','mango','apple','peach','grapes']
(ii) City=['Delhi','Chennai','Kannur','Jalandhar','Hyderabad']
(iii) Mixed=['banana','apple','grapes',12,13]
(iv) Box=[1,5,2,['a','b']]
(v) R10=[random.randint(10,50) for i in range(10)]
(vi) F10=[random.randint(1000,4999)/100 for i in range(10)]
(vii) y=[x*x+2*x-1 for x in range(-4,6)]
(viii) C=[random.randint(-30,-20) for x in range(10)]
(ix) A=[3,5,6.7]
(x) B=[A,[1,2,3]]
2.
(i) A[2] is an element of A and A[2:] is a slice of A.
(ii) Slice A[2:] contains all elements of A starting from A[2] till the last element. Slice
A[:2] contains only the elements A[0] and A[1].
(iii) Slice A[2::] contains all elements of A starting from A[2] till the last element. Slice
A[::2] contains alternate elements of A starting from A[0] till the last element.
(iv) A*3 represents a list which contains all elements of A thrice. [A,A,A] represents a list
which contains three instances of list A.
3.
(i) 12 (ii) 8 (iii) 6
(iv) [8,10,12,23,33,16,6,2,1] (v) [6,2,1] (vi) [3,8,23,6]
(vii) [12,23,33,16,6,2,1] (viii) [8,6] (ix) [6,33,12,8,6]
(x) [1,16,12,7] (xi) [8,23] (xii) []
(xiii) 9 (xiv) 3 (xv) 4
4.
(i) True (ii) False (iii) False (iv) True
(v) True (vi) False (vii) False (viii) True
(ix) True (x) False
5. 2*3*2*3*2*
6. 0
0
0
© Yogesh Kumar Mandaan – (965)96891262)
QB/Python-XI/Lists/YK
i,j=0,n-1
while i<j:
NUMBERS[i],NUMBERS[j]=NUMBERS[j],NUMBERS[i]
i+=1
j-=1
print("The list after swap is: ",NUMBERS)
22. from random import randint
num=[randint(100,999) for i in range(10)]
27. ALL=[]
l1=len(first)
l2=len(second)
i=j=0
while i<l1 and j<l2:
ALL.append(second[j])
ALL.append(first[i])
i+=1
j+=1
while i<l1:
ALL.append(first[i])
i+=1
while j<l2:
ALL.append(second[j])
j+=1
34. list=[]
option=None
while (option != 0):
print("1. Append an element")
print("2. Remove an element from the list")
print("3. Remove all elements from the list")
print("4. Count the occurances of an element")
print("5. Sort the list and display it")
print("6. Reverse the list")
print("7. Display the list")
print("0. Exit")
option=int(input("Enter your option: "))
if option==1:
ele=input("Enter the element to append: ")
list.append(ele)
elif option==2:
ele=input("Enter the element to delete: ")
if ele in list:
list.remove(ele)
print(ele,"deleted from the list")
else: print(ele,"not is not present in the list")
elif option==3:
list.clear()
elif option==4:
ele=input("Enter the element: ")
n=list.count(ele)
print(ele,"appears",n,"times in the list")
elif option==5:
list.sort();
print("The sorted list is: ")
print(list)
elif option==6:
list.reverse()
print("List reversed")
elif option==7:
print("The list is: ")
print(list)
35. list=[]
option=None
while (option != 0):
print("------------------------------------------------")
print("1. Insert an element in the list")
print("2. Remove an element from a location in the list")
print("3. Find the location of an element in the list")
© Yogesh Kumar Mandaan – (965)96891262)
QB/Python-XI/Lists/YK
36. n=0
for i in nums:
n+=1
found=-1
for i in range(n):
if ele==nums[i]:
found=i
break
© Yogesh Kumar Mandaan – (965)96891262)
QB/Python-XI/Lists/YK
if found==-1:
print("Element not found in the list")
else: print("Element found at location",found,"in the list")
37. student=[None]*3
n=int(input("Enter the number of students: "))
data=[None]*n
print("Now enter students data one by one: ")
for i in range(n):
print("Students number",i+1,":")
student[0]=int(input("Roll number: "))
student[1]=input("Name: ")
student[2]=float(input("Marks: "))
data[i]=student.copy()
print("List of students scoring marks more than 75: ")
for i in range(n):
if data[i][2]>75:
print(data[i])
38. units=['','One','Two','Three','Four','Five','Six','Seven','Eight','
Nine',
'Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
'Seventeen','Eighteen','Ninteen']
tens=['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Ei
ghty',
'Ninety']
n=int(input("Enter a two digit number: "))
if n not in range(10,100):
print("Number out of range")
else:
if (n<20):
print(units[n])
else:
units_digit=n%10
tens_digit=n//10
print(tens[tens_digit],units[units_digit])
39. roll=[]
first=91041; last=91090
stop = 0
while not stop:
print()
print("1. Student Enters")
print("2. Student Exits")
print("3. Student Count")
print("4. Smallest and Largest Roll number present")
print("5. Absentees' list")
print("6. Sorted list of roll numbers")
print("0. Exit")
option = int(input("Enter your option"))
if (option==0):
stop=1
if option==1:
r = int(input("Enter roll number"))
if (r<first or r>last):
print("Roll number out of range")
elif r in roll:
print("Roll number already present")
else: roll.append(r)
elif option==2:
r = int(input("Enter roll number"))
if r in roll:
roll.remove(r)
else:
© Yogesh Kumar Mandaan – (965)96891262)
QB/Python-XI/Lists/YK