Python for loop
The 'for' loop in Python is different form the 'for' loop in the other programming languages.
In other programming languages, we provide 3 arguments for the 'for' loop, which are initialization, condition checking and updating the counter.
for loop in python
In 'for' loop in Python, we provide the variable(initializer) which will represent the object in a sequence. At every iteration the variable gets updated to the next object of the sequence.
The 'in' operator in the for loop tells that the variable is present in the sequence and initially the variable represents the first object in that sequence. And gets updated to the next object with each iteration.
In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.
Python 'for' loop syntax
Example
Input
2
3
4
5
for loop with range() function in Python
The range() function is used to specify the number of iterations to be performed by the for loop.
The range() function takes 3 arguments.
range(start, stop, step)
start and step are optional. start has '0' default value.
Example:- print the numbers from 0 to 10.
Program
Input
Output
Example:- print the numbers starting from 2 till 10.
Program
Input
Output
Example:- print the numbers by taking 2 steps in the numbers ranging from 0 to 10.
Program
Input
Output
The break statement in for loop
The break statement will terminate / end the for loop.
At a particular condition when we want to stop the loop, we make use of the break keyword to terminate the loop.
Example:- terminate the loop when i = 6.
Program
Input
Output
The continue statement in for loop
At some iteration when we want to skip the statements of a loop, we make use of the continue keyword.
When continue statement is triggered all the statements below that in a loop will be skipped, and the next iteration continues.
Example:- In this for loop when x is 'even', the print statement will be skipped, and continues with the next loop.
Program
Input
Output
Can we use else clause with for loop
Yes, it's possible to combine 'for' loop and 'else' clause.
When for loop fails, 'else' statement is executed.
Example:- here the for loop fails to find the character 's'.
Program
Input
Output
It won't works with break. If the for loop is terminated by the break statement, then the 'else' statement will not be executed.
Example:- In this the for loop is terminated by the break statement when number 5 is detected, and the 'else' statement is not executed.
Program
Input
Output
It works with continue statement. The 'else' statement is executed after the completion of 'for' loop.
Example:- when i is even then the Iteration is continued, and after the loop is completed the 'else' statement is executed.
Program
Input
Output
Loop through a list Python
List is given to the for loop to iterate over it, yields each element at a time.
Python Loop Through a Tuple
Tuple is given to the for loop to iterate over it, yields each element at a time.
Iterate over a set in Python
Set is given to the for loop to iterate over it, yields each element at a time.
Loop through a string in python
String is given to the for loop to iterate over it, yields each character at a time.
for loop in dictionary python
Dictionary is given to the for loop to iterate over it, yields each key or value or both at a time.
Conclusion
Only works with the iterables (objects which have sequence) and range method.
We have learned the use of break and continue statements with the 'for' loop.
And learned how to use else statement with the 'for' loop.
Also learned to loop through a list, tuple, set, string and dictionary.
إرسال تعليق
If you have any query, feel free to ask.