How to End a Loop in Python

Python is a great language for beginners and experienced programmers alike. One of its key features is its ease of use when it comes to loops. In this blog post, we’ll show you how to end a loop in Python.

Checkout this video:


In this tutorial, we’ll learn how to exit a loop early, how to skip the rest of the instructions in the loop, and how to end a loop before it’s even begun. We’ll also take a look at some real-world examples of where and when these concepts might come in handy.

What is a Loop?

In computer programming, a loop is a sequence of instructions that is repeatedly executed until a certain condition is reached. Loops are handy because they save time and effort by allowing you to write a set of instructions once and have them executed repeatedly.

There are two types of loops in Python: the for loop and the while loop.

The for loop is used to iterate (repeat) over a sequence of values. The while loop is used to execute a set of instructions until a condition is met.

Typically, you use a for loop when you know the number of iterations (repetitions) ahead of time. For example, if you want to print the numbers from 1 to 10, you can use a for loop:

for i in range(1, 11):

On the other hand, you use a while loop when you do not know how many iterations will be required ahead of time. For example, if you want to keep asking the user for input until they enter the word “quit”, you can use a while loop:

while True: # this will run forever until we break out of the loop

user_input = input(“Please enter some text”)

if user_input == “quit”:
break # this will exit the while loop

Types of Loops

There are several type of loops that are used in programming. The most common type of loop is the while loop. This loop will continue running until a specific condition is met. For example, you might use a while loop to print out all of the numbers from 1 to 10.

Another type of loop is the for loop. This loop will run a certain number of times. For example, you might use a for loop to print out the first 10 numbers in a list.

You can also use nested loops, which means having one loop inside of another. This can be helpful if you want to run multiple loops at the same time.

The while Loop

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

We generally use this loop when we don’t know the number of times to iterate beforehand.

Here is the general form of the while loop:

while test_expression:
Body of while

The body of the while loop is executed as long as the test_expression evaluates to True. If the condition is False, then the body of the loop is skipped and control jumps to the next line after the while loop.

The for Loop

Python’s for loop is a direct way to iterate over a sequence of objects. In most cases, you will want to use a for loop when you know how many iterations you need to run, and you want each iteration to run on a different item in a sequence. For example, if you wanted to print out each element in a list, you could use a for loop:

>>> my_list = [1,2,3,4,5]
>>> for i in my_list:
… print(i)


Nested Loops

Python allows for nested loops. This means that you can have a loop within a loop. The inner loop will be executed one time for each iteration of the outer loop:

for i in range(3):
for j in range(2):
print(“i =”, i, “; j =”, j)


The break Statement

The break statement in Python terminates a loop and transfers execution to the statement immediately following the loop.

In the simplest form, you can use break to terminate a for or while loop:

for i in range(10):
if i == 5:

while True:
cmd = input(“Enter a command: “)
if cmd == ‘quit’:

The continue Statement

The continue statement in Python returns the control to the beginning of the current loop. It skips the execution of the rest of the statements in the current iteration of the loop and returns control to the loop. The continue statement can be used in both while and for loops.

If you are using nested loops, the continue statement will return control to the inner loop.

The pass Statement

If you want to end a loop prematurely, you can use the break statement. The break statement causes the program to immediately leave the loop and continue execution at the next statement after the loop.

Consider this simple for loop:
for i in range(10):
if i == 5:
print(i) “`
In this example, the loop will iterate 10 times. However, as soon as the value of i is equal to 5, the break statement is encountered, causing the program to exit the loop and continue at the next statement after the loop. The output of this program would therefore be 0, 1, 2, 3, 4.

There may also be times when you want to exit a loop early but don’t want anything to happen after the loop has ended. In these cases, you can use the pass statement. The pass statement simply tells Python to do nothing — it doesn’t cause any action to be taken and can be used as a placeholder for code that hasn’t been written yet. For example:
for i in range(10):
if i == 5:
print(i) “`
This code will produce exactly the same output as the previous code example because Python will do nothing when it encounters the pass statement.

Ending a Loop

There are several ways to end a loop in Python. The most common is to use the keyword “break.” This keyword will immediately stop the execution of the loop and move on to the next statement after the loop. For example, let’s say we have a list of numbers and we want to print out only the even numbers. We could do this with a while loop:

numbers = [1,2,3,4,5,6,7,8,9]
i = 0
while i < len(numbers): if numbers[i] % 2 == 0: # if the number is even print(numbers[i]) # print it out i += 1 # increment i

Scroll to Top