Python For Loops


A for loop is used to execute a code block for a specified number of times.

When it comes to iterating through a sequence, a for loop is used (that is either list, tuple, dictionary, set, or string).

Example :- Print each fruit from a list :

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Output :-

apple
banana
cherry

The for loop don't requires no previously defined indexing variable.

You can also search for these topics, python for loops practice, check the python for loops range, python increment the for loops, python for loops program example, add for loops in single line using python, check the index value of python for loops.

Looping Through a String

Strings, even if they are iterable objects, have a series of characters :

Example :- Loop through the letters in the word "banana" :

for x in "banana":
  print(x) 

Output :-

b
a
n
a
n
a

You can also search for these topics, python looping throught a string length, looping through a string with index in python, python list the loop through a string, array and index python looping through a string, looping through a string between two dates using python, define the characters of for loop through the string with python, python with or without key value in python looping through string, python loop through a string backwards, how to create a string using for loop in python, Example for python looping through a string.

The break Statement

We can interrupt the loop with the break declaration, before all elements are looped through :

Example 1 :- Exit the loop when x is "banana" :

fruits = ["apple", "banana", "cherry"]
for x in fruits:  
  if x == "banana":
    break
  print(x)

Output :-

apple

You can also search for these topics, python the break statement into multiple lines, python break all for loop, for loops between two values using python, use of python break statement with for loops best practices, python for loops break statement brackets, how to block the break statement with python, example for python break statement.

The continue Statement

We can halt the current loop iteration using the continue statement and proceed with the next :

Example :- Do not print banana :

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

Output :-

apple
cherry

You can also search for these topics, python continue statement in for loop, python continue Statement next line, check the working of python continue statement, example for continue statement in python, python continue statement after error, use the python code for continue statement, continue statement conditional using python.

The range() Function

To loop a given number of times through a code set, the range() method can be used.

The range() method returns a number series, start with 0 and increasing by 1 by default and ending with a specified number.

The range() function has three parameters, which is:

  • Starting Number :- The default start value is "0". You can define from where the loop should start.
  • Ending Number :- You can set an ending number for the loop.
  • Increment :- How much to be increment after every loop iteration completion.

Example 1 :- Using the range() function:

for x in range(6):
  print(x)

Output :-

0
1
2
3
4
5

In the above program, we did not specified an start value and increment value. So it used the default start value as "0" and the default increment counter as "1".

Note:- It should be noted that range(6) is not 0 to 6 then but 0 to 5.

Example 2 :- Using the start parameter :

for x in range(2, 6):
  print(x) 

Output :-

2
3
4
5

By default, the range() feature increases the sequence by 1 but it can be specified by adding a third parameter : range(2, 30, 3) :

Example 3 :- Increment the sequence with 3 (default is 1) :

for x in range(2, 30, 3):
  print(x) 

Output :-

2
5
8
11
14
17
20
23
26
29

You can also search for these topics, python range function reverse, example for range() function using python, parameters for python range() function, range() source code in python, python documentation for function range, how to include last range of function using python, ways to define the range() python arguments, range to create a python function.

Else in For Loop

The else keyword in a loop provides a code block for running when the loop is complete :

Example 1 :- Display all of 0 to 5 numbers and then, when the loop ends, print a message :

for x in range(6):
  print(x)
else:
  print("Finally finished!")

Output :-

0
1
2
3
4
5
Finally finished!

Note : If the loop is halted by a break statement, the else block is NOT executed.

Example 2 :- Break the loop when x is 3 and see the else block :

for x in range(6):
  if x == 3: break
  print(x)
else:
  print("Finally finished!")

Output :-

0
1
2

Note:- If the loop breaks, the else block is not executed.

You can also search for these topics, python else in for loop one time, example for Else in For Loop using python, how to get index in python else with for loop, use go back one method with else in for loop in python, how to get two elements in python else in for loop, python else in for loop high to low, python how to skip else in for loop.

Nested Loops

There is a nested loop within a loop.

For every iteration of the "outer loop" the "inner loop" is performed once :

Example :- For every fruit, print every adjective :-

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

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

Output :-

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

You can also search for these topics, python nested loops example, Nested Loops pattern in python, how to break the python nested loops, continue the nested loops with python, python nested loops in one line, nested loops alternative syntax with python, Example for nested loops using python.

The pass Statement

The for Loops cannot be empty, but if you have a loop with no content for some reason, put the pass statement in order to avoid error.

for x in [0, 1, 2]:
  pass

# having an empty for loop like this, would raise an error without the pass statement

Note:- Having an empty for loop like this, would raise an error without the pass statement.

You can also search for these topics, python pass statement example, purpose of for loops pass Statement in python, python working of function in pass statement, pass statement loop in python, python pass statement by value, pass by assignment statement using python.