Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Exploring Common Assertion Formats

00:00 Exploring Common Assertion Formats. When it comes to writing the assert statement, you’ll find several assertion formats that are common in Python code.

00:09 Being aware of these formats will allow you to write better assertions. The examples that you’ll see showcase a few of these common formats, starting with assertions that compare objects.

00:25 Comparison assertions are intended to test conditions that compare two or more objects using comparison operators. These assertions can also include compound expressions based on Boolean operators.

00:48 Another common assertion format is related to membership tests.

00:58 Membership assertions allow you to check if a given item is present in a specific collection, such as a list, tuple, set, or dictionary. These assertions use the membership operators, in and not in, to perform the required check.

01:16 Another assertion format is related to an object’s identity.

01:28 Identity assertions provide a way to test for an object’s identity. In this case, the assertion expression uses the identity operators, is and is not.

01:44 Finally, you’ll see how to check the data type of objects in the context of an assertion. Type check assertions commonly involve using the built-in isinstance() function to make sure that a given object is an instance of a certain class or classes.

02:07 Even though these are some of the most common assertion formats that you’ll find in Python code, there are many other possibilities. For example, you can use the built-in all() and any() functions to write assertions that check for the truth value of items in an iterable.

02:26 The all() assertions check if all the items in an input iterable are truthy, while the any() examples check if any item in the input iterable is truthy.

02:45 Your imagination is the only limit for writing useful assertions. You can write assertions using predicate or Boolean-valued functions, regular Python objects, comparison expressions, Boolean expressions, or general Python expressions.

03:01 Your assertion will depend on what specific condition you need to check at any given moment. Now you know some of the most common assertion formats you can use in your code.

03:12 It’s time to learn about specific use cases of assertions, and in the next section of the course, you’ll learn how to use assertions to document your code.

Become a Member to join the conversation.