for loop in python programming language
for loop in python programming language
We will learn about for loop in python programming language
In python, for loop is used to repeat a block of codes a limited time until given conditions are fulfilled. We can use any python object such as string, array, list, tuples and dictionary at for loop in python.
Declaration
The syntax of for loop in Python
for var in sequence: statements - 1 //body of for loop statements - 2 statements - 3 statements - 4
var – var is used to read each element from a range of elements.
sequence– sequence means range of element in python(eg – list, tuple, String, )
Flowchart of python for loop
Looping through of the integer in Python
Example
program 1
loop displays of the range of 6 natural numbers, results are vertically
for x in range(6): print x;
when the above code is executed, it produces the following result
0 1 2 3 4 5
Program 2
loop displays of the range of 6 natural numbers, results are horizontally
for xin range(6): print x,;
when the above code is executed, it produces the following result
0 1 2 3 4 5
program 3
for x in range(1,6): print x,;
when the above code is executed, it produces the following result
1 2 3 4 5
program 4
the loop displays the odd number in the range of 1 – 10
for x in range(1,10,2): print x,
when the above code is executed, it produces the following result
1 3 5 7 9
program 5
for i in range(6): print 'this is for loop'
when the above code is executed, it produces the following result
this is for loop this is for loop this is for loop this is for loop this is for loop this is for loop
Program 6
Displays number from the list usingĀ for loop
number=[45,65,34,23,21,8,9] //#python list for i in number: print i
when the above code is executed, it produces the following result
45 65 34 23 21 8 9
Program 7
number=[45,65,34,23,21,8,9] //#python list for i in number: print i,
when the above code is executed, it produces the following result
45 65 34 23 21 8 9
Program 8
find the sum of the total numbers in a the list of Python
number=[45,65,34,23,21,8,9] sum=0; for i in number: sum=sum+i print sum,
when the above code is executed, it produces the following result
45 110 144 167 188 196 205
Looping through the string in Python
Example
program 9
Loop through the character in the word “mango”:
for x in "mango": print(x)
when the above code is executed, it produces the following result
m a n g o
Program 10
animal={'tiger','lion','how','goat','fox','dog','cat'} for i in animal: print('This is a animal'+i)
when the above code is executed, it produces the following result
This is a animal tiger This is a animal lion This is a animal how This is a animal goat This is a animal fox This is a animal dog This is a animal cat
Program 10
birds={‘Parrot’,’Crow’,’Pigeon’,’Duck’,’Ostrich’,’Cock’,’Owl’}
for i in birds:
print ‘a bird ,’ +i,
birds={'Parrot','Crow','Pigeon','Duck','Ostrich','Cock','Owl'} for i in birds: print i+ ' a bird ,',
when the above code is executed, it produces the following result
Owl a bird , Parrot a bird , Crow a bird , Pigeon a bird , Ostrich a bird , Duck a bird , Cock a bird ,
Program 11
Find the total of numbers in the list of numbers
#This is a program helps to find the sum of numbers stored in a list number=[45,56,67,78,89,43,56]#list of numbers #define a variable and initialized sum=0 #user for loop find sum for n in number: sum=sum+n #display elements print("Total number is",sum)
when the above code is executed, it produces the following result
('Total number is', 434)
Else in for loop
for x in range (6): print(x) else: print("finished the loop")
when the above code is executed, it produces the following result
0
1
2
3
4
5
finished the loop
Suggested for you