Loops in Python
Python provides different types of loop in python programming.
- for loop
- while loop
- nested loops
for loop in python
For loops iterate over a given sequence.In sequence either (dictionary, tuple, set, list and string )
for loop syntax (for variable in sequence or range)
for loop concept
Examples
#For loop in list
color_list=['red','green','white','purple','pink','marron']
for i in color_list:
print(i)
#For loop in string
fr_string="Goeduhub technologies"
for i in fr_string:
print(i)
#For loop in dictionary
fr_dict={1:'q',2:'w',3:'e',4:"r"}
for i in fr_dict:
print(i)
#Sum of all the list elements
Num=[1,2,3,4,5,6,7,8,9,10]
Sum=0
for i in Num:
Sum=Sum+i
print(Sum)
Output
red green white purple pink marron
G
o e d u h u b t e c h n o l o g i e s
1 2 3 4
55
For loop with range() Function
range() syntax
range(a) - Generates a sequence started from 0 to a , where a is excluded
range(a,b)- Generates a sequence started from a ,end at b , where b is excluded
range(a,b,c)- Generates a sequence started from a, end at (b-1) and c is difference (steps)
Note:-By default range() function take difference of 1, if difference is not given.
Example
color_list=['red','green','white','purple','pink','marron']
for i in range(len(color_list)):
print(color_list[i])
s=0
for i in range(1,14,3):
print(i)
s=s+i
print("sum is =",s)
Output
red green white purple pink marron
1
4
7 10 13
sum is= 35
For loop with else statement
Example
for i in range(5):
print(i)
else:
print("Finally finished!")
Output
0 1 2 3 4
Finally finished!
Nested for loop
color_list=['red','green','white']
for i in range(2):
for j in color_list:
print(i,j)
Output
0 red 0 green 0 white 1 red 1 green 1 white
While Loop in python
While loop also work like for loop and iterate a sequence , with a condition.
Python while loop concept flow chart
Syntax of while loop
while expression or condition:
statement(s)
Example
n=int(input("A Number ="))
i=0
while i <=n:
i=i+1
s=i+n
print(s)
Output
A Number =4 5 6 7 8
9
Loop control statements
Pass statement-If we have empty for loop , put in the pass statement to avoid getting an error.
Continue statement- The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
Break statement- Terminates the loop statement.
While loop with break statement
Example
n=int(input("A Number ="))
b=5
i=0
while i <=n:
i=i+1
if i>b:
break
print(i)
Output
A Number =10
1 2 3 4
5
While loop with continue statement
num=int(input("Num value="))
while num > 0:
num = num -1
if num == 3:
continue
print(num)
print ('Number=', num)
Output
Num value=4
Number= 2
Number= 1
Number= 0
For loop with break statement
Example
List =[1,5,6,8,77,9,6,2.,7]
for i in List:
if i==77:
break
print(i)
Output
1 5 6 8
For loop with continue statement
List =[1,5,6,8,77,9,6,2,7]
for i in List:
if i==77:
continue
print(i)
Output
1 5 6 8 9 6 2
7
For loop with pass statement
numbers = [ 1, 2, 4, 3, 6, 5, 7,8, 9,10,11,12,13,14,15,16 ]
for number in numbers:
if number % 2 == 0:
pass
else:
print (number)
Output
1 3 5 7 9 11 13
15
Part-1
Part-2
Python Tutorial
Machine Learning Tutorial
AI Tutorial
Free Online Tutorials