Python Assert Statements | Assertionerror in Python

Free Python course with 25 real-time projects Start Now!!

In what follows, we will discuss Python Assert Statements. We will see what they are, how to implement and where to use Python Assert Statements.

Moreover, we will discuss Python Assert syntax and example. Also, we will learn using error message and handling Assertionerror Python.

So, let’s start Python Assert Tutorial.

Python Assert Statements | Assertion Error in Python

Python Assert Statements | Assertion Error in Python

What are Python Assert Statements?

Let’s discuss the meaning of Assertion in Python Programming Language in detail:

What is Assertion in Python?

An Assertion in Python or a Python Assert Statement is one which asserts (or tests the trueness of) a condition in your code. This is a Boolean expression that confirms the Boolean output of a condition.

Why Assertion in Python is Used?

This is a debugging tool. If it finds the condition to be true, there is nothing it needs to do. So, it moves over to the next line of code.

If not, it stops all operations and throws an error. It also shows the point of error in the code.

The point of assertions in Python is to help deal with unrecoverable errors.

Python Assert

The assertion in Python programming Language

Where Assertion in Python used?

  • In checking types/ in checking valid input.
  • In checking values of arguments.
  • Checking outputs of functions.
  • As a debugger to halt where an error occurs.
  • In testing code.
  • In detecting abuse of an interface by another programmer.

Python Assert Example and Syntax

Python Assert Statement is one we always want to see true.

Let’s take an example and see what happens when a condition turns false-

>>> assert 2==3

Output

Traceback (most recent call last):
File “<pyshell#0>”, line 1, in <module>
assert 2==3
AssertionError

Hence, this is an AssertionError in Python. We’d use it to catch false assumptions we made while writing the code.

Python Assert Syntax

assert <condition>
assert <condition>, <error message>

Python Assert Example

Now, let’s devise a function to divide two numbers. What happens when we try to divide a number by 0?

>>> def div(p,q):
       assert q!=0
       return p/q
>>> div(2,3)
0.6666666666666666
>>> div(2,0)

Output

Traceback (most recent call last):
File “<pyshell#7>”, line 1, in <module>
div(2,0)
File “<pyshell#5>”, line 2, in div
assert q!=0
AssertionError400;”>

As you can see, this stopped the program and threw a Python AssertionError.

Using an Error Message in Python Assert

Instead of throwing six red lines at the developer, Python may want to display something more sophisticated and less hostile.

For this, we give an error message to the assert statement. Let’s see how.

>>> def div(p,q):
        assert q!=0, "You cannot divide a number by zero\nPlease try again"
        return p/q
>>> div(2,0)

Output

Traceback (most recent call last):
File “<pyshell#10>”, line 1, in <module>
div(2,0)
File “<pyshell#9>”, line 2, in div
assert q!=0, “You cannot divide a number by zero\nPlease try again”
AssertionError: You cannot divide a number by zero
Please try again

Okay, this seems longer than what we got before this, but pull a magnifying glass to the last line. It tells you what really happened, but we can do better than this.

Handling the AssertionError in Python

Let’s put this function in a try block and hope to catch it.

>>> try:
       def div(p,q):
               assert q!=0, "You cannot divide a number by zero\nPlease try again"
               return p/q
except:
       print("So you tried to divide by 0. Please try again")
>>> div(2,0)

Output

Traceback (most recent call last):
File “<pyshell#18>”, line 1, in <module>
div(2,0)
File “<pyshell#17>”, line 3, in div
assert q!=0, “You cannot divide a number by zero\nPlease try again”
AssertionError: You cannot divide a number by zero
Please try again

Wow, this did not work at all. Where’s the “So you tried to divide by 0. Please try again”? Let’s give this another try.

>>> def div(p,q):
	try:
		assert q!=0, "You cannot divide a number by zero\nPlease try again"
		return p/q
	except:
		print("So you tried to divide by 0. Please try again")
>>> div(2,0)

Output

So, you tried to divide by 0. Please try again

Ah, works like a charm this time!

So, this was all in Python Assert. Hope you like our explanation.

Python Interview Questions on Assert Statements

  1. What is assert statement in Python?
  2. What happens when Python assert fails?
  3. How does Python assert work?
  4. What is assertion in Python? Explain with example.
  5. What is the syntax for Python assert statement?

Conclusion

Call it sanity check or paranoia, Python assert statements help us make sure everything’s going fine with our code.

Or maybe we can say that assertions in Python are a secret gift to you from yourself. Twenty days from today, you will open the box and feel grateful.

Hence, we discussed Assert Statements in Python. Moreover, we saw Python Assert example and syntax.

Also, we discussed using error message and handling Assertionerror Python. Have something to add to this tutorial? Feel free to drop it in the comments below.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.