Categories: For ststementPython

Python language for loop with example

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

Example
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
Example
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
Pattern
Program 10
When the above code is executed it produces the folowing result
Pattern
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

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.