codingstreets
Search
Close this search box.
python-for-loop

Introduction to Python for loop with Practical Example

In this article you will learn about Python for loop.

Python for loop – Before moving ahead, let’s know a little bit about Python while loop

for loop – A for loop iterate over the sequence, in other words, it executes a block of code once for each item. A sequence that consists of a list, tuple, set and dictionary, etc.

Example 1- Use ‘for’ loop to iterate over the statement.

list_strg = ['a', 'b', 'c', 'd']
for x in list_strg:
     print(x) 
python-for-loop 
As it is shown clearly that for loop iterate over element of a list and returned each element one by one.

Loop Through a String – Strings are iterable objects and can iterate over the string.

Example 2- Iterate over the string.

s = 'string'
for x in s:
     print(x) 
python-for-loop 
As it is shown clearly that for loop iterate over element of a string and returned each element one by one.

Break Statement – Using break keyword loop can be stopped through looping all items.

Example 3- Use of break keyword to exit loop.

s = 'string'
for x in s:
    print(x)
    if x == 'n':
         break 
python-for-loop 

Explanation – Loop will stop after printing ‘n’ because the print statement is before break keyword. In this process, the first loop will print all elements until the element ‘n’ and as it gets x is equal to n, it will break the loop.

Example 4- Use of break keyword to exit loop but this time print statement comes after break keyword.

s = 'string'
for x in s:
    if x == 'n':
        break
    print(x) 
python-for-loop 

Explanation – Loop will stop before printing ‘n’ because the print statement is after break keyword. In this process, the first loop will iterate once for each item and break loop where x is equal to n, and then execute the statement.

continue statement – Instead of stopping a loop let it go to the next iteration with the continue keyword.

Example 5- Continue loop to the next iteration.

s = 'string'
for x in s:
    if x == 'n':
        continue
    print(x) 
python-for-loop 

Explanation – Loop will continue printing after keyword ‘n’ because the print statement is after the continuing keyword. In this process, the first loop will print all elements except the element ‘n’ and as it gets x is equal to n, it will again continue printing element.

range() Function – It returns a sequence of numbers. By default, it starts with zero and increments one at each step. The range() function uses to get the number in the sequence.

Example 6- Use range() function to get sequence of number.

for x in range(6):
      print(x)
python-for-loop 

Explanation – As it is shown clearly that it returned number one by one till 5.

Note: range() function doesn’t count last number, it stops just one step before this.

range() Function with parameter – By default, range() function starts with zero but by adding parameters it can start from any specified number.

Example 7- Use range() function to get sequence of number from 5 to 10.

for x in range(5, 11):
      print(x)
python-for-loop 

Explanation – As it is shown clearly that it returned number between the range.

range() Function with increment – By default, range() function increments one but by adding third parameter increment can be changed.

Example 8- Use the range() function to get the sequence of numbers from 1 to 9 by incrementing 2 once per iteration.

for x in range(1, 11, 2):
      print(x)
python-for-loop 

Explanation – As it is shown clearly that it returned number between the range with the specifically interval of numbers.

Else in For Loop – It executed when a block of code is finished.

Example 9- Use else keyword to execute a block of code.

for x in range(6):
     print(x)
else:
     print('Loop is finished') 
python-for-loop 

Explanation – As it is shown clearly that it returned number one by one till 5 and at the last else condition. As range doesn’t include last step therefore, when x == 6, condition went false and it printed else statement.

Nested for loop – A for loop is inside another for loop. The inside loop will iterate each time for every element in outer loop.

Example 10- Use nested loop.

x = ['Python' , 'Java' , 'C++']
z = ['Ruby' , 'JavaScript' , '.Net']
for s in x:
    for k in z:
       print(s , k) 
python-for-loop 

Explanation – As it is shown clearly that element first looped one by one by both loops and then printed every individual element of both loops together one by one.

pass statement – A for loop cannot be empty but in case it is necessary to leave it empty then use pass keyword to avoid getting an error.

Example 11- Use pass keyword in for to avoid an error.

for x in range(5):
     pass
python-for-loop 

Explanation – As it is shown clearly that keyword pass, passed the answer.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on