For loop in python

for_loop_in_python

Skill - For loop in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision

for loop is used to iterate over a sequence of items and perform a task on each item one after the other.

For loop on an array

x = [1,8,6,5]
for n in x:
	print('The number is {0}'.format(n))

# this will print
# The number is 1
# The number is 8
# The number is 6
# The number is 5

y = ['Nagasudhir', 'Lakshmi', 'Kishore']
for n in y:
	print('Hi {0}!'.format(n))
# this will print
# Hi Nagasudhir!
# Hi Lakshmi!
# Hi Kishore!

For loop on a sequence

In this example we are generating a sequence using range function

for n in range(1,10,2):
	print('The number is {0}'.format(n))

# this will print
# The number is 1
# The number is 3
# The number is 5
# The number is 7
# The number is 9

Online Interpreter

You can run these codes online at https://www.programiz.com/python-programming/online-compiler/

Video

The video for this post can be found here


Table of Contents

Comments