JOIN OUR FORUM

What are assertions...
 
Notifications
Clear all

What are assertions in Python?


Bhavana Nande
Member Admin
Joined: 2 months ago
Posts: 6
Topic starter  

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

The easiest way to think of an assertion is to liken 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, an exception is raised.

assert Expression[, Arguments]
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)
When the above code is executed, it 

Quote
Share:
Shopping Cart