In Python, there are two types of loop statements:
for
loops and
while
loops.
For Loop in Python
It are used to iterate over a sequence (such as a list, tuple, or string) or other iterable object. Here is the general syntax of a
for
loop in Python:
1 2 | for variable in iterable: statements |
Here,
variable
is a variable that takes on the value of each element in the
iterable
one at a time, and
statements
are the statements that are executed for each iteration of the loop. For example, consider the following code that uses a
for
loop to iterate over a list of integers and print their squares:
1 2 3 | numbers = [1, 2, 3, 4, 5] for number in numbers: print(number**2) |
This would output the following:
1 2 3 4 5 | 1 4 9 16 25 |
While Loop in Python
It are used to execute a block of statements repeatedly as long as a certain condition is true. Here is the general syntax of a
while
loop in Python:
1 2 | while condition: statements |
Here,
condition
is a boolean expression that is evaluated at the start of each iteration of the loop. If it is
True
, the loop continues and the
statements
are executed. If it is
False
, the loop terminates and control is transferred to the next statement after the loop.
Here is an example of a
while
loop that counts down from 5 and prints the numbers:
1 2 3 4 | count = 5 while count > 0: print(count) count -= 1 |
This would output the following:
1 2 3 4 5 | 5 4 3 2 1 |
You can also use the
break
and
continue
statements within loops to control the flow of execution. The
break
statement will exit the loop, while the
continue
statement will skip the rest of the current iteration and move on to the next one. Iterating over a list in Python
You can use a
for
loop to iterate over a list of elements like this:
1 2 3 | fruits = ['apple', 'banana', 'mango'] for fruit in fruits: print(fruit) |
This will print each fruit in the list on a separate line.
Iterating over a string in Python
You can also use a
for
loop to iterate over the characters in a string like this:
1 2 3 | s = 'hello' for c in s: print(c) |
This will print each character in the string on a separate line.
Using the range function in Python
You can use the
range
function to specify a range of numbers to iterate over. The
range
function takes three arguments:
start
,
stop
, and
step
. The
start
argument is the first number in the range, the
stop
argument is the last number in the range, and the
step
argument is the size of the step between numbers.
For example, to print the numbers 0 through 9, you can use the following
for
loop:
1 2 | for i in range(10): print(i) |
To print the even numbers between 0 and 10, you can use the following
for
loop:
1 2 | for i in range(0, 10, 2): print(i) |
This will print the numbers 0, 2, 4, 6, 8.
I hope this helps! Let me know if you have any more questions about
for
loops in Python.
Here is some more information about
while
loops in Python:
Basic syntax
A
while
loop consists of a block of code and a condition. The block of code is executed repeatedly as long as the condition is
True
. Here is an example of a basic
while
loop:
1 2 3 4 | i = 0 while i < 10: print(i) i += 1 |
This will print the numbers 0 through 9.
Using the break statement in Python
You can use the
break
statement to exit a
while
loop prematurely. For example:
1 2 3 4 5 6 | i = 0 while True: print(i) i += 1 if i == 10: break |
This will print the numbers 0 through 9 and then exit the loop when
i
becomes 10.
Using the continue statement in Python
You can use the
continue
statement to skip the rest of the current iteration and move on to the next one. For example:
1 2 3 4 5 6 | i = 0 while i < 10: i += 1 if i % 2 == 1: continue print(i) |
This will print the even numbers between 2 and 10 (2, 4, 6, 8, 10).