How to play with for loop in python

python for loop

Content

  • Introduction
  • Understand the for loop
  • Make the first program of for loop
  • Range Function in for loop
  • for loop using numbers
  • for loop using string
  • Break, Continue, Pass statement in for loop
  • Nested loop 
  • Conclusion

Introduction

For loop is used to iterate the list of any things. For example list of numbers, string, etc.
If you never heard these words for loop then don't worry, we will also know the terms and also do some examples and we will also describe the examples.

Understand  the  for loop

If you watch a youtube video then you notice at bottom of the video you found the loop button. This button helps to repeat the video again and again. So, this for loop is also similar to this youtube button loop. For loop iterate the elements one by one and it stops when there's no element remain in the list.  

First python program of for loop

To run the for loop, first, we need the list or tuple. So, first, we make a list that contain the name of the students. And then we will iterate the list. The syntax for the loop is very small and easy.

Program:

    

list = [Ankur, Kriva, Swara, Bharat, Micale, Jhon]
for i in list:
    print(i)

Output:
Ankur
Kriva
Swara
Bharat
Micale
Jhon


Here the first line contains the list of student names. And the second line contains the simple syntax of for loop. And in the second-line, the "i" is the name of for loop and we can use any name instead of "i".
And lastly, we print the for loop.

Range Function in for loop

The range function is used only for numbers. And you all know range means scale type where upper limit and lower limit are fixed. For example the range of 1 to 10 numbers.
In for loop we use range function to iterate the number.
So, we will make the program to iterate the number using the Range function.

Program:

    

    
    
for i in range(1,10):
    print(i)

Output:
1
2
3
4
5
6
7
8
9


For loop using numbers

To iterate the number, we want to use the range function that was already done above. We can also write one more example of a range function.

Program:

    

    
    
for i in range(10,21):
    print(i)

Output:
10
11
12
13
14
15
16
17
18
19
20

For loop using strings

To iterate the string, first, we need to import the strings in the list. So, we first import the strings in the list, and then we will iterate the list and we finally found the iteration of strings. So, we write the program.

Program:

    

    
    
    
list = ["Ankur","Rahul","Shubham","Bharat"]
for i in list:
    print(i)

Output:
Ankur
Rahul
Shubham
Bharat

Break, Continue, Pass statement in for loop

1) Break Statement in the for loop

The break statement is used to stop the loop. For example, I will iterate the numbers and I want to stop the loop when the number is equal to 5, at that time we, will use the break statement. So, we write programs to understand the break statement.

Program:

    

    
    
for i in range (1,10):
    print (i)
    if i == 5:
        break

Output:
1
2
3
4
5

2) Continue Statement in for loop

The Continue statement is used to skip any elements from the list while for loop is running. For example, I want to remove 5 from the list then at that time I will use the Continue statement in the for loop. So, we can write a program to understand the Continue statement.

Program:

    

    
    
for i in rage(1,5):
    if i == 2:
        continue
    print (i)

Output:
1
3
4

3) Pass Statement in for loop

If you don't want to print anything from the for loop you can simply use the pass statement.

Program:

    

    
    
for i in range(1,100):
    pass

Output:

Nested loop

A nested loop is a loop inside a loop. we can write a program so, we can easily understand, what is nested loop. 

Program:

    


    
for i in range(1,5):
    for j in range(5,10):
        print( i, j )

Output:
1 5
1 6
1 7
1 8
1 9
2 5
2 6
2 7
2 8
2 9
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9

Conclusion

In this post, we will learn how to play with for loop and how we learn the different functions of the loop with the example. And we will also see how we run for loop inside the other loop.
Next Post Previous Post
No Comment
Add Comment
comment url