Python for loop

In programming, ‘for’ loop is used to repeatedly execute a block of code for certain conditions.

Similarly, in Python ‘for’ loop, we iterate over Python sequences such as list, tuple, set, string, etc. This iteration helps us to execute the statements one by one for Python Sequences.

Let’s see some examples of ‘for’ loop:

Looping through a list:
With the help of for loop, we are going to print the list items.

char = ["a", "b", "c", "d"]
for t in char:
    print(t)

a
b
c
d

This process is also called iteration.

Looping through a tuple: Let’s see a simple Example to loop through the Python Tuple.

char = {"a", "b", "c", "d"}
for t in char:
    print(t)

a
b
c
d

Looping through a set: We can also loop through the set values by the help of the for a loop. Let’s see a simple example.

char = {"a", "b", "c", "d"}
for t in char:
    print(t)

a
d
c
b

Looping through a string: We can simply iterate a string as well by the help of the for loop. Let’s see a simple example.

char = "TutorialsClass"
for t in char:
    print(t)

T
u
t
o
r
i
a
l
s
C
l
a
s
s


Break statement

With the help of the break the statement, we can terminate the loop. We can use it will terminate the loop if the condition is true. By the break keyword we describe the break statement.

Let’s see a simple example of break statement in the for loop:
We are going to print a series, and we want to stop the loop after executing two times.

char = ("a", "b", "c", "d")
for t in char:
   if t == "c":
        break
   print(t)

a
b

Note: The break statement only terminates the loop, it does not completely stop the flow of the program.
Let’s see an example.

char = ("a", "b", "c", "d")
for t in char:
   if t == "c":
        break
   print(t)
print("WELCOME")

a
b
WELCOME


Python for loop – Continue statement

With the help of the continue statement, we can terminate any iteration and it returns the control to the beginning again. Suppose we want to skip/terminate further execution for a certain condition of the loop. By using the continue keyword we define the continue statement.

Let’s see a simple example of continue statement in the for loop:
We are going to print a series, and we want to skip the loop at the second iteration where ‘b’ character found.

char = ("a", "b", "c", "d")
for t in char:
    if t == "b":
        continue
    print(t)

a
c
d

Note: The continue statement only skip on an iteration of the loop, it does not completely stop the flow of the program. Therefore, the statements outside loops will still run if exists.
Let’s see an example of it.

char = ("a", "b", "c", "d")
for t in char:
    if t == "b":
        continue
    print(t)
print("WELCOME")

a
c
d
WELCOME


Python For Loop – with else statement

Like the if statement, we can also use else statement with the for loop. Let’s see the basic format of else statement with for loop :

for x in y:
 statement of loop
else:
 statement of else

If the condition of a for loop is satisfied then the loop statement will execute. Otherwise, the else statement will execute.
Let’s see a simple example of it:

char = ("a", "b", "c", "d")
for t in char:
    if t == "b":
        continue
    print(t)
else:
    print("finished")

a
c
d
finished


Python For Loop – Range Function

When we want to repeat a block of code number of times, then we use range() function.
Example of range() function:

Let’s see a simple example of range() function with the ‘for’ loop. Here, the default starting value of range is 0 if we pass only one value because the single argument will be treated as stop value.

char = ("a", "b", "c", "d")
for t in range(3):      
    print(char[t])

a
b
c

Here it starts at 0 and returned till the 3rd element.

Some key points about range() function:

  • We can also specify the starting of range function such as range(2,6).
  • By default, the increment in range function’s range is 1, we can modify it as well passing 3rd argument to the function such as range(30,65,3)

Let’s see some examples of the range() function:

char = ("a", "b", "c", "d", "e", "f", "g", "h", "j", "k")
for t in range(1, 3):
    print(char[t])

print()

for s in range(4, 10, 2):
    print(char[s])

b
c

e
g
j


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.