You are on page 1of 12

CBSE

Python
Programming

Lists(Worksheet 1)
Grade: 11 Sub: Computer Science

1. Find the output of the following:


(a) L1 = [1, 2, 3, 4]
print (L1[0]);print (L1[-4])
print (L1[1]); print (L1[-3])
print (L1[2]); print (L1[-2])
print (L1[3]); print (L1[-1])

(L1[0])=1 ; (L1[-4])=4
(L1[1])=2; (L1[-3])=3
(L1[2])=3; (L1[-2])=2
(L1[3])=4; (L1[-1])=1

(b) L1=[11,22,33,44,55]
print (L1)
L5=L1[:]
print (L5)
L6=L1[0:2]
print (L6)

(L1)=[11,22,33,44,55]
[11,22,33,44,55]
[11,22]

(c) A="Save Earth" #string


print (A[1:3])
L1 = [1, 2, 3, 4] #List
print (L1[1:3])
L1[2]=55
print (L1)
print(L1[0:2])
print(L1[:])
print (L1)
A[2]=100
print (A)

av
[2, 3]
[1, 2, 55, 4]
[1, 2]
[1, 2, 55, 4]
[1, 2, 55, 4]

(d) L1 = [11, 21, 32, 43]


L2 = ["Delhi", "Chennai", "Mumbai"]
L3 = [ ]
L4 = ["abc", 10, 20]
L5 = [1, 2, [6, 7, 8], 3]
print (L1)
L1 [2] = 55
L1[1]=100
print (L1)
print (L5[2][1])
print (L5)
print (L2[0])

[11, 21, 32, 43]


[11, 100, 55, 43]
7
[1, 2, [6, 7, 8], 3]
Delhi

(e) L1=[1,2,3]
L2=list()
L3=[]
L4="CBSE"
print (type(L1),type(L2))
print (type(L3),type(L4))

<class 'list'> <class 'list'>


<class 'list'> <class 'str'>

2. (a) Write a program to create a list in python which allows the user to enter
'n' numbers.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
print()

Ouput:
Enter Number:5
10
3
6
18
22
[10, 3, 6, 18, 22]

(b) Write a program to create a list in python which allows the user to enter
'n' strings.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=input(" ")
L.append(x)
print(L)
print()

Output:
Enter Number:3
bye
hi
hello
['bye', 'hi', 'hello']

(c) Write a program to create a list of ‘n’ numbers and display only even
numbers of the list.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
sum=0
L1=[]
for i in L:
if (i%2==0):
L1.append(i)
sum=i+sum
print(L1)
print("sum=",sum)
print()

Output:
Enter Number:5
44
7
9
9
28
[44, 7, 9, 9, 28]
[44, 28]
sum= 72

(d) Write a program to create a list of ‘n’ numbers and display the sum of
the odd numbers of the list.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
sum=0
for i in L:
if (i%2==1):
L1.append(i)
sum=i+sum
print(L1)
print("sum=",sum)
print()

Output:
Enter Number:5
13
16
17
20
21
[13, 16, 17, 20, 21]
[13, 17, 21]
sum= 51

(e) Write a program to create a list of ‘n’ numbers and display the numbers
divisible by 5.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
for i in L:
if (i%5==0):
L1.append(i)
print(L1)
print()

Output:
Enter Number:3
15
12
5
[15, 12, 5]
[15, 5]

(f) Write a program to create a list of ‘n’ numbers and display the numbers
divisible by 3 and 7 only.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
for i in L:
if (i%3==0 and i%7==0):
L1.append(i)
print(L1)
print()

Output:
Enter Number:5
3
16
14
21
84
[3, 16, 14, 21, 84]
[21, 84]

(g) Write a program to create a list of ‘n’ numbers and display the product of
the numbers divisible by 10.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
product=1
for i in L:
if (i%10==0):
L1.append(i)
product=i*product

print(L1)
print("product=",product)
print()

Output:
Enter Number:5
5
10
40
8
13
[5, 10, 40, 8, 13]
[10, 40]
product= 400

(f) Write a program to create a list of ‘n’ numbers and display the numbers
ending with 3.
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
for i in range (len(L)):
if (L[i]%10==3):
L1.append(L[i])
print(L1)
print()

Output:
Enter Number:5
23
65
33
12
5
[23, 65, 33, 12, 5]
[23, 33]

(g) Write a program to create a list of ‘n’ numbers and display the alternate
numbers of the list starting from the beginning of the list.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=[]
for i in range (len(L)):
if (i%2==0):
L1.append(L[i])
print(L1)
print()

Output:
Enter Number:5
3
5
6
4
8
[3, 5, 6, 4, 8]
[3, 6, 8]

(h) Write a program to create a list of ‘n’ numbers and display the first 3 and
last 3 numbers.
Input:
n=int(input("Enter Number:"))
L=[]
for i in range(n):
x=int(input(" "))
L.append(x)
print(L)
L1=L[:3]
L1.extend(L[-1:-4:-1])
print(L1)

Output:
Enter Number:9
22
42
83
74
23
65
29
17
13
[22, 42, 83, 74, 23, 65, 29, 17, 13]
[22, 42, 83, 13, 17, 29]

You might also like