0% found this document useful (0 votes)
4 views11 pages

Revision - Python

The document contains a series of practice questions focused on loops in Python, including tasks to print sequences, calculate sums, and determine properties of numbers. Each question is followed by a sample answer demonstrating the expected output or code implementation. The document is structured into four sections, progressively increasing in complexity and covering various loop scenarios.

Uploaded by

anotheronez26669
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Revision - Python

The document contains a series of practice questions focused on loops in Python, including tasks to print sequences, calculate sums, and determine properties of numbers. Each question is followed by a sample answer demonstrating the expected output or code implementation. The document is structured into four sections, progressively increasing in complexity and covering various loop scenarios.

Uploaded by

anotheronez26669
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practice Questions of Loops in Python - 1

Q1. Write the output of the following:


1. for i in "Myblog":
print (i, '?')
Answer

2. for i in range(5):
print(i)
Answer
0
1
2
3
4

3. for i in range(10,15):
print(i)

Answer
Ans.
10
11
12
13
14

Q2. Write a program to print first 10 natural number.


Answer
Ans.
for i in range(1,11):
print(i)

Q3. Write a program to print first 10 even numbers.


Answer
Ans.
for i in range(2,22,2):
print(i)

Q4. Write a program to print first 10 odd numbers.


Answer
Ans.
for i in range(1,21,2):
print(i)

Q5. Write a program to print first 10 even numbers in reverse order.


Answer
Ans.
for i in range(20,0,-2):
print(i)

Q6. Write a program to print table of a number accepted from user.


Answer
Ans.
num = int(input("Enter any number")
for i in range(1,11):
print(num*i)

Q7. Write a program to display product of the digits of a number


accepted from the user.
Answer

num=int(input("Enter any number")) num = int(input("Enter any number: "))


p=1 p=1
while(num): for i in range(len(str(num))):
r=num%10 r = num % 10
p=p*r p=p*r
num=num//10 num = num // 10
print("Product of digits is",p)
print("Product of digits is", p)

Q8. Write a program to find the factorial of a number.


Answer
num=int(input("Enter any number"))
f=1
for i in range(1,num+1):
f=f*i
print("Factorial is",f)

Q9. Write a program to find the sum of the digits of a number accepted
from user
Answer
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)

Q10. Write a program to check whether a number is prime or not.


Answer
num = int(input("Enter a number: "))

if num <= 1:
print("Number is not prime")
else:
for i in range(2, num):
if num % i == 0:
print("Number is not prime")
break
else:
print("Number is prime")

Practice Questions of Loops in Python - 2


Q1. Write the output of the following

1. for i in (1,10):
print(i)
Answer
1
10
2. for i in (5,9):
print(i)
Answer
5
9

3. for i in range(2,7):
print(i)
Answer
2
3
4
5
6

4. for i in "learning":
print(i)
Answer
l
e
a
r
n
i
n
g

5. for i in "python":
print(i, end=' ')
Answer
python

6. for i in "python":
print(i, end='?')
Answer
p?y?t?h?o?n?

7. for i in "python":
print(i, '?$')
Answer
p?$
y?$
t?$
h?$
o?$
n?$

8. for i in (1,2,3,4):
print(i)
Answer
1
2
3
4

9. for i in (3,4,7):
print(i)
Answer
3
4
7

10. for i in range(2,10,2):


print(i)
Answer
2
4
6
8

Practice Questions of Loops in Python — 3

Q1. Write the output of the following code :


[1]
x=5
while(x<15):
print(x**2)
x+=3

Answer
25
64
121
196

[2]
a=7
b=5
while(a<9):
print(a+b)
a+=1

Answer
12
13

[3]:
b=5
while(b<9):
print("H")
b+=1

Answer
H
H
H
H

[4]:
b=15
while(b>9):
print("Hello")
b=b-2

Answer
Hello
Hello
Hello

[5]:
x=15
while(x==15):
print("Hello")
x=x-3
Answer
Hello

[6]:
x = "123"
for i in x:
print("a")
Answer
a
a
a

[7]:
i=9
while True:
if i%3==0:
break
print("A")
Answer

Loop will not execute

[8]:
a=6
while(a<=10):
print("a")
a+=1
Answer
a
a
a
a
a
[9]:
i=0
while i<3:
print(i)
i=i+1
else:
print(7)
Answer
0
1
2
7
[10]:

i=0
while i<3:
print(i)
i=i+1
print(0)
Answer
0
0
1
0
2
0

[11]:
i=2
for x in range(i):
i+=1
print(i)
print(i)
Answer
3
3
4
4

[12]:
i=2
for x in range(i):
x+=1
print(x)
print(x)
Answer
1
2
2
[13]:

i=2
for x in range(i):
x+=1
print(x)
print("x")
Answer
1
x
2
x
[14]:

i=100
while i<57:
print(i)
i+=5
Answer

Loop will run infinite times

Practice Questions of Loops in Python — 4


Q1. Write program to print the following pattern.
a)
1
12
123
1234

b)
****
***
**
*
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()

Q2. Accept 10 numbers from the user and display their average.
Answer

s=0

for i in range(10):

n=int(input("Enter number"))

s=s+n

print("Average of 10 numbers is ",s/10)

Q3. Write a program to print all prime numbers that fall between two
numbers including both (accept two numbers from the user)

Answer

start = int(input("Enter first number: "))

end = int(input("Enter second number: "))

for num in range(start, end + 1):

if num > 1:

for i in range(2, num):

if num % i == 0:

break

else:

print(num, end=" ")

Q4. Write a program to display sum of odd numbers and even numbers
that fall between 12 and 37(including both numbers)
Answer

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)

Q5. Write a program to display all the numbers which are divisible by
11 but not by 2 between 100 and 500.
Answer

for i in range(100,500):

if i%11==0 and i%2!=0:

print(i)

Q6. How many times the following loop execute?


c=0
while c < 20:
c += 2
Answer
Ans. The loop will run 10 times
Q7. Write the output of the following.
c = -9
while c < 20:
c += 3
print(c)
Answer

-6
-3
0
3
6
9
12
15
18
21
Q8. Write a program to print numbers from 1 to 20 except multiple of 2
& 3.
Answer

for i in range(1,21):
if i%2!=0 and i%3!=0:
print(i)

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
Answer

num = int(input("Enter any number"))


for i in range(1,11):
print(num,"*",i,"=",num*i)

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.
Answer

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)

You might also like