The Usefulness of For and While Loop in Python : One of the essential building blocks in programming is the loop. The main function of a loop is to verify whether a certain condition in a piece of code is true; if it is, the loop executes to produce the desired output. Unless the situation changes, this code will continue to run.
Developers use loops to repeatedly execute a piece of code until a particular condition is met. Thus, it speeds up development and improves code readability.
This article will use different examples to explain the for and while loop in Python.
Understanding the For and While Loop in Python
Python is one of the most user-friendly programming languages you can use to build websites and software and conduct analysis. In Python, there are two different types of loop structures: FOR and WHILE loops.
You can use both to execute the same condition, but the “for” loop structure only requires initialization to occur once rather than repeatedly. You can iterate over a condition “n” times with a FOR loop. And you can use it to iterate over a list or a string. On the other hand, until the condition is false, a WHILE loop iterates continuously. The ”while” loop iterates until a condition is satisfied.
Let’s look at some Python examples of for and while loop implementations.
For and While Loops in Python: How They Work
You can use the for and while loops to repeat tasks in a more efficient manner.
How to use a for loop in Python
Example one: Using a for loop to iterate over a list
fruits = ["strawberry ", "pear", "cherry", "orange"] for x in fruits: print(x)
Introducing a break statement to the loop
fruits = ["strawberry ", "pear", "cherry", "orange", "carrot", "cashew", "mango"] # break halts the program once it meets the x value for x in fruits: if x == "mango": break print(x)
Example 2: Using a for loop to iterate over a range of numbers
#Define for loop #Using range here x will start from values after 1 and before 10 x=0 for x in range(1,10): print(x)
Example 3: Using a for loop for strings
for x in "documentation ": print(x) else: print(" nothing else to execute!")
nothing else to execute!
How to use a while loop in Python
Example 1: Using a while loop to print even numbers
y=0 #while loop example while(y < 20): print(x) x = x+2
Example 2: Using a while loop with a list
a = ['daze', 'cost', 'food', 'goat'] while a: print(a.pop(-1))
Example 3: Using else Statement with While Loop
nitro = 0 while nitro < 3: print nitro, " is less than 3" nitro = nitro + 1 else: print("oops's!!!!!!") print nitro, " is not less than 3"
0 is less than 3
1 is less than 3
2 is less than 3
3 is not less than 3
How to Use Break Statements With a For Loop
If you want to exit a loop or skip part of a loop, you can achieve this with a break statement.
You can use a break statement to break or terminate an ongoing program when it encounters a certain condition.
for x in range(10,20): if (x == 15): break print(x)
Using a break statement with a list
Friends_list=["mika","Hassan","khan","Maria","Ralph","Sean"] for n in Friends_list: if n == "Ralph": # break here print('This operation ended because we came across a break command when we met with Mr.',n) break print(n)
This operation ended because we came across a break command when we met with Mr. Ralph.
Note: The break statement will not allow a loop to satisfy the original condition if it meets a condition that triggers it. It brings the operation to an abrupt end.