Skip to Content

Python for Fintech Professionals: Loops

Loops in Python, such as "for" and "while", enable code execution repeatedly based on a condition or a sequence.
This is the eleventh blog on Python for fintch professionals.

Loops are a fundamental concept in programming, allowing us to execute code multiple times without the need to write repetitive instructions. In Python, we primarily use two types of loops: while loops and for loops. Each serves different purposes based on how we want to control the flow of our code.

The while Loop

The "while" loop executes a block of code as long as a specified condition remains true. This is particularly useful when you don't know in advance how many times you need to run the loop. Here’s the basic syntax:

while define_condition:
define_instruction()

As long as "define_condition" evaluates to "True", the instructions within the loop will continue to execute.

Here's a simple example that prints numbers from 1 to 5:

count = 1
while count <= 5:
  print(count)
count += 1

Infinite Loops

An important aspect of while loops is the potential for creating infinite loops. If the condition never evaluates to False, the loop will run indefinitely. For instance:

while True:
  print('This sentence will print indefinitely unless you press Ctrl + C on your keyboard.')

  To avoid infinite loops, it's crucial to include a mechanism that can break out of the loop when needed.

The for Loop

The "for" loop, on the other hand, is used when you want to iterate over a sequence (like a list or a range of numbers). It is particularly useful when you know in advance how many iterations you want to perform. The syntax looks like this:

for control_variable in sequence:
define_instruction()

Here’s how you can use a `for` loop to print numbers from 1 to 100:

for i in range(1, 101):
print(i)

In this example, "range(1, 101)" generates numbers from 1 to 100, and the "for" loop iterates over these values.

Iterating Over Lists

If you have an existing list, you can directly iterate through its contents:

my_list = [1, 2, 3]
for item in my_list:
print(item)

Control Statements: "break" and "continue"

In both "while" and "for" loops, we can use "break" and "continue" statements to control the flow of execution.

break: This statement immediately exits the loop.

continue: This statement skips the current iteration and continues with the next one.

Let’s say we want to print numbers from 1 to 10, but skip the number 2 and stop the loop entirely when we reach 11:

for i in range(1, 100):
    if i == 2:
        continue  # Skip the current iteration
    elif i == 11:
        break  # Exit the loop
    print('Current value is', i)

This code will print values from 1 to 10, skipping 2.

Conclusion

Loops are an essential part of Python programming, allowing for efficient and dynamic execution of code. Understanding how to use "while" and "for" loops, along with control statements like "break" and "continue", will greatly enhance your coding skills and enable you to handle repetitive tasks effectively.


Share this post
Tags
Sign in to leave a comment
Python for Fintech Professionals: Conditionals
Conditionals allow code execution based on whether a specific condition is true or false, using "if", "elif", and "else" statements.