Python For Loop, Else, Range and Nested For Loop

To perform certain iterations, you can use Python for loop. There are many ways and different methods available in Python to use for loop in Python. These methods are given below with an example.

Python For Loop Syntax

The for loop syntax contains two variables to use. The first variable is the iteration variable to use and store values. The second variable can be valued range or variables of Python like string, list, dictionary, and tuple.

Inside the loop, you can put the code to execute each time when the iteration perform.

Use For Loop to Iterate Through String

You can use the loop with the string to get each individual character of the string. You can print each string one by one using the below method. If you want to learn more about the string variable, you can read our post based on how to create a string variable in Python.

Output

B
i
l
l
y

The output of the above example contains the single character in a single line using Python. If you want to learn more about the loop with string, you have to read the post on how to iterate through Python string elements.

Loop Through List Elements in Python

The list variable is the variable contains both the string and numbers together inside the list. The elements of the list are enclosed within the square([]) brackets. Each element is comma separated. If you want to learn more about list variable, you have to read our post of List variable in Python. To get each element of the list, you have to use the below-given method.

Output

Roby
Sam
19
Billy
21
Femmy

The above example prints all the elements of the list. The elements in the output contain both the string and the numbers. If you want to learn the use of for loop with list element in detail, you have to read our post how to loop over List elements in Python.

Iterate Through Dictionary Using Python

The Python Dictionary contains the elements in an associated array manner. It means each element are stored with its keys. You can identify the elements with their given keys. The elements are arranged in a sequence and can be string or numbers.

To print each element of Dictionary, you have to use the for loop and the keys to getting elements. After using the loop, you can print either key, value or both of them together.

Output

one
two
three
fore
five
six

The above example showing each key of the Dictionary in each line. However, you can also print the values of these keys in the output. To know more about this, you have to read the post on
how to loop through dictionary elements in Python and get elements.

Perform For Loop Over Tuple Elements in Python

The tuple is same as of list in Python. It may contain both the string and number elements together as the items. However, you cannot add, update or modify the elements of the tuple once created. Learn more about the tuple in the post Python tuples.

You can use for loop to get each element of the tuple in the output.

Output

Ramu
26
11
Darren
37
Jacob

The above example contains the output which contains elements of tuple prints in each line. You can perform a different operation over the tuple using for loop. To learn more, you have to read our post on how to iterate through tuple elements in Python.

Break Statement Within Loop

The break statement can be used to comes out of loop immediately when strike. To use the break statement, you have to use the below method to perform a loop over the Python string elements.

Output

j
a

The above example contains the if statement inside which break statement located. If the condition is true, it comes out of the loop and stop performing iterations.

Continue Statement Within Loop

You can use the Continue statement with the for loop to skip the statement after it and continue the loop. To use the Continue statement with the loop, you have to use the below method.

Output

j
a
g
o
n

The above example do not print the string character when if the condition is true. It continue with the loop when reaches the Continue statement.

How to Use Else Statement With For Loop in Python

The else statement gets executed after the for loop execution. However, if the loop contains the break statement, it will not execute the else statement and also comes out of the loop. You have to use the else statement as given in the method below.

You can put the break statement in an optional manner to use else statement.

Output

j
a

The above example contains the break statement. The output does not execute the else statement after it reaches the break statement. However, it’s optional to use the break statement. You can also use the continue statement with the loop.

Loop in Python With Range()

The above all examples can also work with the range(). The range function limits the iteration of the loop in Python. The below example iterate through the string elements.

Output

j
a
r
g

The above example performs iteration 4 times because of the range(). However, you can limit it to more or less number of iterations as per your requirement.

Nested For Loop in Python

If you want to use for loop inside another for loop, you have to use the below-given method. The below method also using the range() to limit the loop. You can use the range or without the range using the below method.

Output

1
22
333
4444
55555
666666
7777777
88888888
999999999

The above example prints the output as given above. You can also print the elements of string or print numbers in matrices format.

You may also like to read

Hope, you like this post of how to use for loop in Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to work on for loop with different Python variables.

THE ULTIMATE TOOLKIT

Get FREE Access to Toolkit and Resources that we are using in Tutorialdeep that Every Professional Should Have! in one handy PDF.

Get the PDF

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Download Link Will be Send to Your Email id