Loops are an integral part of any programming language, they depict a continuous action until a condition is fulfilled or until a defined range nears its end. There are multiple sorts of loops in Python such as while loop, for loop etc. These loops are the most basic aspect of any programming or scripting language and almost all of them have a for loop but with different syntaxes.
In Python, for loop has multiple categories and uses unlike any other programming language and can be used to perform various actions apart from set ranges. Let’s take a look:
For Loop:
A for loop in python is generally an iterative action for a sequence, this sequence can be defined by any list, dictionary, set or tuple or any other combination of variables or values. For loop allows the user to iterate between the values/variables one by one and perform a specific task in them, let’s have a look at the multiple uses you can gain through for loops.
List1 = ["Cars", "Bikes", "Trucks"]
For n in List1:
print(n) //This will print all the items in the list in a sequence one by one starting from index 0 to index 2.
Looping a String:
You can also loop through the separate characters/integers in a string one by one, use this syntax:
Str1 = "HiThere"
For n in Str1:
print (n) // This will print all the characters/integers in the string starting from index 0.
Range() in For Loop:
Ranges in for loop are used to iterate a set of commands over a given sequence or number of iterations, take a look below:
For x in range(10):
print("this is a for loop") // This entire loop will print "This is a for loop" ten times, starting from 0 with an increment of 1 until 10 is reached, the increment is done implicitly.
Nested For Loop:
You can pass multiple for loops inside a single parent for loop, there is no limit on how many loops you want to iterate through and this opens up avenues for multiple functions in lists and tuples. A nested for loop runs the parent for loop once then the control is passed to the second for loop etc. until the last for loop runs and the cycle repeats from the start until the condition set for the parent loop is met or otherwise defined. Here is how you can create a nested for loop.
List1 = ["Cars", "Bikes", "Trucks"]
List2 = ["Good", "Fast", "Heavy"]
For n in List1:
For x in List2:
print(n, x) //This will print all the items in List1 followed by List2, the control will pass to List1 and the first value will be read then the control will pass to the first value in List2 and it will be read, then both will be printed and the cycle will continue until the lists are empty.
Break & Continue in For Loops:
A break statement will stop the loop before it has completely iterated through the variables/values passed to it. Here is how it works:
List1 = ["Cars", "Bikes", "Trucks"]
for n in List1:
if n = "Bikes":
break // the break statement will stop the for loop here and it will continue on with the rest of the code.
print(n) //This will print the last value before the break happens, we can pass the break before or after the print, it wouldn't matter much in this case.
You can also pass a break without any condition set to it, it will immediately stop after running once.
In a similar manner to the break functions, the continue function will stop at a specific iteration to allow the user to perform some task, then continue with the rest of the loop.
List1 = ["Cars", "Bikes", "Trucks"]
for n in List1:
if n = "Bikes":
continue // the continue statement will stop the code at "Bikes" and then continue from the next iteration.
print(n) //This will print the last value before the continue is called.
Pass in For Loop:
A pass in for loop will simply skip the for loop for the time being, if no action is called within the for loop, instead of giving an error at run-time, here is how it will work:
for n in List1:
pass
While Loop:
A while loop is very different as compared to a for loop, a for loop works on the iterative function and a while loop works on the fulfilment of a condition. A while loop will keep iterating the commands inside until a specific condition is met, it can be modified as an iterative loop too with an incrementing variable passed to the same while loop. Let’s take a look below on it exactly works:
i=0
While i < 10
print (i)
i += 1 // This entire while loop will print the value of i starting from 0 until it reaches 9 or "<10" with an increment of 1, the increment is passed in the same while loop to keep it running. Python does not support ++ or -- operators.
Break and Continue in While Loops:
Similar to the break in for loop, the break in while loop will exit the loop once it is called, it can be called at a specific condition, if no condition is given to the break statement the loop will exit after the very first iteration, it can be utilised for making a selector/choice variable.
i=0
While i < 10
Print (i)
If i == 4:
break
i += 1
You can call the print statement before or after the break statement, it doesn’t really matter, in the afore-mentioned case, it will print i before breaking the loop.
A continue statement works pretty similarly to that of for loop, a continue statement in while loop stops it at a certain iteration to allow the user to perform a task then continue with the iterations from the next index.
i=0
While i < 10
Print (i)
If i == 4:
continue
i += 1