Loops in Python
i). In Python, iterative statements keep repeating a set of statements as
long as the given condition is true.
ii). Iterative statements are also known as loops.
iii). There are two types of Iterative statements: for and while loop.
iv). for loop is generally used when you have to repeat a task a specified
number of times or within a given range.
v). The in operator checks whether a given value is a part of the specified
sequence.
vi). The range() function produces a list of a sequence starting with an initial
value up to a final value, updating the value at each step. By default, the
value is incremented by 1.
vii). The while loop can be applied to a program where the number of
iterations is not known beforehand. The while loop keeps on executing
the block of statement as long as the specified test condition evaluates to
true.
viii). The while loop has four main components: Initialisation, Condition, Loop
body, and Step value.
A. Multiple Choice Questions:
i). Iterative statement is also known as.
a. Looping b. Conditional c. Selection
ii). The ________________ loop is used when you are sure about how
many times a loop body will be executed.
a. while b. for c. while...else
iii). If the condition in a loop is false in the first step itself, you get
a. No output b. Infinite c. Error
iv). The ________ operator checks whether a given value lies within a
given set of values.
a. in b. between c. range
v). The………………..loop can be applied to a program where the number
of iterations is not known beforehand.
a. while b. for c. for...not...else
B. Answer in one word:
1. Mention the other name for iterative statements. looping
2. Name the two types of iterative statements. For loop, While
loop
3. Name the two membership operators in Python. in & not in
4. Which function is used to check the range in a loop? Range()
5. Which operator checks whether a given value is a in (membership
part of the specified sequence. operator)
1. Rewrite the following code of statements after correcting the syntactical
errors.
a=integer(input("First number")) a=integer(input("First number"))
b=10 b=10
for i in range(a:b): for i in range(a,b):
PRINT(a*b) print(a*b)
if (a>b) then if (a>b) :
print(a) print(a)
i=10 i=10
while (i=>5): while(i>=5):
print(I) print(i)
i=1+1: i=1+1
2. Rewrite the following for loop program using the while loop.
a=int(input("Enter the first number")) a=int(input("Enter the first number"))
b=int(input("Enter the second b=int(input("Enter the second
number")) number"))
for i in range(a,b,3): i=a
print(a*i) while(i<b):
print("Program Over") print(a*i)
i=i+3
print("Program Over")
3. Rewrite the following code of programs using the for loop.
a=int(input("Enter a number")) a=int(input("Enter a number"))
i=a i=a
while i>=0: for i in range(a,1,-2):
print(a*i) print(a*i)
i=i-2
x=10 x = 10
y=100 y =100
if x==y: if x==y:
print(x) print(x)
elif y > x: elif y > x:
print(y) print(y)
while x<10: for x in range (10,10):
print(x) print(x)
X+=1
4. Write the output for the following code of statements.
a=100 100
b=20
s=0
while a>=b:
if a%5==0:
s+=a
a=1
print(s)
n=1 4
while n<5: 9
n+=1 16
print(n*n) 25
n=10 20
while n<50: 40
print(n+n) 60
n+=10 80
.
5. Write the program code for the following:
a. To print the table of a number using 'for' loop
Ans. n = int(input("Enter a number: "))
print("Table of number : ", n, " is")
for i in range(1,11,1):
print(n," * ", i , " = ", n * i)
b. To print the sum of all the even numbers in the range entered by
the user
Ans. n1 = int(input("Enter first number: "))
n2= int(input("Enter second number: "))
s=0
for i in range(n1,n2,1):
if (i%2==0):
s= s+i
print ("sum of all even no between ", n1, " and ", n2, " = " ,s)
c. To print the odd numbers between 1 to 30
Ans. for i in range(1,30):
if (i%2):
print(i)
d. To print the first 10 Fibonacci numbers
Ans. n1=1
n2=1
t=0
print(n1)
print(n2)
for i in range(2,10):
t=n1 + n2
n1= n2
n2=t
print(t)
e. Write a program to find square of a number range
Ans. n1 = int(input("Enter first number: "))
n2= int(input("Enter second number: "))
for i in range(n1,n2,1):
print ("square of ",i, " = " ,i * i)
f. Write a program to find factorial of a number.
Ans. n = int(input("Enter a number: "))
fact=1
for i in range(1,n+1):
fact= fact* i
print ("Factorial on ", n, " = " , fact)
G Write a program to find compound interest
Ans. p= int(input("enter principle amount: "))
r =int(input("enter rate of interest: "))
t = int(input("enter time period : "))
amt=0
for i in range(1,t+1):
amt= p + (p*r/100)
print ("amount after " ,i," year :" ,amt)
p= amt
C. Answer the following:
1. What do you mean by iterative statements? Give an example.
Ans. The Statements that keep repeating themselves as long as a given
Condition is true are called Iterative statements or Repetitive
Statements.
In iterative statement is based on three value:
A start value (initial value), e.g., C=0.
A test condition, e.g., C<=6
A step value (increment or decrement), eg., C=C+1)
2. How is the “for” loop different from the while loop?
For loop While Loop
The for loop is used when you The while loop is the simplest of all
are sure about how many lapping structures. In general, this
times a loop body will Be loop can be applied to a program
executed. It also known as where the number of iterations is
definite loop. not known beforehand. The while
loop keeps on executing the block
of statement as long as the
specified test condition evaluates to
true.
Syntax: Syntax:
for variable in -sequences: Initialisation
loop body (statements to while <condition>:
be executed) loop Body
upation of loop variable
For Loop i=0
For I in range(0,10): while(i<=10):
print(i) print(i)
i=i+1
3. What is the use of membership operators in Python?
Ans. Membership operators play an important role in controlling the
working of a loop. There are two membership operators, in and not in
Out of these, the in operator plays a significant side in the working of
loops
The in Operator
The in Operator is used to check if a given value exist in the sequence
or not. It evaluates to true if it finds a value in the specified sequence
else it returns false.
4 What is infinite loop? Give example.
Ans. An infinite loop (or endless loop) is a sequence of instructions in a
computer programs, which looks endlessly. It happens either due to
the loop having no terminating condition or having the condition that
can never be met.
In the program, as per the loop condition, the loop will continue until
the value of i is greater than or equal to 1.
Ans. An infinite loop (or endless loop) is a sequence of instructions in a
computer programs, which looks endlessly. It happens either due to
the loop having no terminating condition or having the condition that
can never be met.
In the program, as per the loop condition, the loop will continue until
the value of i is greater than or equal to 1.
Print 1 to n
n = int(input("enter a number: "))
for agrima in range (1,n+1):
print( agrima)
print n to 1
n = int(input("enter a number: "))
for agrima in range (n,0,-1):
print( agrima)