Table of Contents
Python language for loop with example
In this tutorial, we will discuss the concept of Python language’s for loop with example
In this post, we are going to learn how to use for loop in Python language
Python for loop is used to repeat a block of codes up to limited time to keep the computer running a program. We can use any python object such as string, array, list, tuples, dictionary in for loop.
Syntex
var – variable used to read each element form the range of elements
range – the range of elements in python (list, tuple,String any others)
Program 1
When the above code is executed it produces the folowing result
0
1
2
3
4
5
This program prints numbers from 0 to 5
program 2
When the above code is executed it produces the folowing result
1
2
3
4
5
When the above code is executed it produces the folowing result
0
2
4
6
8
10
12
14
16
18
Program 3
When the above code is executed it produces the folowing result
0
1
3
6
10
15
21
28
36
45
This program sums up every number from 0 to 10 as in the example
Program 4
When the above code is executed it produces the folowing 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
For loop in list
Program 5
When the above code is executed it produces the folowing result
(‘Total value is’, 227)
Program 6
When the above code is executed it produces the folowing result
Dog
Cat
Monkey
Donkey
Elephant
Mouse
Program 7
When the above code is executed it produces the folowing result
This is a animal name Dog
This is a animal name Cat
This is a animal name Monkey
This is a animal name Donkey
This is a animal name Elephant
This is a animal name Mouse
Program 8
When the above code is executed it produces the folowing result
Hen Crow Parrot Duck Ostrich Owl
Else in for loop
for i in range(10): print(i); else: print("Program finished")
When the above code is executed it produces the folowing result
0 1 2 3 4 5 6 7 8 9 Program finished
Pattern printing using for loop
Program 9
Display Floyd triangle using for loop in python
When the above code is executed it produces the folowing result
Program 10
When the above code is executed it produces the folowing result
Program 11
When the above code is executed it produces the folowing result
Program 12
When the above code is executed it produces the folowing result
Enter a non-negative integer to find factorial : 5
1
2
6
24
120
Related post