Python Assert Statement, How to Test a Condition
In Python, an assertion is a statement that confirms something about the state of your program. For example, if you write a createUser function and you are sure that the user needs to be older than 18, you assert that the age field is greater than or equal to 18. You can think of an assert statement like a unit test that is performed at runtime.
def createUser(user):
assert user.age >= 18
tl;dr
- Assertions are simply boolean expressions that raise exceptions if their condition is
False - The
assertstatement is the built-in syntax for assertions in Python - Developers often use
assertto do type checking, and input/output validation for function signatures - The
assertstatement is used for debugging purposes
Anatomy of an assert statement in Python
Python has a built-in assert statement, and its syntax is as follows.
assert condition [, error_message]
If the {condition} is false, an AssertionError is raised. If the optional second parameter, error_message was set, then that error message is used.
Catching an assertion error
You can catch an assertion error just like you would any other error in Python.
age = 17
try:
assert age >= 18, "too young!"
except Exception as e:
print(e)
# prints: "too young!"
Don’t use the assert statement in production
The assert statement is a fantastic tool for debugging code and writing tests. You should probably not use an assert statement in a production environment. You should be checking your code for unexpected behavior before you deploy it.

Don’t use parenthesis for the assert parameters
Do not use parentheses to call assert as if it were a normal function. Asser is a statement, not a function. When you call assert(condition,message), you execute the assert statement with a tuple (condition,message) as the condition, and no actual message.
Related Articles
Complete Guide to Removing Elements From Lists in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
While lists aren’t the most efficient data structure if you’ll be doing lots of deleting from the middle, there are definitely good ways to accomplish the task. The built-in remove() method should be your first option. Let’s go over some examples.
How to Use the Ternary Operator in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Developers love concise code that’s easy to read, and that’s exactly what ternary operators are for. The ternary operator in Python lets you perform a small if/else statement in a single line. Let’s take a look at a few examples.
Removing Duplicates From a List in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Let’s go over a few idiomatic ways to remove duplicates from lists in Python. Method #1 - Create a new list (simplest) This is the easiest algorithm to code, but because it requires creating a new list, also requires more memory and is a bit slower.
How to Check if a File Exists in Python
Dec 08, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
When working with files in Python, you’ll often need to check if a file exists before you do anything else with it, such as reading from or writing to it. Luckily, the Python standard library makes this a piece of cake.