Skip to content
Home » Blog » Python For Loop – Now in a Simple and Efficient Version

Python For Loop – Now in a Simple and Efficient Version

Python For Loop

Python For Loop helps us to loop around Strings, Lists, Tuples, Range and etc. It is basically used for iterating over a Sequence.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. We will also discuss about some helpful functions that go with the for loop, such as pass, continue, break.

Now that we have a idea what a for loop does, let’s see it in action.

Python For Loop

LOOPING THROUGH STRINGS

When it comes to Strings, Python iterates through each character of the string

for i in "geekalgo":
   print(i)

Here ‘i’ takes the value of each letter of the String from g and all the way to o during each iteration

When we run the code, we get

g
e
e
k
a
l
g
o

LOOPING THROUGH LISTS

Whereas in Lists, the loop iterates through each item in the list.

for i in ['Cat', 'Dog', 'Hamster', 'Budgies']:
   print(i)

Here ‘i’ takes the value of each item of the list from Cat and all the way to Budgies during each iteration.

When we run the code, we get

Cat
Dog
Hamster
Budgies

LOOPING USING RANGE

ONE PARAMETER:

To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

for i in range(5):
   print(i)

When we run the code, we get

0
1
2
3
4

TWO PARAMETERS:

We can use two parameters in the range() function in order to indicate the start and stop of the number sequence.

for i in range(2, 8):
   print(i)

When we run the code, we get

2
3
4
5
6
7

THREE PARAMETERS:

We can use third parameter in the range() function to mention the increment number which is 1 by default.

for i in range(2, 11, 2):
   print(i)

When we run the code, we get

2
4
6
8
10

STATEMENTS IN PYTHON FOR LOOP

BREAKING A PYTHON FOR LOOP

With the break statement we can stop the loop before it has looped through all the items.

for i in range(20):
   if i == 5:
      break
   print(i)

When we run the code, we get

0
1
2
3
4

CONTINUE STATEMENT

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

for i in range(10):
   if i == 5:
      continue
   print(i)

When we run the code, we get

0
1
2
3
4
6
7
8
9

PASS STATEMENT

In Python you cannot have a loop without any content inside, this raises a IndentationError, to avoid this, we use the pass statement

for i in range(5):
   pass

Python For Loop -Conclusion

In today’s post, we’ve discussed Python For Loop and used them in Strings, List, and Range. We have also discussed the use of the pass, continue, break statements. If you are on your way to becoming a Pythonista, you might find our site really helpful. Hit this link for more python related posts
Check it out, it’s Simply the Best Code.

Leave a Reply

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