ADHARSH VIDHYALAYA PUBLIC SCHOOL
Sub-Topic : LIST MANIPULATION
Name of Student : ________________________________________
1. What will be the output of following code:
L=[1,2]
print (L * 2)
a) [1, 2, 1, 2]
b) [1, 2, 2,1]
c) [1, 2, 2,2]
d) [1, 1, 2, 2]
2. What will be the output of following code:
a=[[[1,2],[3,4],5],[6,7]]
print(a[0][1][1])
a) 3
b)4
c) 6
d)7
3. Predict the output of code segment:
a=[2,1,3]
a.remove(2)
print(a)
a) [1, 3]
b) [1, 2]
c) [2, 3]
d) [3, 1]
4. What will be the output of following program:
L=[1, 2, [1.1, 2.2]]
print(len(L))
a)5
b)4
c) 3
d)None of these
5. What will be the output of
following program:
p = [2 ** x for x in range(5) if x
> 2]
print( p)
a)[8,16] b)[24,16] c)[16,24] d)[24,32]
6. What will be the output of following program:
r = [x for x in range(5) if x % 2 == 1]
print(r)
a)[3,1]
b)[1,3]
c)[1,1]
d)[3,3]
7 Write the output of code given for slicing:
L=['I','N','D','I','A']
print(L[2:])
a) ['D', 'I', 'A']
b) ['D', 'I']
c) [ 'I', 'A']
d) ['I', 'N', 'D']
8 Write the output of code given for slicing:
L=['I','N','D','I','A']
print(L[-2:])
a) ['I', 'I']
b) ['I', 'A']
c) ['A', 'I']
d) ['A', 'A']
9. What is the possible output of the given code
segment:
L=[1,2,3,4,5]
L.pop()
print(L)
a) [1, 2, 3, 4]
b) [ 2, 3, 4,5]
c) [1, 2, 3, 5]
d) None of these
10. Try to find the output of the given code segment:
L=[1,2,30,4,5]
r=max(L)
print(r)
a) 5
b) 4
c) 30
d) 1
1. Write the python code to create the list using input from user values:
A = []
n = int(input("Enter Number of Elements You want to Input : "))
# iterating till the range
for i in range(0, n):
V = int(input("Enter Data Elements : "))
A.append(V)
print(A)
Output:
Enter Number of Elements You want to Input : 3
Enter Data Elements : 11
Enter Data Elements : 22
Enter Data Elements : 33
[11, 22, 33]
2. Write the code to take two lists and check them either identical or not:
T1 = [1, 2, 4, 3, 5]
T2 = [1, 2, 5, 4, 3]
# sorting both the lists
T1.sort()
T2.sort()
# using == to check if lists are equal
if T1 == T2:
print ("The lists are identical")
else :
print ("The lists are not identical")
Output:-
The lists are identical
3. Write the python code for linear search.
T1 = [1, 2, 4, 3, 5]
f=0
N=int(input("Enter Number u want to search : "))
for c in T1:
if c==N:
f=1
print("Search Found ")
break
if f==0:
print("Search Not found")
Output:
Enter Number u want to search : 3
Search Found
4. Program to Find Even elements in a List:
a = [1, 4, 20, 16, 25, 36, 51, 100, 150]
evenlst = []
for num in a:
if num % 2 == 0:
evenlst.append(num)
print("The New List of Even Numbers is", evenlst)
Output:
The New List of Even Numbers is [4, 20, 16, 36, 100, 150]
5. WAP to display the frequency of each item in a list:
L=[10,13,14,17,10,13,15,25,27,24]
L1=[]
L2=[]
for i in L:
if i not in L2:
c=L.count(i)
L1.append(c)
L2.append(i)
print(' ')
print('Item','\t\t','frequency')
for i in range(len(L1)):
print(L2[i],'\t\t',L1[i])
Output:
Item frequency
10 2
13 2
14 1
17 1
15 1
25 1
27 1
24 1
1. Write the python code to input a list from user and find the sum of even numbers
only.
e.g. [10,20,25,30,45]
Result : 60
2. Input a list of integer numbers from user and find the square of even numbers
and cube of odd numbers.
e.g. [2,3,4,5,6]
Result: 4,27,64,125,36
3. Write the python code to swap the list numbers in given manner:
e.g. [10,20,30,40,50]
Result: [50,40,30,20,10]
4. Write the python program to input a list from user and print only those values
which are divisible by 3 and 5 both.
e.g. [15,25,30,18,20]
Result: 15,30
5. Write the python program to input a list from user and swap the values in the
given manner:
e.g.[10,20,30,40,50,60]
Result: [20,10,40,30,60,50]