Python Loops
What are Python Loops?
In programming, a loop is a control flow structure that allows specified block of code to be executed repeatedly or in succession based on a specified condition (i.e., refers to a sequence of instructions or code being repeated until a specific end result is achieved).
- AKA Conditional Loops or Repetitive Control Structures
Loops are important for automating repetitive tasks and iterative data structures (e.g., lists, dictionaries)
Two Main Types of Loops
Python has two primitive loop commands
- For Loop: AKA a definitie loop.
- While Loops: AKA an indefinite loop.
A for loop is ideal when you know the number of iterations you want to run (usually more simple, easier to code and understand). A while loop should only be used when you do not know the number of iterations you want to run.
Rule of Thumb: Always use the simplest bit of code you can use to accomplish what you want. This means using a for loop over a while loop.
How are the number of repetitions defined?
For Loops
In a for loop, the number of repetitions is determined by the length of the iterable provided in the loop header. Therefore, the number of times in which the body of the loop is executed is the length of the iterable. Example:
# Example: Using a for loop to iterate over a list
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
In this example, the loop iterates over each element in the list. The body of the loop specifies that a print should be executed. As long as there are elements in the list, the loop body will be executed. Once all elements have been processed, the loop will exit.
While Loops
The number of repetition is based on conditional criteria defined in the loop structure in the form of a logical expression. This means that the loop will continue to execute as long as the condition specified in the loop header is True. Example:
# Example: Using a while loop to iterate until a condition is met
count = 0
while count < 5:
print(count)
count += 1 # Loop control variable
In this example, the loop continues to execute as long as the condition count < 5 is true. The print(count) statement and the increment of count are part of the loop body. Once count reaches 5, the condition becomes false, and the program exits the loop, moving on to the next sequence of code after the loop.
Components of a Loop Structure
There are three components of a loop structure:
-
The initialization is an expression that initializes the loop — it's executed once at the beginning of the loop.
-
Is the first statement to be executed and will be executed only once i.e. at the start of the loop.
-
This is generally used for initializing the loop variable.
-
This statement will be executed both at the entrance and at the end of each iteration.
-
The condition is to be tested each time the loop iterates or is performed.
-
The loop will only continue if the value it returns is true, otherwise it will exit.
-
This statement will be executed at the end of each iteration before rechecking the condition.
-
It is used to update the value of the variable.
-
This is so the condition will eventually be met and loop will end e.g., in counter-loops; count += 1
Example:
# For Loop
for loop_var in iterable:
# initialization (optional)
loop_var = some_val
# Body of the loop to execute some function, e.g.,
print(loop_var)