Definition and Usage
The assert
keyword is used when debugging code.
The assert
keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.
You can write a message to be written if the code returns False, check the example below (w3school.com).
x = "hello" #if condition returns False, AssertionError is raised: assert x == "goodbye", "x should be 'hello'"
Inspired by Raymond Hettinger pop quiz
#Python grammar pop quiz: Will this assertion succeed or fail? Why?
t = 10 + 1, 20 + 2
assert t == 11, 22— Raymond Hettinger (@raymondh) October 19, 2020
I checked out and made my guess:
>>> assert t == (1, 23), "\n========\nError: Not same tuple\n=========" Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError: ======== Error: Not same tuple =========
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame