|
|
| Introduction | Variables | If-else |
| Strings | Functions | while-loop |
| For-Loop | List | Set |
| Dictionary | Tuple | Try..Except |
| Class/Object | Inheritance | Polymorphism |
| File Handling | ||
The for loops are very useful in python. They are used to loop through the collections or strings to extract individual characters.
A for loop in Python can run a segment of code repeatedly certain number of times, according to a sequence.
Unlike the while loop, there is no condition required for the for loop. You simply run a segment of code multiple times. In other terms, the while loop continues to run the code block inside it as long as the condition remains True,
whereas the for loop runs the code inside it a predetermined number of times. The for-loops are particularly useful when you are doing backend development in Python frameworks like Flask and Django. You may need to loop through a dataset that needs to be displayed as tabular data in the front-end.
To loop through certain number of times, use the range function. Here's the syntax of range function.
start, stop, step (3 arguments):
Generates a sequence of numbers starting from start (it is inclusive) up to, but does not include the stop value and increment of step.
step can be positive number for the sequences that are ascending or negative for the sequences that are descending.
If start value is not provided in the for loop, it defaults to 0.
If step value is not provided in the for loop, it defaults to 1.
Let's see some examples of for loops with range.
Code Example 1: #Here start is 5, stop is 30 and the step is 3. for x in range(5, 30, 3): print(x) #Output: 5 8 11 14 17 Code Example 2: #The following example has start is 0, stop is 5 and step is 1 for x in range(5): print(x) #Output: 0 1 2 3 4
Sometimes it is necessary to break the for loop based on certain conditions. That's where the break is used. With the break statement, it is possible to come out of for loop before it is iterated through all the items. Let's see some examples below.
Code Example 3:
#The Following will break out of for loop
countries = ["USA", "UK", "Germany", "Japan"]
for x in countries:
if x == "Germany":
break
print(x)
#Output:
USA
UK
Code Example 4:
#The following will exit loop at x=15
for x in range(3, 30, 3):
if(x==15):
break
print(x)
#Output:
3
6
9
12
When you use a continue statement in a for loop, you can stop the current iteration, and move to the next iteration.
Code Example 5:
#The Following will skip 'Germany'.
countries = ["USA", "UK", "Germany", "Japan"]
for x in countries:
if (x == "Germany"):
continue
print(x)
#Output:
USA
UK
Japan
Code Example 6:
#The following will skip x=3
for x in range(1, 5, 1):
if(x==3):
continue
print(x)
#Output:
1
2
4
In Python, for loop can directly loop over the characters of a string. For each iteration, a variable will be assigned one character in the string from left to right.
Code Example 7:
#The following will print each charatcter of a string.
str = "Hello"
for x in str:
print(x)
#Output:
H
e
l
l
o
The else keyword in a for loop executes a block of code when the for loop has completed. It is particularly used inr the backend development when some notification needs to be sent right after the loop is complete or some piece of python code need to be executed as soon as the lines of code in the for loop have completed.
Code Example 8:
#The following will execute else after the for loop ends.
for x in range(5):
print(x)
else:
print("Finally finished!. Do something else.")
#Output:
0
1
2
3
4
Finally finished!. Do something else.