AppDividend
Latest Code Tutorials

Python For Loop Example | How To Use Loops

0

Python For Loop Example | How To Use Loops is today’s topic. Python programming language provides three types of loops to handle looping requirements, and one of them is For Loop. The for loop is used for iterating over the sequence that is either the list, a tuple, a dictionary, a set, or the string. It can iterate over the elements of any sequence, such as a list.  For loops are used for sequential traversal. The Python for loop starts with a keyword “for” followed by an arbitrary variable name, which will hold the values of the following sequence object, which is stepped through.

Python For Loop Example

Loop continues until we reach the last element in the sequence. The body of for loop is separated from the rest of the code using indentation. The for loops are traditionally used when you have a block of code which you want to repeat a fixed number of times.

The general syntax looks like the following code.

for <variable> in <sequence>:
	<statements>

Let us take a simple example of For Loop in Python.

# app.py

GoT = ['Daenerys', 'Jon', 'Tyrion']

for item in GoT:
    print(item)

The output is following.

 

Python For Loop Example | How To Use Loops

In the above example, we have taken one list and iterate list in the loop and printed the item one by one.

Looping Through a String in Python

The strings are iterable objects; they contain the sequence of characters. Let us take the following example.

# app.py

name = 'KRUNAL'
for item in name:
    print(item)

The output is following.

 

Looping Through a String in Python

The break Statement in Python

With the use of break statement in the loop, we can stop the loop before it has looped through all the elements. See the below example.

# app.py

GoT1 = ['Daenerys', 'Jon', 'Tyrion']
for item1 in GoT1:
    print(item1)
    if item1 == 'Jon':
        break

In the above example, if the current iterable item is equal to Jon, it will break the loop. That is why we will not see the Tyrion logged in the console.

 

The break Statement in Python

Else in For Loop

The else keyword in the for loop specifies the block of code to be executed when a loop is finished. If the else statement is used with the for loop, the else statement is performed when the loop has exhausted iterating the list.

# app.py

for x in range(5):
    print(x)
else:
    print('Executed Else Statement!!')

 

Else in For Loop

The range() Function in Python

The range() function returns the sequence of numbers, starting from 0 by default, and increments by 1 by default, and ends at the specified number. We can generate the sequence of numbers using the range() function.

# app.py

for z in range(4):
      print(z)

 

The range() Function in Python

Related Posts
1 of 42

The continue Statement in Python

With the help of the continue statement, we can stop the current iteration of the loop, and continue with the next iteration.

# app.py

GoT2 = ['Daenerys', 'Jon', 'Tyrion']
for item2 in GoT2:
    if item2 == 'Jon':
        continue
    print(item2)

In the above example, when the current iterator is Jon, it will end the iteration and does not print the Jon in the console. The output is following.

 

The continue Statement in Python

Nested For loops in Python

The nested loop is a loop inside a loop. The “inner loop” will be executed one time for each iteration of the “outer loop.” See the below demo.

# app.py

for x in color:
    for y in fruits:
        print(x, y)

The output is following.

 

Nested For loops in Python

Finally, Python For Loop Example | How To Use Loops is over.

Leave A Reply

Your email address will not be published.

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