Control Flow and Loops in Python

December 6, 2023

This post covers the fundamentals of control flow and loops in Python, including if/else statements, for and while loops, and nested loops. We will also discuss common use cases and provide code examples for better understanding.

Control flow refers to the order in which a program executes its statements. This is important because it allows us to control the flow of our code based on certain conditions. In Python, control flow is achieved through if/else statements, which allow us to execute different blocks of code depending on whether a condition is true or false.

Let’s take a look at an example:

num = 10

if num > 0:
    print("The number is positive.")
else:
    print("The number is negative.")

In this code, we have a variable called num with a value of 10. The if statement checks if num is greater than 0. Since this condition is true, the code inside the if block will be executed, and we will see the message “The number is positive.” printed to the console. If the condition was false, the code inside the else block would be executed instead.

Next, let’s talk about loops. Loops are used to execute a block of code repeatedly until a certain condition is met. In Python, we have two types of loops: for and while.

The for loop is used to iterate over a sequence, such as a list or a string. Let’s see an example:

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

In this code, we have a list of fruits, and we use a for loop to iterate over each item in the list and print it to the console. The output would be:

apple
banana
orange

The while loop, on the other hand, is used to execute a block of code as long as a certain condition is true. Let’s see an example:

num = 1

while num <= 10:
    print(num)
    num += 1

In this code, we have a variable num with a value of 1. The while loop will continue to execute as long as num is less than or equal to 10. Inside the loop, we print the value of num and then increment it by 1. This will continue until num reaches a value of 11, at which point the loop will stop executing.

Finally, we have nested loops, which are loops within loops. These can be useful in situations where we need to iterate over multiple lists or perform a certain task multiple times with different values. Let’s see an example:

for i in range(1, 4):
    for j in range(1, 4):
        print(i * j)

In this code, we have two for loops, one nested inside the other. The first loop iterates over the numbers 1, 2, and 3, and for each iteration, the second loop iterates over the same numbers. Inside the second loop, we print the product of i and j, which will give us the multiplication table from 1 to 3.

That’s it for this post on control flow and loops in Python. We’ve covered the basics of if/else statements, for and while loops, and nested loops, and provided code examples for better understanding. With these fundamentals, you’ll be able to write more complex programs and tackle a variety of tasks in Python. Happy coding!