What are the Break, Pass and Continue Statements in Python?
break, pass, and continue are control statements used to manage the flow of loops.
1) Break Statement
The break statement stops a loop right away. When it runs, the loop ends, and the program moves to the next line of code outside the loop.
Syntex
for veriable_name in sequence:
if condition:
break # Exits the loop
# Other code
Note : Always remember that when the cursor reaches the break
statement, it immediately stops the loop's iterations and exits out of the loop.
Example:
Problem : Write a Python program where you have a string. The program should loop through the characters in the string and stop (break) the loop when it encounters the letter d.
solution:
data = "aayee dishudii"
for i in data:
print(i)
if i == "d":
break
If also prints d because first, we print the elements and then check the condition.
a
a
y
e
e
d
=== Code Execution Successful ===
2) Continue Statement in Python:
The continue statement skips the rest of the code inside the loop for that round and goes to the next round. It tells the loop to continue without finishing the current iteration.
for variable in sequence:
if condition:
continue
# Code here runs only if condition is False
The syntex is same for Continue Statement with while loop:
while condition:
if condition:
continue
# Code here runs only if condition is False
Note : Always remember that when the cursor reaches the continue statement, it skips the remaining code inside the loop for the current iteration and moves on to the next iteration of the loop.
Example:
Problem : Write a python program to remove all the 100 from the given list using the continue statement in python.
solution:
#using for loop
lst = [10, 100, 23, 34, 100, 200, 100, 400, 200, 100]
for i in lst:
if i == 100:
continue
print(i)
Solution
10
23
34
200
400
200
=== Code Execution Successful ===
Same Problem with while Loop:
#using while loop
lst = [10, 100, 23, 34, 100, 200, 100, 400, 200, 100]
x = 0
while x < len(lst):
if lst[x] == 100:
x = x + 1
continue
print(lst[x])
x = x + 1
Solution
10
23
34
200
400
200
=== Code Execution Successful ===
2) Pass Statement in Python:
The pass
statement does nothing. It is a way to say, "I’ll put code here later." It lets you keep the program running without doing anything at that point.
Example
for i in range(1, 10):
pass
its print nothing, not even error.
What is assert in Python?
The assert statement in Python is used for debugging purposes. It allows you to test if a condition is true. If the condition evaluates to True, the program continues running. If it evaluates to False, an AssertionError is raised, and the program stops executing.
Simple Definition
In simple words, assert is like a check. You use it to make sure something is true in your code. If it's not true, the program will show an error message and stop running.
Syntex
assert condition, "Error message if condition is False"
Example
Here’s a basic example:
x = 5
assert x > 0, "x should be greater than 0"
Output
=== Code Execution Successful ===
If
x = -10
assert x > 0, "x should be greater than 0"
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 2, in <module>
AssertionError: x should be greater than 0
=== Code Exited With Errors ===
In this example, the program checks if x is greater than 0. Since it is, the program continues. If x were less than or equal to 0, it would raise an error with the message "x should be greater than 0".
Tags:
Leave a comment
You must be logged in to post a comment.