How to use assert keyword in python

In this tutorial, you will learn about the assert keyword.it can help you to validate assumptions and verify particular conditions within code. It helps detect problems early in your program, where the cause is clear, rather than later when some other operation fails. A type error in Python, for example, can go through several layers of code before actually raising an Exception if not caught early on.

Assert keyword in python

python assert keyword in any programming language are the finding and correcting mistakes in tools that help in the smooth flow of code. those statements are mainly ideas that a programmer knows or always wants to be true and because of this puts them in code so that failure of these doesn’t allow the code to run further.

x=10
y=0
print("The value of x/y is :")
assert y!=0,"hello world"
print(x/y)

Output:

The value of x/y is :
Traceback (most recent call last):
File "c:\Users\USER\Desktop\python\assert.py", line 4, in <module>
assert y!=0,"hello world"
^^^^
AssertionError: hello world

Leave a Comment

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

Scroll to Top