How to Break for Loop in Python

Share This Post

Python break for loop examples is a great way to learn how to use the Python break statement. A Python break statement allows you to exit a loop prematurely. In this blog post, we will provide Python break-for-loop examples so that you can become a Python break master!

How to break for Loop

The Python break statement is a great tool to use when you need to exit a loop prematurely.

Here is the first Python break example:

i = 0
while i <= 100:
	if i == 50:
		break
	print(i)
	i += 1

In the example above, we use the Python break statement to exit the loop when the value of `i` is equal to `50`. The output of this Python code would be:

“`

0

49

“`

Read more: 10 Best Ways To Use While Loop In Python

As you can see, the break statement allowed us to exit the loop when `i` was equal to `50`. Let’s take a look at another Python break example:

In this second break example, we use the break statement to exit the loop when the value of `i` is equal to `75`. The output of this Python code would be:

“`

0

74

“`

As you can see, the break statement allowed us to exit the loop when `i` was equal to `75`. Let’s take a look at one more Python break example:

[/php]

#python

i = 0
while i <= 100:
if i % 25 == 0:
break
print(i)
i += 15

[/php]

In this break for loop example, we use the break statement to exit the loop when the value of `i` is divisible by `25`. The output of this Python code would be:

0

15

30

45

60

75

As you can see, the break statement allowed us to exit the loop when `i` was divisible by `25`.

Read More: 10 Ways to Use Python For Loop, What is For Loop in Python?

spot_img

Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img