For Loop In Python Programming Language

Python for Loop

In this tutorial, you will learn about what is Python for Loop ? and different variations of using it in your code.

What is Python for loop?

Python for loop is used to repeat or iterate a list, tuple or string in a sequential manner.

 

Syntax of for loop

for repeating_variable in sequence:
	Block of for loop


Now I will explain what the above Python for loop syntax means. Here the variable repeating_variable we use you can use whatever you want now in sequence we will set the limit of time for we want to iterate the block of code. The block of for loop keeps iterate until the limit we set in the sequence finished.

Recommended:  Nested Loop In Python Programming Language

Python for loop FlowChart

For-Loop-Flowchart-in-Python-Programming-Language

Example: Python for Loop

# Python for loop example to find out the sum of all numbers stored in a list

# List of numbers
numbers_list = [3, 2, 1, 4, 2, 4, 10, 8, 22]

# variable we use to store the calculated sum
sum = 0

# here we do iteration to the list
for value in numbers_list:
	sum = sum+value

print("The sum is", sum)

When you execute the code you will get the following results:

The sum is 56

Python range() function

The range() function is also handy in generating numbers sequentially.

For example range(20) displays the numbers on the screen from 0 to 19 (all 20 numbers).  In some cases, we want to define the numbers from where to start, where stop and what should be the step size of the range(). But if we don’t define step size it is 1 by defaults.

Recommended:  Variables,Data Types and Type Conversion In Python Programming Language

Remember: The range() function generates the up-coming number by remembering the start, stop and step size and it does not keep/store all the record in his memory

See the example below:

# Here we set the Output for range as: range(0, 20)
print(range(20))

# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
print(list(range(20)))

# Output: [2, 3, 4, 5, 6, 7]
print(list(range(2, 8)))

# Output: [2, 5, 8, 11, 14, 17]
print(list(range(2, 20, 3)))

The range() function can also be combined with for loop to iterate numbers sequentially through the loop. len() function is used to iterate using the indexing.
See the below example:

# This shows the iteration through a list using indexing

categories = ['pop', 'rock', 'jazz', 'classic']

# now we use for loop to iterate over the list using index
for i in range(len(categories)):
	print("I love", categories[i])

When you execute the code the output will be as:

I love pop
I love rock
I love jazz
I love classic

for loop with else

An optional else statement can also be used with loop statement available in python programming.  The else block executed right after the iteration of for loop finishes execution. At any point in the code, you want to stop the for loop you can use break statement but in this case, all the else part will be ignored.

Recommended:  Python Environment Setup

See the below example:

#Below code to show the execution of for loop with the esle statement.

values = [0, 3, 7]

for i in values:
    print(i)
else:
    print("Tehre is No items left.")

After the code is executed the following result will be displayed.

0
3
7
There is No item left.

 

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu