Iterative Statements in Python
In programming, it's common to need to perform the same operation multiple times on different inputs or data structures. Iterative statements provide a way to automate this process by repeating a block of code until a certain condition is met. In Python, there are two types of iterative statements: for loops and while loops .
For Loops
A for loop is used to iterate over a sequence of elements, such as a list or a string. The general syntax of a for loop in Python is:
for element in sequence:
# code to execute on each iteration
Here,
element
is a variable that takes on the value of each element in the sequence, one at a time, and
sequence
is the sequence of elements to iterate over. The code inside the loop is executed once for each element in the sequence.
For example, here's a for loop that iterates over a list of numbers and prints each one:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
This will output:
1
2
3
4
5
You can also use a for loop to iterate over the characters in a string:
my_string = "Hello, world!"
for char in my_string:
print(char)
This will output:
H
e
l
l
o
,
w
o
r
l
d
!
You can also use the built-in
range()
function to generate a sequence of numbers to iterate over:
for i in range(10):
print(i)
This will output the numbers 0 through 9.
While Loops
A while loop is used to repeat a block of code while a certain condition is true. The general syntax of a while loop in Python is:
while condition:
# code to execute on each iteration
Here,
condition
is an expression that evaluates to either
True
or
False
. The code inside the loop is executed repeatedly as long as the condition is true.
For example, here's a while loop that counts up from 1 to 10:
i = 1
while i <= 10:
print(i)
i += 1
This will output the numbers 1 through 10.
It's important to be careful when using a while loop, as it's possible to create an infinite loop if the condition never becomes false. For example, this code:
while True:
print("Hello, world!")
will print "Hello, world!" forever, because the condition is always true. If you accidentally create an infinite loop, you can interrupt the program by pressing Ctrl+C in the terminal.
Break and Continue Statements
Both for loops and while loops can be controlled using break and continue statements.
A
break
statement is used to exit a loop prematurely, regardless of whether the loop condition has been met. For example, in this code:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
break
print(num)
the loop will exit after printing the numbers 1 and 2, because the
break
statement is executed when
num
is 3.
A
continue
statement is used to skip over an iteration of the loop and move on to the next one. For example, in this code:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue
print(num)
only the odd numbers (1, 3, and 5) will be printed, because the
continue
statement is executed when
num
is even.
Conclusion
Iterative statements are a fundamental concept in programming, and Python provides two powerful types of loops: for loops and while loops. You can use these loops to automate repetitive tasks and process large data sets. By combining loops with control statements like
break
and
continue
, you can create complex algorithms and solve a wide variety of programming problems.