1"""Quick note!
2This code snippet has been copied by Pseudo Balls.
3This is the original answer.
4Please consider justice by ignoring his answer.
5"""
6"""assert:
7evaluates an expression and raises AssertionError
8if expression returns False
9"""
10assert 1 == 1 # does not raise an error
11assert False # raises AssertionError
12# gives an error with a message as provided in the second argument
13assert 1 + 1 == 3, "1 + 1 does not equal 3"
14"""When line 7 is run:
15AssertionError: 1 + 1 does not equal 3
16"""
1x = "hello"
2
3#if condition returns False, AssertionError is raised:
4assert x == "goodbye", "x should be 'hello'"
5-----------------------------------------------------------------
6Traceback (most recent call last):
7 File "demo_ref_keyword_assert2.py", line 4, in <module>
8 assert x == "goodbye", "x should be 'hello'"
9AssertionError: x should be 'hello'
1name = 'quaid'
2# check if name assigned is what assert expects else raise exception
3assert(name == 'sam'), f'name is {name}, it should be sam'
4
5print("Hello {check_name}".format(check_name = name))
6#output: Assertion Error
7# quaid is not what assert expects rather it expects sam as a string assigned to name variable
8# No print out is received
1The assert keyword is used when debugging code.
2
3The assert keyword lets you test if a condition in your code returns
4True, if not, the program will raise an AssertionError.
5
6You can write a message to be written if the code returns False, check
7the example below.
8
9x = "hello"
10
11#if condition returns False, AssertionError is raised:
12assert x == "goodbye", "x should be 'hello'"