We would cover Python Loops in this lesson. A loop in programming is a piece of code or block of codes that execute repeatedly a number of times based on some criteria. This criteria is known as the loop condition. The statement or statements to be executed are is called the body of the loop.
The loop conditions is further controlled by the loop variable (a variable that is checked for each time the loop executes. An execution of the body of the loop is called an iteration.
The behavior of a loop is illustrated in Figure 1.0
We would now consider three types of loop construct in Python
- For Loops
- While Loops
- Nested Loops
A while loop in Python repeats a statement or block of statements while a condition is true. The condition must be an expression that evaluates to either True or False (remember non-zero values are True by default while zero or null values are False in Python)
An example of a while loop is given below. The loop prints out numbers from 1 to 10
i = 0 while i < 11: # condition print(i) i = i+1 print("The End")
In the program above, the program checks if i is less than 11 and if not, it prints i and increments the value of i. It then checks again, this time i is increased from 0 to 1. The process continues until i becomes 11, then the loops terminates and the program prints “The End” which is the statement following the loop
The For loop in Python is used to execute a statement or block of statements a number of times specified in the condition. The condition could be the number of items in a sequence(list or range) specified.
An example of a for loop it given for a loop continues to execute for i = 1 to 10
for i in range(1,10) print(i)
The example below uses a for loop to print all the items in list1. In this case the loop continues to execute as long as there are items in the list.
list1 = ["Kany", "Osoo", "Adii", "Jackie", "Ottie", "Haty", "Ollie", "Saff" ] for item in list1: print(item)
A loop can be nested within a loop. Let’s say we want to prints a two dimensional array. We can use two loops: the first loop iterates through the row while the second (nested loop) iterates through the columns.
This means that for each execution of the outer loop, the nested loop executes completely. An example is given below:
for i in range(1,11): print("i = ":i) for j in range(1,6): print(j)
Loop Control Statements
Python provides 3 loop control statements which are explained below:
Break Statement: This statement is used to completely terminate a loop and continue execution with the statements after the loop
Continue Statement: This statement is used to terminate the current iteration of a loop thereby skipping the remainder of the loops body. Continue statement automatically causes the loop to start a new iteration.
Pass Statement: This is a Python-specific statement used when a statement is required but you have not provided any statement. You can simply use pass to tell the compiler that the statements are not available or are not ready yet.
Watch the Video Lesson