- 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
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.
list = [Ankur, Kriva, Swara, Bharat, Micale, Jhon]for i in list:print(i)Output:AnkurKrivaSwaraBharatMicaleJhon
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.
for i in range(1,10):print(i)Output:123456789
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.
for i in range(10,21):print(i)Output:1011121314151617181920
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.
list = ["Ankur","Rahul","Shubham","Bharat"]for i in list:print(i)Output:AnkurRahulShubhamBharat
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.
for i in range (1,10):print (i)if i == 5:breakOutput:12345
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.
for i in rage(1,5):if i == 2:continueprint (i)Output:134
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.
for i in range(1,100):passOutput:
A nested loop is a loop inside a loop. we can write a program so, we can easily understand, what is nested loop.
for i in range(1,5):for j in range(5,10):print( i, j )Output:1 51 61 71 81 92 52 62 72 82 93 53 63 73 83 94 54 64 74 84 9
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.