DEV Community

Cover image for Mastering Control Flow: A Beginner's Guide to Python Loops
Abdulla Fajal
Abdulla Fajal

Posted on

Mastering Control Flow: A Beginner's Guide to Python Loops

Control flow is a fundamental concept in programming that allows developers to control the order of execution of statements in their code. In Python, there are several constructs that allow you to implement control flow, with loops being one of the most commonly used. In this beginner’s guide to Python loops, we will explore how to use loops to iterate over sequences, perform repeated actions, and more.

What are Loops?

Loops are constructs in programming that allow you to repeat a set of instructions multiple times. In Python, there are two types of loops: for loops and while loops. Both loops allow you to execute a block of code repeatedly, but they differ in how they control the number of iterations.

The for Loop

The for loop is used to iterate over a sequence of items. A sequence can be any collection of items, including lists, tuples, and strings. The basic syntax of a for loop is as follows:

for item in sequence:
    # do something with item
Enter fullscreen mode Exit fullscreen mode

The for loop starts by initializing a variable item to the first item in the sequence. It then executes the block of code inside the loop, which can access the current value of item and perform some action on it. After the block of code is executed, the loop moves on to the next item in the sequence and repeats the process until all items have been processed.

Let’s look at an example of a for loop that iterates over a list of numbers and prints out each number:

numbers = [1, 2, 3, 4, 5]
for number in numbers:
    print(number)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

In this example, the for loop iterates over the list of numbers, and for each number, it prints out the value of the variable number. The loop continues until all items in the list have been processed.

The range() Function

In some cases, you may need to execute a block of code a specific number of times, rather than iterating over a sequence of items. In such cases, you can use the range() function to generate a sequence of numbers that can be used in a for loop.The range() function takes one, two, or three arguments, depending on how you want to use it. The basic syntax is as follows:

range(stop)
range(start, stop)
range(start, stop, step)
Enter fullscreen mode Exit fullscreen mode

The range() function generates a sequence of numbers that starts at start, ends at stop (exclusive), and increments by step (default is 1). Here are some examples:

# generate a sequence of numbers from 0 to 4 (exclusive)
for i in range(5):
    print(i)
# generate a sequence of numbers from 2 to 5 (exclusive)
for i in range(2, 5):
    print(i)
# generate a sequence of numbers from 0 to 10 (exclusive), incrementing by 2
for i in range(0, 10, 2):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
2
3
4
0
2
4
6
8
Enter fullscreen mode Exit fullscreen mode

The while Loop

The while loop is used to execute a block of code repeatedly as long as a certain condition is true. The basic syntax of a while loop is as follows:

while condition:
    # do something
Enter fullscreen mode Exit fullscreen mode

The while loop starts by checking the condition. If the condition is true, the loop executes the block of code inside it. After the block of code is executed, the condition is checked again. If it is still true, the block of code is executed again, and the process repeats until the condition becomes false.

Let’s look at an example of a while loop that counts down from 5 to 1:

count = 5
while count > 0:
    print(count)
    count -= 1
Enter fullscreen mode Exit fullscreen mode

Output:

5
4
3
2
1
Enter fullscreen mode Exit fullscreen mode

In this example, the while loop executes as long as the value of count is greater than 0. Each time the loop iterates, it prints the current value of count and subtracts 1 from it, until count becomes 0 and the loop exits.

For Read More Click Here Espere.in

Top comments (0)