In this article, we will discuss loop control flow statements in Python, which function similarly to those in other programming languages like C, C++, and Java.
Loops are essential when you need to execute the same block of code multiple times to achieve the desired result. In Python, you can control this repetitive execution with loop control statements.
Python has two primary types of loops:
- Python While Loop
- Python For Loop
1. Python WHILE Loop
A while loop keeps running as long as a given condition is true. Once the condition becomes false, the loop stops. The syntax for a while loop is similar to if-else statements.
Syntax:
while condition:
# Statement(s) to execute while the condition is true
statement1
statement2
...
last_statement
Example:
count = 0
while count < 5:
print(count)
count += 1
In this example, the loop will print the numbers 0 through 4. The loop continues as long as the condition (count < 5) remains true.
2. Python For Loop
Another loop statement – for loop, is the best for when you determine in advance how many times you need to execute the block of statements placed in the loop.
It allows you to perform an operation on each element in a list or character in a string.
Syntax:
for variable in sequence:
# Statement(s) to execute for each item in the sequence
statement1
statement2
...
last_statement
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
In this example, the for loop iterates over each item in the fruits list and prints it. The loop runs once for each item in the list, making it a straightforward way to process collections of data.
Python offers two main types of for
loops: the List – for loop and the Range – for loop.
2.1 List – for Loop
The List – for loop is straightforward. You provide a list of items, and the loop iterates over each item in that list.
Example:
for integer in [0, 1, 2]:
print("Integer :" + str(integer))
print("Integer Multiplication :" + str(integer * integer))
In this example, the loop processes each integer in the list, printing the integer and its square.
Output:
Integer :0
Integer Multiplication :0
Integer :1
Integer Multiplication :1
Integer :2
Integer Multiplication :4
2.2 Range – for Loop
The Range – for loop provides more control over the sequence of numbers you want to iterate through. You can specify the start, end, and step values to define the range.
Syntax:
for var in range(start_pos, end_pos, step):
# Statement(s)
- start_pos: The starting point of the range (inclusive).
- end_pos: The end point of the range (exclusive).
- step: The increment between each value in the range.
Example 1:
for i in range(1, 20, 2):
print("Range value :" + str(i))
In this example, the loop prints numbers from 1 to 19, incrementing by 2 each time.
Example 2: Decrement python loop
for i in range(20, 0, -2):
print("Range value :" + str(i))
Here, the loop prints numbers from 20 to 2, decrementing by 2 each time.
Summary
In Python, for
loops come in two main types: List – for loops and Range – for loops.
- List – for loops are used to iterate over each item in a list, executing a block of code for each item.
- Range – for loops provide control over the sequence of numbers by specifying the start, end, and step values, allowing for more flexible iterations.
Both types of loops help simplify repetitive tasks and manage how code is executed across a sequence of values.
ADDITIONAL RESOURCES:
- How to define and call functions in Python?
- Control statements in Python: break, continue, pass keywords
- If-Else Statement in Python
- For loop and While loop in Python
- Python IDLE, Shell and Command Prompt (Guide)
- Exception handling in Python
- Python Variables, data types, and values
- Python function arguments
- Python interactive mode and script mode programming
- How To Install and Download Python Anaconda?
- Python Raise Keyword
- What is Python? Why it is most loved programming language?
- Write your first Python program!
- Zen of Python and Anti-Zen of Python
- Best Python books to read and learn!