Loop Statements in Python

Posted by

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:


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:

This would output the following:

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:

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:


This would output the following:

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:

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:

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:

To print the even numbers between 0 and 10, you can use the following for loop:

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:


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:

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:

This will print the even numbers between 2 and 10 (2, 4, 6, 8, 10).

Leave a Reply

Your email address will not be published. Required fields are marked *