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.

Using Python's assert to Debug and Test Your Code (Summary)

Now you know how to use Python’s assert statement to set sanity checks throughout your code and make sure that certain conditions are and remain true. When any of these conditions fail, you have a clear indication of what’s happening. This way, you can quickly debug and fix your code.

The assert statement is pretty handy when you need to document, debug, and test your code during the development stages. In this video course, you learned how to use assertions in your code and how they can make your debugging and testing process more efficient and straightforward.

In this video course, you learned:

  • What assertions are and when to use them
  • How Python’s assert statement works
  • How assert is handy for documenting, debugging, and testing code
  • How assertions can be disabled to improve performance in production
  • What common pitfalls you can face when using assert statements

With this knowledge on the assert statement, you can now write robust, reliable, and less buggy code, which can take you to the next level as a developer.

To fill out your debugging and testing tool kit, check out these courses:

Download

Sample Code (ZIP)

3.1 KB
Download

Course Slides (PDF)

16.3 MB

gunnarpruss on April 28, 2023

Hi Darren, thanks a lot for the course! It’s not related to Python assertions, but I was wondering how you achieved such smooth typing in the screencast?

Many thanks and with best regards, Gunnar

Darren Jones RP Team on May 2, 2023

@Gunnar - No surprise, it’s some python code! It started out as a simple script that just read in a text file and typed it using the pynput library to simulate keypresses. Over time I’ve added extra features - delays, cursor and other keypresses to allow simulation of anything someone can type, as well as repeats and other features, such as different behaviour for different programs. It’s rather messy (as I’ve just hacked it together over time), so it’s not something I’d release, but it’s a worthwhile project if you’re looking to create something similar.

Py-Max on Aug. 19, 2023

This was a great and thorough course. A question though:

If using assertions for dev data validation, wouldn’t production code ultimately have many assertions in addition to proper conditionals and wouldn’t this affect readability? A few could be ok but if the code has more than a handful, I’d get concerned as readability would suffer with assertions stacked on conditionals.

Become a Member to join the conversation.