for loop in Python

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

for variable in sequence:
    statements

Example

Input

a = [1, 2, 3, 4, 5]
for i in a:
    print(i)

Output

1
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

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

Output

0
1
2
3
4
5
6
7
8
9
10


Example:- print the numbers starting from 2 till 10.


Program

Input

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

Output

2
3
4
5
6
7
8
9
10


Example:- print the numbers by taking 2 steps in the numbers ranging from 0 to 10.


Program

Input

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

Output

2
4
6
8
10



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

for i in range(11):
    if i == 6:
        break
    print(i)

Output

0
1
2
3
4
5



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 is 'even', the print statement will be skipped, and continues with the next loop.


Program

Input

for i in range(11):
    if i%2 == 0:
        continue
    print(i)

Output

1
3
5
7
9



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 

for i in range(6):
    if i == 's':
        print("character 's' found")
    print(i)
else:
    print("Loop failed, character 's' not found")

Output 

0
1
2
3
4
5
Loop failed, character 's' not found


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 

for i in range(6):
    if i == 5:
        break
    print(i)
else:
    print("Loop failed, 5 not found")

Output 

0
1
2
3
4



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 

for i in range(6):
    if i%2 == 0:
        continue
    print(i)
else:
    print("It works with continue statement")

Output 

1
3
5
It works with continue statement



Loop through a list Python

List is given to the for loop to iterate over it, yields each element at a time.

...READ MORE.



Python Loop Through a Tuple

Tuple is given to the for loop to iterate over it, yields each element at a time.

...READ MORE.



Iterate over a set in Python

Set is given to the for loop to iterate over it, yields each element at a time.

...READ MORE.



Loop through a string in python

String is given to the for loop to iterate over it, yields each character at a time.

...READ MORE.



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.

...READ MORE.





Conclusion

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.

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.





Previous                          Next

If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)