Javaexercise.com

Python For And For-else Loop

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of for and for-else loops. Let us have a look at them in detail.

What are for loops?

In Python, for loops are used for the purpose of iteration on objects of sequential data type.

These include data types such as strings, lists, tuples, dictionaries, and sets. A for loop in python is a bit different from the concept of for loops found in other programming languages. Rather, it is more similar to the concept of iterators in other languages.

In python, for loops help us perform something known as definite iteration. This refers to the cases of iteration or repeatedly performing a task for a fixed number of times that is known beforehand. The following flowchart will help us understand the concept of for loops in a better manner.

Python for  loop and flowchart

Python for loop Syntax

The syntax of for loops is as follows. Note that the statements to be executed per iteration are added from the next line onwards with an indentation. The keyword used here is for. A colon is also present at the end of the first line.

for <variable_name> in <iterable>:
    <statements to be executed per iteration>

Now, let us look at some use cases of for loops with different iterables:

For loops with strings in Python

In this method, we shall use an object of string data type as the iterable for our for loop. Iterating through a string like this helps us print each character of the string at a time in a separate line. Let us look at the python code and corresponding output for this example.

# Iterate string using for loop  in python
x = "hello"
for var in x:
  print(var)

Output

h

e

l

l

o

For loops with lists in Python

In this method, we shall use an object of list data type as the iterable for our for loop. Iterating through a list like this helps us print each element of the list at a time in a separate line.

Let us look at the python code and corresponding output for this example:

# Iterate list using for loop  in python
x = [0,1,2]
for var in x:
  print(var)

Output

0

1

2

For loops with tuples in Python

In this method, we shall use an object of tuple data type as the iterable for our for loop. Iterating through a tuple like this helps us print each element of the tuple at a time in a separate line.

Let us look at the python code and corresponding output for this example:

# Iterate tuple using for loop  in python
x = (0,1,2)
for var in x:
  print(var)

Output

0

1

2

For loops with dictionaries in Python

In this method, we shall use an object of dictionary data type as the iterable for our for loop. Iterating through a dictionary like this helps us print each value of the dictionary at a time in a separate line.

Let us look at the python code and corresponding output for this example:

# Iterate dictionary using for loop  in python
x = {'a':0, 'b':1, 'c':2}
for var in x:
  print(x[var])

Output

0

1

2

For loops with sets in Python

In this method, we shall use an object of set data type as the iterable for our for loop. Iterating through a set like this helps us print each element of the set at a time in a separate line.

Let us look at the python code and corresponding output for this example:

# Iterate set using for loop  in python
x = {0,1,2} # Creating Set

# for loop
for var in x:
  print(var)

Output

0

1

2

For loops with the range() function in Python

This is one of the most common ways in which a for loop is usually used.

The range function is used to return a list of values within a particular range. By default, if a number, say 3 is passed as a parameter to the range() function, it returns a list of numbers starting from 0 to one less than the number specified, here 3.

Let us look at the python code and corresponding output for this example:

# Iterating with range over a specified number range
for var in range(3):
  print(var)

Output

0

1

2

For-else loop in Python

Another statement we can use along with for loops is the else statement and in such cases, it is known as the for-else loop.

The statements specified by the else statement are executed once the for loop finishes execution.

Let us look at the python code and corresponding output for this example - 

# for-else loop in python
for var in range(3):
  print(var)
else:
  print("For loop finished execution")

Output

0

1

2

For loop finished execution

Nested for loops

Here, we shall look at the concept of nested for loops. In this, we have one for loop inside the body of another one.

All iterations of the inner loop are executed for each iteration of the outer loop. The range function is used to return a list of values within a particular range.

By default, if a number, say 2 is passed as a parameter to the range() function, it returns a list of numbers starting from 0 to one less than the number specified, here 2.

Let us look at the python code and corresponding output for this example.

# Nested for loop in python
# outer loop
for i in range(2):
  # inner loop
  for j in range(2):
    print(j)

Output

0

1

0

1

Conclusion

In this topic, we have learned the use and advantages of for and for-else loops in a Python program, following a running example of a simple iterative printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions