ITERATIVE STATEMENTS IN PYTHON
DEFINITIONS:
Iterative statements: These statements are used to repeat the execution of a list of
statements, depending upon the value of an integer expression.
For loop: It is used when the user is sure about how many times the loop body will
be executed.
Membership operators: are operators used to validate the membership of a value.
It tests for membership in a sequence, such as strings, lists or tuples.
There are two membership operators – in and not in.
The ‘in’ operator used to check if a value exists in sequence or not. Evaluates to true
if it finds a variable in the specified sequence and false otherwise.
Eg: a=int(input(“enter the number:”))
print(a in[25,35,45,55,65,75])
The ‘not in’ operator checks for the value exists in a sequence and evaluates to
False else returns True.
Range function: It returns a sequence of numbers, starting from 0 by default and
increments by 1.
Eg: WAP to print first 10 natural numbers:
for i in range (1,11):
print(i)
Eg: WAP to print odd numbers between 15-27:
for i in range(15,27,2):
print(i)
Eg: WAP to print numbers ffrom 100 - 1
for i in range(100,1,-1):
print(i)
While Loop: It is used to repeat a series of instructions for a specified number of
times as long as the given condition evaluates to true.
EG: WAP to print all the odd numbers between 35 to 75
n=35
while n<=75:
print(n)
n=n+1
2. WAP to print the cubes of the user given number
n=int(input("Enter the number"))
y=int(input("Enter the number"))
while n<= y:
print(n*n*n)
n=n+1
Infinite Loop: It is a sequence of instructions in a computer program, which loops
endlessly.
i=20
while i>=1:
print(i)
i+=1