Introduction
There are two types of loops that you can employ in Python 3. You have for loops and while loops. You can use them for repeat tasks. As a result, the repetitive tasks will happen automatically, making the process more efficient. Unfortunately, your loops can encounter some problems. At times, your program may run into an issue where you need it to skip a part of the loop or exit it entirely. Or maybe you need it to ignore the external factor that is influencing the program. If this is something you want to add to your program, you need to use the break
, continue
, and pass
statements.
In this guide, we will discuss how you can use the break, continue, and pass statements when working with loops in Python 3.
How to Use Break Statement
The break
statement lets you exit the loop in the presence of an external influence. You will need to place this statement in the code of your loop statement. We typically use it with a conditional if
statement. To help you understand, let’s take the example of the following loop. Here, we are using a break
statement in a for
loop:
1 2 3 4 5 6 7 8 9 |
number = 0 for number in range(10): if number == 5: break # break here print('Number is ' + str(number)) print('Out of loop') |
As you can see, we initialize the variable number
at 0. We then put in a for
statement to make the loop. The condition is that the number
is less than 10. After that, we added an if
statement. It states that if the variable number
equals 5, then the loop will be broken. Another factor in the loop code is the print()
statement which executes repeatedly with each loop until it breaks. Finally, the last print()
statement allows us to be alerted when we exit the loop.
Adding and running the code with this break
statement will give you an output like this:
This shows that once the variable number
became equivalent to 5, the loop broke. This means the program is out of the loop now.
How to Use Continue Statement
With the continue
statement, you can successfully skip only a certain part of the loop. Thus, when your program encounters a trigger, it will skip a preset part of the loop and will continue to complete the rest of it from the top in a new iteration. Again, you will need to use the if
statement. Let’s use the same example as the previous section. Here, instead of a break
statement, we will apply a continue
statement:
1 2 3 4 5 6 7 8 |
number = 0 for number in range(10): if number == 5: continue # continue here print('Number is ' + str(number)) print('Out of loop') |
The obvious difference between running break
statements and continue
statements is that with the latter, we do not exit the loop. Instead, the code continues in the next iteration even when the variable number
is equivalent to 5. The output will look something like this:
As you can see, the output does not show the value Number is 5
. The loop simply continues beyond it. It prints outlines for the numbers 6, 7, 8, 9, and 10 before it exits the loop. This is useful when you want to avoid some conditional code.
How to Use Pass Statement
The pass
statement lets you deal with the external trigger without having to disrupt the loop. This means that regardless of the outside factor, the loop will continue printing lines unless it encounters another statement. Similarly to the other two statements, we will add this statement in the loop code after a conditional if
statement. Let’s apply this statement to our example:
1 2 3 4 5 6 7 8 9 |
number = 0 for number in range(10): if number == 5: pass # pass here print('Number is ' + str(number)) print('Out of loop') |
With the pass
statement, we are telling the program to ignore the fact that the variable number
is equal to 5. Running this code will give you this output:
This shows that the trigger did not have any impact whatsoever on the loop. It seems as if there was no conditional statement at all. As such, you can use the pass
statement to make minimal classes or even use it as a placeholder on codes that are still in the making.
Finally, you can take a look at our other tutorials that will help you get familiar with what you can do with Python:
- Follow along to learn how to extract Twitter data using Python.
- Learn how to retrieve NASDAQ per minute data using Python.
- Check how to install Python 3 and set up a local programming environment on Ubuntu 16.04.
- You can also learn how to utilize CloudSigma Python script to automate the conversion of a drive snapshot into a full drive on our platform.
Conclusion
In this guide, we helped you understand what each of the break
, continue
, and pass
statements are and how they work. Now you know how you can add them to the block of code of your loop statements in Python 3. They will help you make your for
and while
loops more effective in your program.
Happy Computing!
- Removing Spaces in Python - March 24, 2023
- Is Kubernetes Right for Me? Choosing the Best Deployment Platform for your Business - March 10, 2023
- Cloud Provider of tomorrow - March 6, 2023
- SOLID: The First 5 Principles of Object-Oriented Design? - March 3, 2023
- Setting Up CSS and HTML for Your Website: A Tutorial - October 28, 2022