AppDividend
Latest Code Tutorials

Python Assert Statement Tutorial With Example

0

Python Assert Statement Tutorial With Example is today’s topic. Assertions are the statements that state the fact confidently in your program. An assertion is the sanity-check that you can turn on or turn off when you are done with your testing of the program. Let’s take a simple example of writing the division function; you’re confident that the divisor shouldn’t be zero, you assert divisor is not equal to zero. We will see this example in later in this post.

Assertions in Python are boolean expressions check if the conditions return true or not. 

If it is true, the program does nothing and continue to execute to the next line of code. 

However, if it’s false, then the program stops and throws an error.

Python Assert Statement Tutorial With Example

If you want to simulate or debug your code like what is happening in which stage, then you can use the python assert statements in your code.

It is the debugging tool as it brings a program on halt as soon as any error is occurred and shows on which point of the program that error has occurred.

Assertions are the conditions or boolean expressions which are always supposed to be true in the code.

The assert statement takes an expression and optional message.

An assert statement is used to check types, values of argument and the output of the function.

The assert statement is used as the debugging tool as it halts the program at the point where an error occurs.

The following syntax is the basic structure of python assert statements.

assert condition

If you wish to write a message when the condition is false, then its syntax is following.

assert condition, message

You can also send information like a message with the assert statement for a better understanding of the fault of the code. Let’s see the following example.

# app.py

def avg(ranks):
    assert len(ranks) != 0
    return round(sum(ranks)/len(ranks), 2)

ranks = [62, 65, 75]
print("Average of mark1:",avg(ranks))

In the above example, we do not want to pass the empty ranks, and if it is empty, then it will throw an Assertion Error. See the below output.

 

Python Assert Statement Tutorial

In the above example, we will get an average because we have passed the list of items.

Now, let’s pass the empty list and see the output.

# app.py

def avg(ranks):
    assert len(ranks) != 0
    return round(sum(ranks)/len(ranks), 2)

ranks = []
print("Average of mark1:",avg(ranks))

Now, in the above example, we have not passed the ranks items. So the length will be 0, and we will get an Assertion Error.

 

Python Assert Statement Tutorial With Example

But, in the above example, it does not throw an error with a proper message. You can write your custom error message as well. Let’s do that.

# app.py

def avg(ranks):
    assert len(ranks) != 0, 'Ranks list should not be empty'
    return round(sum(ranks)/len(ranks), 2)

ranks = []
print("Average of ranks:",avg(ranks))

In the above example, we have passed the second argument to the assert statement which is the message. So in the output, we will see the message as well.

 

Assert Statement In Python

More Examples on Python Assert Statement

Let’s see another example which is divide by 0.

# app.py

def divide(x, y):
    assert y != 0 , 'Divisor cannot be zero'
    return round(x/y, 2)

z = divide(21,3)
print(z)

a = divide(21,0)
print(a)

In the above example, if the divisor is 0 then assert will throw an error with a message. Let’s see the output.

 

More Examples on Python Assert Statement

In the third line of the above code, we can see the assert statement.

In this line, it is checked that whether the variable y value is equal to 0 or not. 

If it is greater than zero, i.e., the condition is true, then no problem occurs, and we will get the output accordingly.

But when we called the method division() with the 2nd argument 0, then the assert condition is false. 

That is why an AssertionError occurs, and it throws an error with a message ‘Divisor cannot be zero,’ that we wrote in the message part of the python assert statement.

Finally, Python Assert Statement Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.