
Practice Questions of Loops in Python — Test 1
Q1. Write the output of the following:
1. for i in "Myblog":
print (i, '?')
[showhide type="links1" more_text="Show Answer" less_text="Hide Answer"]Ans.
M ?
y ?
b ?
l ?
o ?
g ?[/showhide]
2. for i in range(5):
print(i)
[showhide type="links2" more_text="Show Answer" less_text="Hide Answer"]Ans.
0
1
2
3
4[/showhide]
3. for i in range(10,15):
print(i)
[showhide type="links3" more_text="Show Answer" less_text="Hide Answer"]Ans.
10
11
12
13
14[/showhide]
Q2. Write a program to print first 10 natural number.
[showhide type="links4" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(1,11):
print(i)[/showhide]
Q3. Write a program to print first 10 even numbers.
[showhide type="links5" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(2,22,2):
print(i)[/showhide]
Q4. Write a program to print first 10 odd numbers.
[showhide type="links6" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(1,21,2):
print(i)[/showhide]
Q5. Write a program to print first 10 even numbers in reverse order.
[showhide type="links7" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(20,0,-2):
print(i)[/showhide]
Q6. Write a program to print table of a number accepted from user.
[showhide type="links8" more_text="Show Answer" less_text="Hide Answer"]Ans.
num = int(input("Enter any number")
for i in range(1,11):
print(num*i)[/showhide]
Q7. Write a program to display product of the digits of a number accepted from the user.
[showhide type="links9" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
p=1
while(num):
r=num%10
p=p*r
num=num//10
print("Product of digits is",p)[/showhide]
Q8. Write a program to find the factorial of a number.
[showhide type="links10" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
f=1
for i in range(1,num+1):
f=f*i
print("Factorial is",f)[/showhide]
Q9. Write a program to find the sum of the digits of a number accepted from user
[showhide type="links11" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
s=0
while(num):
r=num%10
s=s+r
num=num//10
print("Sum of digits is",s)[/showhide]
Q10. Write a program to check whether a number is prime or not.
[showhide type="links12" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
f=0
if num==1 or num==0:
f=1
for i in range(2,num):
if num%i==0:
f=1
if f==1:
print("Number is not prime")
else:
print("Number is prime")[/showhide]
Practice Questions of Loops in Python — Test 2
Q1. Write the output of the following
1. for i in (1,10):
print(i)
[showhide type="links13" more_text="Show Answer" less_text="Hide Answer"]Ans
1
10[/showhide]
2. for i in (5,9):
print(i)
[showhide type="links14" more_text="Show Answer" less_text="Hide Answer"]Ans.
5
9[/showhide]
3. for i in range(2,7):
print(i)
[showhide type="links15" more_text="Show Answer" less_text="Hide Answer"]Ans.
2
3
4
5
6[/showhide]
4. for i in "csiplearninghub":
print(i)
[showhide type="links16" more_text="Show Answer" less_text="Hide Answer"]Ans.
c
s
i
p
l
e
a
r
n
i
n
g
h
u
b[/showhide]
5. for i in "python":
print(i, end=' ')
[showhide type="links17" more_text="Show Answer" less_text="Hide Answer"]Ans.
p y t h o n[/showhide]
6. for i in "python":
print(i, end=='?')
[showhide type="links18" more_text="Show Answer" less_text="Hide Answer"]Ans.
p?y?t?h?o?n?[/showhide]
7. for i in "python":
print(i, '?$')
[showhide type="links19" more_text="Show Answer" less_text="Hide Answer"]Ans.
p?$y?$t?$h?$o?$n?$[/showhide]
8. for i in (1,2,3,4):
print(i)
[showhide type="links20" more_text="Show Answer" less_text="Hide Answer"]Ans.
1
2
3
4[/showhide]
9. for i in (3,4,7):
print(i)
[showhide type="links21" more_text="Show Answer" less_text="Hide Answer"]Ans.
3
4
7
[/showhide]
10. for i in range(2,10,2):
print(i)
[showhide type="links22" more_text="Show Answer" less_text="Hide Answer"]Ans
2
4
6
8[/showhide]
Practice Questions of Loops in Python — Test 3
Q1. Write the output of the following code :
x=5
while(x<15):
print(x**2)
x+=3
[showhide type="links23" more_text="Show Answer" less_text="Hide Answer"]
25
64
121
196
[/showhide]
a=7
b=5
while(a<9):
print(a+b)
a+=1
[showhide type="links24" more_text="Show Answer" less_text="Hide Answer"]
12
13
[/showhide]
b=5
while(b<9):
print("H")
b+=1
[showhide type="links25" more_text="Show Answer" less_text="Hide Answer"]
H
H
H
H
[/showhide]
b=15
while(b>9):
print("Hello")
b=b-2
[showhide type="links26" more_text="Show Answer" less_text="Hide Answer"]Hello
Hello
Hello[/showhide]
x=15
while(x==15):
print("Hello")
x=x-3
x = "123"
for i in x:
print("a")[showhide type="links28" more_text="Show Answer" less_text="Hide Answer"]a
a
a[/showhide]
i=9
while True:
if i%3==0:
break
print("A")[showhide type="links29" more_text="Show Answer" less_text="Hide Answer"]
Loop will not execute[/showhide]
a=6
while(a<=10):
print("a")
a+=1 [showhide type="links30" more_text="Show Answer" less_text="Hide Answer"]a
a
a
a
a[/showhide]
i=0
while i<3:
print(i)
i=i+1
else:
print(7)[showhide type="links31" more_text="Show Answer" less_text="Hide Answer"]0
1
2
7[/showhide]
i=0
while i<3:
print(i)
i=i+1
print(0)[showhide type="links32" more_text="Show Answer" less_text="Hide Answer"]0
0
1
0
2
0[/showhide]
i=2
for x in range(i):
i+=1
print(i)
print(i)[showhide type="links33" more_text="Show Answer" less_text="Hide Answer"]3
3
4
4[/showhide]
i=2
for x in range(i):
x+=1
print(x)
print(x)[showhide type="links34" more_text="Show Answer" less_text="Hide Answer"]1
2
2[/showhide]
i=2
for x in range(i):
x+=1
print(x)
print("x")[showhide type="links35" more_text="Show Answer" less_text="Hide Answer"]1
x
2
x[/showhide]
i=100
while i<57:
print(i)
i+=5[showhide type="links36" more_text="Show Answer" less_text="Hide Answer"]
Loop will run infinite times[/showhide]
Practice Questions of Loops in Python — Test 4
Q1. Write program to print the following pattern.
a)
1
1 2
1 2 3
1 2 3 4
b)
* * * *
* * *
* *
*
[showhide type="links41" more_text="Show Answer" less_text="Hide Answer"]Ans a)
for i in range(1,5):
for j in range(1,i+1):
print(j,end=" ")
print()
Ans b)
for i in range(4,0,-1):
for j in range(i):
print("*",end=" ")
print()[/showhide]
Q2. Accept 10 numbers from the user and display their average.
[showhide type="links42" more_text="Show Answer" less_text="Hide Answer"]Ans.
s=0
for i in range(10):
n=int(input("Enter number"))
s=s+n
print("Average of 10 numbers is ",s/10)[/showhide]
Q3. Write a program to print all prime numbers that fall between two numbers including both(accept two numbers from the user)
[showhide type="links43" more_text="Show Answer" less_text="Hide Answer"]Ans.
def primn(num):
f=0
if num==1 or num==0:
f=1
for i in range(2,num):
if num%i==0:
f=1
if f==1:
return 'n'
else:
return 'y'
num1=int(input("Enter first number"))
num2=int(input("Enter Second number"))
if num1>num2:
for i in range(num2,num1+1):
r=primn(i)
if r=='y':
print(i)
else:
for i in range(num1,num2+1):
r=primn(i)
if r=='y':
print(i)[/showhide]
Q4. Write a program to display sum of odd numbers and even numbers that fall between 12 and 37(including both numbers)
[showhide type="links44" more_text="Show Answer" less_text="Hide Answer"]Ans:
so=0
se=0
for i in range(12,38):
if i % 2==0:
se=se+i
else:
so=so+i
print("Sum of all even numbers is ",se)
print("Sum of all odd numbers is ",so)[/showhide]
Q5. Write a program to display all the numbers which are divisible by 11 but not by 2 between 100 and 500.
[showhide type="links45" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(100,500):
if i%11==0 and i%2!=0:
print(i)[/showhide]
Q6. How many times the following loop execute?
c = 0
while c < 20:
c += 2
[showhide type=”links46″ more_text=”Show Answer” less_text=”Hide Answer”]Ans. The loop will run 10 times[/showhide]
Q7. Write the output of the following.
c = -9
while c < 20:
c += 3
print(c)
[showhide type="links47" more_text="Show Answer" less_text="Hide Answer"]Ans.
-6
-3
0
3
6
9
12
15
18
21[/showhide]
Q8. Write a program to print numbers from 1 to 20 except multiple of 2 & 3.
[showhide type="links48" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(1,21):
if i%2!=0 and i%3!=0:
print(i)[/showhide]
Q9. Write a program to print table of a number(accepted from user) in the following format.
Like : input number is 7, so expected output is
7 * 1 = 7
7 * 2 = 14 and so on
[showhide type="links49" more_text="Show Answer" less_text="Hide Answer"]Ans.
num = int(input("Enter any number"))
for i in range(1,11):
print(num,"*",i,"=",num*i)[/showhide]
Q10. Write a program that keep on accepting number from the user until user enters Zero. Display the sum and average of all the numbers.
[showhide type="links50" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=1
i=-1
s=0
while(num!=0):
num=int(input("Enter any number"))
s=s+num
i=i+1
print("Average of numbers entered by you is ",s/i)[/showhide]
Practice Questions of Loops in Python — Test 5
Q1. Write a program to print the following pattern
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
[showhide type="links51" more_text="Show Answer" less_text="Hide Answer"]Ans.
for i in range(5,0,-1):
for j in range(1,i+1):
print(i,end=" ")
print()[/showhide]
Q2. Find errors in the following code:
a = int(“Enter any number”)
for i in Range[2,6]
if a=i
print(“A”)
else
print(“B”)
[showhide type="links52" more_text="Show Answer" less_text="Hide Answer"]Ans.
a = int(input(“Enter any number”))
for i in range(2,6):
if a==i:
print(“A”)
else:
print(“B”)[/showhide]
Q3. Write a program to accept decimal number and display its binary number.
[showhide type="links53" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
l=[]
while(num):
r=num%2
l.append(r)
num=num//2
for i in range(len(l)-1,-1,-1):
print(l[i],end=" ")[/showhide]
Q4. Accept a number and check whether it is palindrome or not.
[showhide type=”links54″ more_text=”Show Answer” less_text=”Hide Answer”]Ans.
num=int(input("Enter any number"))
ornum=num
rn=0
while(num):
r=num%10
rn=rn*10+r
num=num//10
if ornum==rn:
print("Number is Palindrome")
else:
print("Number is not a Palindrome")[/showhide]
Q5. Write a program to accept a number and check whether it is a perfect number or not.
(Perfect number is a positive integer which is equal to the sum of its divisors like divisors of 6 are 1,2,3, and
sum of divisors is also 6, so 6 is the perfect number)
[showhide type="links55" more_text="Show Answer" less_text="Hide Answer"]Ans.
num=int(input("Enter any number"))
s=0
for i in range(1,num):
if num%i==0:
s=s+i
if num==s:
print("Number is perfect")
else:
print("Number is not perfect")[/showhide]
Q6. Write a program to find the sum of the following series(accept values of x and n from user)
1 + x/1! + x2/2! + ……….xn/n!
[showhide type="links56" more_text="Show Answer" less_text="Hide Answer"]Ans
import math
n=int(input("Enter number of terms"))
x=int(input("Enter the base"))
s=1.0
for i in range(1,n+1):
s=s+math.pow(x,i)/math.factorial(i)
print(s)[/showhide]
Q7. Write a program to print the following pattern
A
B C
D E F
G H I J
K L M N O
[showhide type="links57" more_text="Show Answer" less_text="Hide Answer"]Ans.
l=65
for i in range(1,6):
for j in range(1,i+1):
print(chr(l),end=" ")
l=l+1
print()
l=65[/showhide]
Q8. Write a program to find the sum of following (Accept values of a, r, n from user)
a + ar + ar2 + ar3 + ………..arn
Q9. Write the output of the following
for x in range(10,20):
if (x%2==0):
continue
print(x)
[showhide type="links59" more_text="Show Answer" less_text="Hide Answer"]Ans. No Output[/showhide]
Q10. Write a function to display prime numbers below any number accepted from the user.
[showhide type="links60" more_text="Show Answer" less_text="Hide Answer"]Ans.
def primn(num):
f=0
if num==1 or num==0:
f=1
for i in range(2,num):
if num%i==0:
f=1
if f==1:
return 'n'
else:
return 'y'
num=int(input("Enter number"))
for i in range(num):
r=primn(i)
if r=='y':
print(i)[/showhide]
Practice Questions of Loops in Python — Test 6
Q1. Write the output of the following.
a=5
while a>0:
print(a)
a=a-1
[showhide type="links61" more_text="Show Answer" less_text="Hide Answer"]Ans.
5
4
3
2
1[/showhide]
Q2. for loop statement is terminated by symbol ___________
[showhide type="links62" more_text="Show Answer" less_text="Hide Answer"]Ans. Semicolon(;)[/showhide]
Q3. What is the difference between break and continue statements?
[showhide type="links63" more_text="Show Answer" less_text="Hide Answer"]Ans. break statement terminates the loop as the condition matches. for example.
a=5
while a>0:
a=a-1
if a==3: #as this condition matches it terminates the loop
break
else:
print(a)
Output : 4
Continue statement is used to skip a particular condition. for example
a=5
while a>0:
a=a-1
if a==3: #as this condition matches it goes to the next condition of the loop
continue
else:
print(a)
Output :
4
2
1
0
[/showhide]
Q4. Convert the following loop into for loop :
x = 4
while(x<=8):
print(x*10)
x+=2
[showhide type="links64" more_text="Show Answer" less_text="Hide Answer"]Ans.
for x in range(4,9,2):
print(x*10)[/showhide]
Q5. Write the output of the following:
for k in range(10,20,4):
print(k)
[showhide type="links65" more_text="Show Answer" less_text="Hide Answer"]Ans.
10
14
18[/showhide]
Q6. Find errors in the following code:
x = input(“Enter value”)
for k in range[0,20]
if x=k
print(x+k)
else:
Print(x-k)
[showhide type="links66" more_text="Show Answer" less_text="Hide Answer"]Ans
x = input("Enter value")
for k in range(0,20):
if x==k:
print(x+k)
else:
print(x-k)[/showhide]
Q7. Write the output of the following:
x=3
if x>2 or x<5 and x==6:
print(“Bye”)
else:
print(“Thankyou”)
[showhide type=”links67″ more_text=”Show Answer” less_text=”Hide Answer”]Ans. Bye[/showhide]
Q8. Write the output of the following:
x,y=2,4
if(x+y==10):
print(“Thankyou”)
else:
print(“Bye”)
[showhide type=”links68″ more_text=”Show Answer” less_text=”Hide Answer”]Ans. Bye[/showhide]
Q9. Write the output of the following:
x=10
y=1
while x>y:
x=x-4
y=y+3
print(x)
[showhide type="links69" more_text="Show Answer" less_text="Hide Answer"]Ans.
6
2[/showhide]
Q10. Write the output of the following:
for x in range(3):
for y in range(2):
print(“Practice Questions of loops in python”)
print()
[showhide type="links70" more_text="Show Answer" less_text="Hide Answer"]Ans.
Practice Questions of loops in python
Practice Questions of loops in python
Practice Questions of loops in python
Practice Questions of loops in python
Practice Questions of loops in python
Practice Questions of loops in python[/showhide]
Practice Questions of Loops in Python — Test 7
Q1. What do you mean by jump statement?
Q2. What is nested loop?
Q3. Write a program to print the following pattern.
1 2 3 4
1 2 3
1 2
1
Q4. Write a program to print the following pattern.
A
B C
D E F
G H I J
Q5. Write a program to print the following pattern.
A A A A
A A A A
A A A A
A A A A
Q6. Write a program to convert temperature in Fahrenheit to Celsius.
Q7. Write a program to print the factorial of a number.
Q8. Write a program to find the sum of digits of a number.
Q9. Accept two numbers from the user and display sum of even numbers between them(including both)
Q10. Write a program to reverse a number.
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series




Q10. Write the output of the following if a = 9
if (a > 5 and a <=10):
print("Hello")
else:
print("Bye")
[showhide type="links20" more_text="Show Answer" less_text="Hide Answer"]Ans. Hello[/showhide]