Python assert statement explanation with examples

Python assert statement:

Assertion statements are useful in many ways. For example, if you have a function that can accept only positive integers, you can use an assertion statement at the beginning of the function to test if the parameter is a positive integer or not. If not, it will throw an error.

Assertion statements are used for debugging as it halts the program as soon as any error occurred.

Python has a built-in way for assertion. This post will show you how to use assertion in Python with examples.

assert statement:

The assert statement is used as an assertion statement in Python. It checks if a condition is true or false. If it is true, it does nothing. Else, it throws an error. The syntax of assert is:

assert condition, msg

The assert statement will throw an AssertionError if the condition is false. Otherwise, it will not halt the program.

Optionally, we can also pass a message, msg to show on error. If the condition is false, it will show this message with AssertionError.

Example 1: Assertion example with division function:

Let’s take an example of assert. We will create one function that will divide one number by another. It will take the dividend and divisor as parameters. If the divisor is 0, it will throw AssertionError:

def divide(dividend, divisor):
    assert divisor != 0
    return dividend/divisor


dividend = int(input('Enter the dividend: '))
divisor = int(input('Enter the divisor: '))

print(f'{dividend}/{divisor} = {divide(dividend, divisor)}')

In this example,

  • The divide method is used to find the division.
  • It uses assert to check the value of the divisor. If it is not equal to 0, it will not throw any error. But if it is equal to 0, it will throw an AssertionError.

If you run this program, it will print output as below:

$ python3 example.py
Enter the dividend: 12
Enter the divisor: 3
12/3 = 4.0

$ python3 example.py
Enter the dividend: 12
Enter the divisor: 0
Traceback (most recent call last):
  File "example.py", line 9, in <module>
    print(f'{dividend}/{divisor} = {divide(dividend, divisor)}')
  File "example.py", line 2, in divide
    assert divisor != 0
AssertionError

As you can see here, it will throw an AssertionError only if the divisor is 0.

Example 2: Assertion example with division function and error message:

Let’s try the assert statement with a custom error message. As I explained above, this is an optional value. Let me change the above program to show an error message with the AssertionError:

def divide(dividend, divisor):
    assert divisor != 0, "Divisor shouldn't be 0"
    return dividend/divisor


dividend = int(input('Enter the dividend: '))
divisor = int(input('Enter the divisor: '))

print(f'{dividend}/{divisor} = {divide(dividend, divisor)}')

Here, we added the message "Divisor shouldn’t be 0”` with the assert condition. It will throw errors with this message:

Enter the dividend: 12
Enter the divisor: 0
Traceback (most recent call last):
  File "example.py", line 9, in <module>
    print(f'{dividend}/{divisor} = {divide(dividend, divisor)}')
  File "example.py", line 2, in divide
    assert divisor != 0, "Divisor shouldn't be 0"
AssertionError: Divisor shouldn't be 0

Example 3: Assertion with try-except:

The AssertionError is a built-in exception. So, we can handle it with try-except conditions as below:

def divide(dividend, divisor):
    assert divisor != 0, "Divisor shouldn't be 0"
    return dividend/divisor


dividend = int(input('Enter the dividend: '))
divisor = int(input('Enter the divisor: '))

try:
    print(f'{dividend}/{divisor} = {divide(dividend, divisor)}')
except AssertionError as error:
    print(error)

In this example, we are using try-except with the print statement that calls the divide method. It will print the error message.

Python assert example

You might also like: