Tuesday 28 January 2020

assert keyword



Assert keyword

18th Feb,2020.

Debugging:
Identifying and solving the errors (bugs) is called debugging.

Debugging:
Identifying the bug/issues (error) and solving (fixing) the issue/bug/errors is called Debugging.

Assert keyword:
def addition(a,b):
    return a-b
assert addition(4,2)==6,"Addition of two number=6"
assert addition(3,2)==5,"Addition of 3,2 is 5"
print("Addition of 4,2 numbers",addition(4,2))
print("Addition of 3,2 numbers",addition(3,2))
"""output:
assert addition(4,2)==6,"Addition of two number=6"
AssertionError: Addition of two number=6

Addition of 4,2 numbers 2
Addition of 3,2 numbers 1
"""
If I’m not using the assert in the above program, wrong output may be handed over to the client, because of the assert, it verifies the expected output other wise it will alert to the programmer.
def addition(a,b):
    return a+b
assert addition(4,2)==6,"Addition of two number=6"
assert addition(3,2)==5,"Addition of 3,2 is 5"
print("Addition of 4,2 numbers",addition(4,2))
print("Addition of 3,2 numbers",addition(3,2))
"""output:
Addition of 4,2 numbers 2
Addition of 3,2 numbers 1

Addition of 4,2 numbers 6
Addition of 3,2 numbers 5

Assert keyword is used to debugging (i.e resolving the error)
We will perform debugging in development environment or in the testing environment but not in the production environment.
Types of assert statements:
1.   Simple version
2.   Augmented version
Simple version:
Assert conditional expression



Augmented version:
Assert conditional expression, any message
Example: assert addition(4,2)==6,"Addition of two number=6"
Handling the AssertionError :

#example program on handling the assertion Error
def avg(marks):
    try:
        assert len(marks)!=0
        return sum(marks)/len(marks)
    except AssertionError:
        print("Marks are empty")
stud1=[]
print("Average of stud1",avg(stud1))
stud2=[60,88,70,90]
print("Average of stud1",avg(stud2))

len() is a predefined function in python, It is used to find the length of the given string,array,list,tuple, dictionary,..etc. The number of elements stored in the list, it displays.

What is the difference between Exception handling and assertions?
Assertion is used to alert or warning to the programmer to resolve the development bugs/issues. Where as exception handling is used to handle runtime errors.

Related Video: https://youtu.be/UORaySj-M2w
Next: Example program on class and object
Previous: else with try and nested exception blocks

1 comment:

  1. As stated by Stanford Medical, It's indeed the SINGLE reason women in this country get to live 10 years more and weigh an average of 19 KG less than us.

    (And by the way, it is not related to genetics or some secret diet and really, EVERYTHING about "HOW" they eat.)

    P.S, I said "HOW", not "what"...

    TAP this link to find out if this quick test can help you unlock your real weight loss potential

    ReplyDelete

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...