Assertion in Python Programming

Image for post
Image for post

Hello dear readers! welcome back to another section of my tutorial on Python. In this tutorial post, we will be studying about Assertions in Python.

What is an Assertion?

An assertion is a sanity-check that you can turn on or turn off when you are done with your program testing.

The easiest way to think of an assertion is to link it to a raise-if statement (or to be more accurate, a raise-if-not statement). An expression is tested and if the result comes up false, then an exception is raised.

Assertions in Python are carried out by the assert statement, the newest keyword to Python, it was introduced in version 1.5

Python programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid output.

Image for post
Image for post

When an assert statement is being encountered, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, then an AssertionError exception is raised by Python.

Syntax

The syntax for assert statement is -

assert Expression[, Arguments]

If the assertion fails, Python uses ArgumentExpression as the code argument for the AssertionError. The AssertionError exceptions can be of course caught and handled like any other exception using the try-except statement, but if it is not handled, they will then terminate the program and then produce a traceback.

Example

Below is a function which converts a temperature from degrees Kelvin to degrees Fahrenheit. Since zero degrees Kelvin is as cold as it gets, then the function bails out if it sees negative temperature -

#!/usr/bin/python

def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32

print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)

Output

When the above code is executed, it will produce the following result -

32.0 451 Traceback (most recent call last): File "test.py", line 9, in <module> print KelvinToFahrenheit(-5) File "test.py", line 4, in KelvinToFahrenheit assert (Temperature >= 0),"Colder than absolute zero!" AssertionError: Colder than absolute zero!

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Object Oriented Programming in Python.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Originally published at https://www.webdesigntutorialz.com.

Written by

A tutorial blog that provides tutorials on Web Design, Programming, Java Technologies, Mobile App Development, Database, Machine Learning, etc.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store