what assert does in python code example
Example 1: python assert
"""Quick note!
This code snippet has been copied by Pseudo Balls.
This is the original answer.
Please consider justice by ignoring his answer.
"""
"""assert:
evaluates an expression and raises AssertionError
if expression returns False
"""
assert 1 == 1 # does not raise an error
assert False # raises AssertionError
# gives an error with a message as provided in the second argument
assert 1 + 1 == 3, "1 + 1 does not equal 3"
"""When line 7 is run:
AssertionError: 1 + 1 does not equal 3
"""
Example 2: assert python 3
# Simple asserting thing, run it by using pytest or something
# If you don't know how to run pytest, then go learn it.
def test_math():
assert(1 + 1 == 2)
# Another way to test it (without pytest) is:
# You could just run the function to see if it makes an error.
# If it doesn't, it means it was fine, if it does, it means there's an error.
# But then again, using pytest or something is much easier and saves time.
# So try to use testing applications instead of running the function to see.
Example 3: how to use assert in python
assert type(num) is int,"num must be an integer"