for loop

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

Flowchart 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

Nested for loop in Java

Nested for loop in C++

Nested for loop in C

Nested for in  Python

For loop in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago