assert in python 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
assert False
assert 1 + 1 == 3, "1 + 1 does not equal 3"
"""When line 7 is run:
AssertionError: 1 + 1 does not equal 3
"""
Example 2: python assert statement
name = 'quaid'
assert(name == 'sam'), f'name is {name}, it should be sam'
print("Hello {check_name}".format(check_name = name))
Example 3: assert python
x = "hello"
assert x == "goodbye", "x should be 'hello'"
-----------------------------------------------------------------
Traceback (most recent call last):
File "demo_ref_keyword_assert2.py", line 4, in <module>
assert x == "goodbye", "x should be 'hello'"
AssertionError: x should be 'hello'
Example 4: assert syntax python
assert <condition>,<error message>
assert 1==2 , "Not True"
Example 5: assert python 3
def test_math():
assert(1 + 1 == 2)