Python Assert Statements | Assertionerror in Python

1. Objective – Python Assert

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

2. What are Python Assert Statements?

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

a. 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.
Do you know about Python variables and Data Types

b. 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

c. 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.

3. 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-
You must read about Python Syntax

>>> assert 2==3

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.

a. Python Assert Syntax

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

b. 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)

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
AssertionError
As you can see, this stopped the program and threw a Python AssertionError.
Have a look at Python Classes and Object-Oriented Programming

Join DataFlair on Telegram

4. 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)

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.

Python Interview Questions

5. 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)

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.
Let’s discuss Exception Handling in Python

>>> 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)

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

6. Conclusion: Python Assert

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.

See also – 
Python Iterator
For reference

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.