Iterative statements: for loop, range function, while loop, flowcharts, break and continue statements, nested loops, suggested programs: generating pattern, summation of series, finding the factorial of a positive number etc
Looping (Iterative) statements :
1.These statements are used to repeat a code of block for given condition.
2.The iteration statements are called loops or looping statements.
3.Every time when loop body is executed called an iteration.
4.Python support two looping statements for and while.
5.Both are entry control loop
6.Python does not offer exit control loop
7.Looping statements have optional else clause in python. Else block is executed if loop body terminated normally.
8.break statement can be used to stop a for loop. In such case, the else part is ignored.
else block will be executed only when Loop terminated normally. If loop terminated with break statement then else block will not executed
In Python, break and continue statements can alter the flow of a normal loop.
break : statement is used to exit from Loop block
L=[1,2,3,4,5,6,7,8,9,10]
for i in L:
print(i)
if(i==5):
break
Output
1 2 3 4 5
continue
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped for the current iteration and the next iteration of the loop will begin
for i in range(1,11,1):
if(i<=5):
continue
print(i)
range() Function
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Syntax
range(start, stop, step)
start and step is optional but stop is required. step value is by default 1 and start value is by default 0
e.g
for i in range(1,6):
print(i)
Output 1 2 3 4 5
Statement Output
range(3, 20, 2) 3 5 7 9 11 13 15 17 19
range(5,10) 5,6,7,8,9
range(9,3,-1) 9 8 7 6 5 4
range(10,1,-2) 10 8 6 4 2
#Write a program to find the sum of all digits of a given number.
n=int(input("enter a numbr"))
s=str(n)
sum=0
for i in s:
sum=sum+int(i)
print(sum)
Write a program to find out the reverse of a given number.
n=int(input("enter a number"))
s=str(n)
str = ""
for i in s:
str = i + str
n=int(str)
print(n)
WAP to print triangle :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for i in range(6):
for j in range(i):
print(j+1,end=" ")
print()
WAP to print triangle :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for i in range(6):
for j in range(i):
print(i,end=" ")
print()
WAP to print triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
k=1
for i in range(6):
for j in range(i):
print(k,end=" ")
k=k+1
print()
WAP to print triangle
*
* *
* * *
* * * *
* * * * *
k=‘*’
for i in range(6):
for j in range(i):
print(k,end=" ")
print()
#WAP to enter a string and check it is palindrome or not
s=input("enter a string")
str = ""
for i in s:
str = i + str
if(s==str):
print("Palindrome")
else:
print("Not Palindrome")
#WAP to calculate the length of a string
s='COMPUTER SCIENCE '
c = 0
for i in s:
c += 1
print(c)
Write Python script to print the following pattern :
1
1 3
1 3 5
1 3 5 7
ans.
for a in range(3, 10, 2) :
for b in range(1, a, 2) :
print (b, end = ‘ ‘)
print()
# WAP to print 1 to 10 natural numbers
1 2 3 4 5 6 7 8 9 10
for i in range(1,11,1):
print(i)
# WAP to print table of given number
n=int(input("Enter a number"))
for i in range(1,11,1):
print(i*n)
Write a program to print the following using a single loop (no nested loops)
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
n = 1
for a in range(5) :
print(n)
n = n*10+1
# program to print
Factorial Using while loop
num = int(input("enter a number: "))
fac = 1
i = 1
while i <= num:
fac = fac * i
i = i + 1
print("factorial of ", num, " is ", fac)
Factorial using for loop
num = int(input("enter a number: "))
fac = 1
for i in range(1, num + 1):
fac = fac * i
print("factorial of ", num, " is ", fac)