A is for assert - Python A to Z 0 ▲ Juha-Matti Santala 2 hours ago · Tech · hide · 0 comments Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. The assert statement is a built-in Python statement that gives developers a nice short hand for raising an AssertionError based on a condition. assert expression, "message" An assert statement is syntactic shorthand for if __debug__: if not expression: raise AssertionError("message") To see how it works in practice, let’s jump into a REPL session: >>> age = 21 >>> assert age > 18, f"Needs to be over 18 years old, {age=}" >>> age = 16 >>> assert age > 18, f"Needs to be over 18 years old, {age=}" Traceback (most recent call last): File "<python-input-5>", line 1, in <module> assert age > 18, f"Needs to be over 18 years old, {age=}" ^^^^^^^^ AssertionError: Needs to be over 18 years old, age=16 When the… No comments yet. Log in to reply on the Fediverse. Comments will appear here.