A loop is a control structure that can execute a statement or group of statements repeatedly. Python has three types of loops:
for loops, and nested loops.
while loop will repeatedly execute a code block as long as a condition evaluates to
The condition of a
while loop is always checked first before the block of code runs. If the condition is not met initially, then the code block will never run.
while condition: # Code inside
This loop will only run 1 time:
hungry = Truewhile hungry:print("Time to eat!")hungry = False
This loop will run 5 times:
i = 1while i < 6:print(i)i = i + 1
for loop can be used to iterate over and perform an action one time for each element in a list.
Proper for loop syntax assigns a temporary value, the current item of the list, to a variable on each successive iteration:
for <temporary value> in <a list>:
for loop bodies must be indented to avoid an
dog_breeds = ["boxer", "bulldog", "shiba inu"]# Print each breed:for breed in dog_breeds:print(breed)
Loops can be nested inside other loops. Nested loops can be used to access items of lists which are inside other lists. The item selected from the outer loop can be used as the list for the inner loop to iterate over.
groups = [["Jobs", "Gates"], ["Newton", "Euclid"], ["Einstein", "Feynman"]]# This outer loop will iterate over each list in the groups listfor group in groups:# This inner loop will go through each name in each listfor name in group:print(name)
In a loop, the
break keyword escapes the loop, regardless of the iteration number. Once break executes, the program will continue to execute after the loop.
numbers = [0, 254, 2, -1, 3]for num in numbers:if (num < 0):print("Negative number detected!")breakprint(num)
In this example, the output would be:
02542Negative number detected!
continue keyword is used inside a loop to skip the remaining code inside the loop code block and begin the next loop iteration.
big_number_list = [1, 2, -1, 4, -5, 5, 2, -9]# Print only positive numbers:for i in big_number_list:if i < 0:continueprint(i)
In this video, you will learn how to use the for and while loops in a Python script.
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions