aku00x
Realtime Debugging Master
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
In this tutorial you will learn:
While loop in Python
A while loop in Python is just like any in any other programming language. Just like a for loop a while loop also needs initialization, condition and increment. A while loop keeps on running until the condition is true. If we want the loop to run indefinitely then we can pass 1 or true in while loop. Let’s go through the examples we did in for loop using while loop.
Example:
numbers = [67,85,23,45,6,7,8,2,1]
index = 0
print("\n\nWHILE Loop Example")
print("This list contains the following: ")
while index
Nested while loop in Python
Loops can be nested at many levels but there is one big disadvantage and that is it increases the computation time of your code by using up most the resources of the hardware in case of large datasets or while executing a complex block of code many number of times. But in some cases they are quite useful such as for printing list of lists.
Example:
sum = 0
index = 0
print("\n\nSum of numbers: ")
while index
Break statement in while loop
Break statement is used to stop the execution of a block of code and it does its job in while loop as well.
Example:
myLists = [["Testing", 1, "Python"], ["John", "Mike", "Todd"]]
print("\n\nPrinting elements in list of lists")
index = 0
listItemIndex = 0
while index len(myLists):
while listItemIndex len(myLists[index]):
print(myLists[index][listItemIndex], end=" ")
listItemIndex+=1
index+=1
Continue statement in while loop
This statement is used to skip a loop iteration and its usage is similar to that of for loop.
Example:
index = 0
print("\n\nStop the loop if 8 is found in numbers: ")
while index
Book traversal links for Python While Loop
Download
- While loop in Python
- Nested while loop in Python
- Break statement in while loop
- Continue statement in while loop
While loop in Python
A while loop in Python is just like any in any other programming language. Just like a for loop a while loop also needs initialization, condition and increment. A while loop keeps on running until the condition is true. If we want the loop to run indefinitely then we can pass 1 or true in while loop. Let’s go through the examples we did in for loop using while loop.
Example:
numbers = [67,85,23,45,6,7,8,2,1]
index = 0
print("\n\nWHILE Loop Example")
print("This list contains the following: ")
while index
Nested while loop in Python
Loops can be nested at many levels but there is one big disadvantage and that is it increases the computation time of your code by using up most the resources of the hardware in case of large datasets or while executing a complex block of code many number of times. But in some cases they are quite useful such as for printing list of lists.
Example:
sum = 0
index = 0
print("\n\nSum of numbers: ")
while index
Break statement in while loop
Break statement is used to stop the execution of a block of code and it does its job in while loop as well.
Example:
myLists = [["Testing", 1, "Python"], ["John", "Mike", "Todd"]]
print("\n\nPrinting elements in list of lists")
index = 0
listItemIndex = 0
while index len(myLists):
while listItemIndex len(myLists[index]):
print(myLists[index][listItemIndex], end=" ")
listItemIndex+=1
index+=1
Continue statement in while loop
This statement is used to skip a loop iteration and its usage is similar to that of for loop.
Example:
index = 0
print("\n\nStop the loop if 8 is found in numbers: ")
while index
Book traversal links for Python While Loop
- ‹ Python For Loop
- Up
- Python Functions ›
Download
You must upgrade your account or reply in the thread to view hidden text.