Looping and Iteration in Python
In this article, we will cover everything you need to know about looping and iteration in Python.
Updated April 5, 2023
Looping and iteration are essential concepts in any programming language, including Python. Loops are used to execute a block of code repeatedly until a certain condition is met. In Python, there are two types of loops - for loops and while loops. Iteration refers to the process of repeatedly executing a piece of code. It can be accomplished using loops or other constructs.
In this article, we will cover everything you need to know about looping and iteration in Python, including the different types of loops, the range function, and how to use loops to iterate over lists and other data structures.
Types of Loops in Python:
As mentioned earlier, there are two types of loops in Python - for loops and while loops. Let’s take a look at each of them in detail.
A for loop is used to iterate over a sequence, such as a list, tuple, or string. The basic syntax for a for loop in Python is:
for variable in sequence: # code to be executed
In this syntax, “variable” is a variable that takes on the value of each item in the sequence, one at a time. The “sequence” is the list, tuple, or string that we want to iterate over. The code inside the loop is executed for each item in the sequence.
Let’s take a look at an example to illustrate how a for loop works:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
apple banana cherry
In this example, we have a list of fruits. We use a for loop to iterate over the list and print each item in the list.
A while loop is used to execute a block of code repeatedly as long as a certain condition is true. The basic syntax for a while loop in Python is:
while condition: # code to be executed
In this syntax, “condition” is a boolean expression that is checked at the beginning of each iteration of the loop. If the condition is true, the code inside the loop is executed. This continues until the condition becomes false.
Let’s take a look at an example to illustrate how a while loop works:
i = 1 while i < 6: print(i) i += 1
1 2 3 4 5
In this example, we use a while loop to print the numbers 1 to 5. The condition for the while loop is “i < 6”, which means that the loop will continue as long as “i” is less than 6. Inside the loop, we print the value of “i” and increment it by 1.
The Range Function:
The range function is a built-in function in Python that generates a sequence of numbers. It is often used in for loops to iterate over a sequence of numbers. The basic syntax for the range function is:
range(start, stop, step)
In this syntax, “start” is the starting number of the sequence (inclusive), “stop” is the ending number of the sequence (exclusive), and “step” is the increment between each number in the sequence (default is 1).
Let’s take a look at an example to illustrate how the range function works:
for i in range(1, 6): print(i)
1 2 3 4 5
In this example, we use the range function to generate a sequence of numbers from 1 to 5. We then use a for loop to iterate over this sequence and print each value.
To exit a loop prematurely, we can use the break statement. This statement terminates the loop immediately and control is passed to the next statement after the loop.
for i in range(10): if i == 5: break print(i)
0 1 2 3 4
In the above example, the loop is terminated when the value of i is equal to 5. As soon as the value becomes 5, the break statement is executed and the loop is exited.
Another statement that can be used in loops is the continue statement. This statement skips the current iteration of the loop and continues with the next iteration.
for i in range(10): if i % 2 == 0: continue print(i)
1 3 5 7 9
In the above example, we are printing only the odd numbers between 0 and 9. When i is even, the continue statement is executed, which skips the current iteration and goes to the next iteration.
In this article, we have discussed the basics of looping and iteration in Python. We have covered the for loop, range() function, and the while loop. We have also seen how to use the break and continue statements to control the flow of the loop. With this knowledge, you can write more efficient and concise code in Python.