Professional Documents
Culture Documents
Python
Programming
Lists(Worksheet 1)
Grade: 11 Sub: Computer Science
(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]
av
[2, 3]
[1, 2, 55, 4]
[1, 2]
[1, 2, 55, 4]
[1, 2, 55, 4]
(e) L1=[1,2,3]
L2=list()
L3=[]
L4="CBSE"
print (type(L1),type(L2))
print (type(L3),type(L4))
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]