The flow control statements are divided into three categories.
1 -- Conditional statements. 2 -- Iterative statements. 3 -- Transfer statements.
Iterative statements
In Python, iterative statements allow us to execute a block of code repeatedly as long as the condition is True. We also call it a loop statements.
Python provides us the following two loop statement to perform some actions repeatedly
1.for loop 2.while loop
Let’s learn each one of them with the examples
In [ ]:
In [ ]:
Using for loop, we can iterate any sequence or iterable variable. The sequence can be
string, list, dictionary, set, or tuple,range function.
In [ ]: # Syntax of for loop:
# for element in sequence:
# body of for loop
In [ ]:
Using the for loop we can repeat the block of statements a fixed number of times. Let’s understand this
with an example.
Fixed number of times: Print the multiplication table of 2. In this case, you know how many iterations you need. Here you need 10 iterations. In such a case use for loop.
In [1]: for i in range(1,10+1): # range() is iternations you need
print(i*2) # <---- block of statement
2
4
6
8
10
12
14
16
18
20
In [ ]:
range(start, stop,step)
In [2]: print(range(8))
range(0, 8)
In [ ]:
In [3]: a = range(6,9)
print(list(a))
[6, 7, 8]
In [ ]:
In [4]: print(list(range(8)))
[0, 1, 2, 3, 4, 5, 6, 7]
In [ ]:
In [5]: print(list(range(0,8)))
[0, 1, 2, 3, 4, 5, 6, 7]
In [ ]:
In [6]: print(list(range(0,10,2)))
[0, 2, 4, 6, 8]
In [ ]:
In [7]: # in operator
In [8]: a = 22
print(a in [2,34,5,6])
print(a)
False
22
In [ ]:
In [9]: for a in [2,34,5,6]:
print(a)
2
34
5
6
In [ ]:
In [10]: for a in range(1,11):
print(5*a)
5
10
15
20
25
30
35
40
45
50
In [ ]:
In [11]: for a in range(3):
print(a)
0
1
2
In [ ]:
write a program of multiplicaton of 17.
In [12]: for a in range(1 ,11):
print(a*17)
17
34
51
68
85
102
119
136
153
170
In [ ]:
In [ ]:
In [13]: for i in range(0,170,17):
print(i)
0
17
34
51
68
85
102
119
136
153
In [14]: for a in range(1,11):
print("17 *",a,"=",17*a)
17 * 1 = 17
17 * 2 = 34
17 * 3 = 51
17 * 4 = 68
17 * 5 = 85
17 * 6 = 102
17 * 7 = 119
17 * 8 = 136
17 * 9 = 153
17 * 10 = 170
In [ ]:
Iterative over sequence
In [16]: for i in "Kolkata":
print(i,end = " ")
K o l k a t a
In [ ]:
In [17]: a= [1,2,3,5]
for i in a:
print(i)
1
2
3
5
In [ ]:
In [18]: for i in (1,2,3,5):
print(i)
1
2
3
5
In [ ]:
In [19]: for i in {1,2,3,5}:
print(i)
1
2
3
5
In [ ]:
In [20]: for i in {"name":"raman","class":"python"}:
print(i)
name
class
In [ ]:
Square of given list item
In [21]: numbers = [1, 2, 3, 4, 5]
for i in numbers:
square = i ** 2
print("Square of:", i, "is:", square)
Square of: 1 is: 1
Square of: 2 is: 4
Square of: 3 is: 9
Square of: 4 is: 16
Square of: 5 is: 25
In [ ]:
print even and odd number in given range
In [22]: n = int(input("enter the number = "))
for i in range(1,n+1):
if i % 2 == 0:
print('Even Number:', i)
else:
print('Odd Number:', i)
enter the number = 9
Odd Number: 1
Even Number: 2
Odd Number: 3
Even Number: 4
Odd Number: 5
Even Number: 6
Odd Number: 7
Even Number: 8
Odd Number: 9
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Python Program for Printing Right Triangle Star Pattern
In [23]: for i in range(1,5+1):
print(i*"*",end="")
print()
*
**
***
****
*****
In [ ]:
Nested for loop
In [24]: n = int(input("enter the num of star patttern = "))
for i in range(1,n+1): # first complete the inner the loop..after that go back to outer loop
for j in range(i):
print("*",end=" ")
print()
enter the num of star patttern = 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
In [ ]:
In [25]: # 1
# 22
# 333
# 4444
# 55555
# print like this
In [26]: for i in range(1,6):
for j in range(i):
print(i,end=" ")
print()
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
In [ ]:
In [27]: # 1
# 12
# 123
# 1234
# 12345
# print like this
In [28]: for i in range(1,6):
for j in range(1,i+1):
print(j,end=" ")
print()
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
In [ ]:
In [29]: numbers = [10, 20]
items = ["Chair", "Table"]
for x in numbers:
for y in items:
print(x, y)
10 Chair
10 Table
20 Chair
20 Table
In [ ]:
Q.1
write a program to find sum from 1 to n.
In [35]: a=int(input("Enter a number"))
sum=0
for i in range(1,a+1):
sum=sum+i
print(sum)
Enter a number6
1
3
6
10
15
21
In [ ]:
In [31]: # 1+2+3...........n <== example
In [34]: num = int(input("enter the num = "))
summ = 0
for i in range(1,num+1):
summ = summ+i
print("sum of n number ",summ)
enter the num = 6
sum of n number 21
In [ ]:
In [ ]:
Q.2
write a program to find sum of square from 1 to n .
In [36]: n=int(input("enter no.= "))
sum=0
sqaure=1
for a in range(1,n+1):
square=a*a
sum+=square
print(sum)
enter no.= 6
91
In [ ]:
In [37]: num = int(input("enter the num = "))
summ = 0
for i in range(1,num+1):
summ = summ+(i*i)
print("sum of n number ",summ)
enter the num = 3
sum of n number 14
In [ ]:
Q.3
numbers = [10, 20, 30, 40, 50]
sum of all the list item.
In [38]: numbers = [10,20,30,40,50]
add = 0
for i in numbers:
add=add+i
print(add)
150
In [ ]:
Q 4.
input = "1234"
calculate the sum of digit 1+2+3+4
In [39]: a =input()
tot = 0
for i in range(len(a)):
tot = tot+int(a[i])
print(tot)
23445
18
In [ ]:
Q 5.
first create a empty list ...and append from 1 to 10 number into a empty list.
In [40]: a = []
for i in range(1,11):
a.append(i)
print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [ ]:
python for loop to count the number of elements in a list
In [41]: number =[1,2,3,4,5,6,7]
count = 0
for i in number :
count +=1
print(count)
In [ ]:
python for loop to find the minimum element in a list
In [42]: numbers = [30, 50, 33, 5, 4, 20,]
min_val = numbers[0]
for num in numbers:
if num < min_val:
min_val = num
print("Minimum value is: {}".format(min_val))
Minimum value is: 4
In [ ]:
numbers = [10, 20, 30, 40, 50] you have to find out average of this list
In [43]: a=[60,60,60,60,60]
sum=0
count=0
for i in a:
sum+=i
count+=1
print("average is :",sum/count)
average is : 60.0
In [ ]:
In [46]: numbers = [10, 20, 30, 40, 50]
add = 0
for i in numbers:
add = add + i
list_size = len(numbers)
average = add / list_size
print(average)
30.0
In [ ]:
In [ ]:
take a input from user (e.g., "12345"), the program will calculate the sum of its individual
digits (1 + 2 + 3 + 4 + 5) and print the result (15).
In [45]: user = input("enter the number = ")
total=0
for i in user:
total+=int(i)
print(total)
enter the number = 12345
15
In [ ]: