assertion in python || python assert examples || python assert multiple conditions

By | 4th March 2019

Assertion in python : 

Python has inbuilt assert statement to use assertion condition within the program.
assert statement contains a condition or expression that is meant to be continuously true.
If the condition is fake assert halts the program and provides associate degree AssertionError.

Assertions area unit statements that assert or state a reality with confidence in your program.
For example, whereas writing a division operate, you are assured the divisor should not be zero, you say divisor isn’t capable zero.

Assertions area unit merely mathematician expressions that checks if the conditions come true or not.
If it’s true, the program does nothing and move to the next line of code.
However, if it’s false, the program stops and throws an error.

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

 

Python Assert Statement

Python Assert Statement

The benefits of programming with assertions :

  • because they provide run-time checks of assumptions that you would have otherwise put in code comments
    the run-time execution environment knows nothing about comments
    comments usually get obsolete and out-of-sync with the code base
  • assert statements (that truly find yourself executing) cannot get obsolete, because if they do, then they will fail for legitimate cases, and you will be forced to update them
  • An assert statement merely checks a mathematician condition, and does nothing if it is true but immediately terminates the program if it is false
  • e.g., assert x >= zero for creating positive the input to a root operate is non-negative
  • e.g., assert isLegalSSN(joe.getSSN()) for making sure that Joe’s social security number (SSN) is legal due to some criteria defined by that function
    An assertion permits you to specific in code what you assume to forever be true regarding information at a specific purpose in execution
  • and terminates execution as shortly as your assumption is profaned
  • since there is usually no purpose in continued execution if your assumptions now not hold
  • You probably already write comments throughout your code to document non-obvious assumptions regarding the values of information structures,
  • so why do not you instead convert those comments into assert statements?
  • whenever potential, instead of (or in addition to) writing a comment, write an assert statement that expresses the same details in code
  • When you area unit writing code stuffed with assert statements, you can be more confident that when execution reaches a certain point, certain conditions are guaranteed to be met.

Python assert Statement :

Python has intrinsic assert statement to use assertion condition within the program.
assert statement encompasses a condition or expression that is meant to be forever true.
If the condition is false to assert halts the program and gives an AssertionError.

assert <condition>

assert <condition>,<error message>

Example 1: Using assert without Error Message

Script.py :

def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)

mark1 = []
print(“Average of mark1:”,avg(mark1))

 

Leave a Reply

Your e-mail address will not be published. Required fields are marked *