...

/

Preventing Bugs

Preventing Bugs

Learn how we can prevent different types of bugs during programming in Python.

How can we prevent bugs in a Python program? It seems there’s no certain way to do that. So we decided to make a list of common programming mistakes.

Bug 1: Mixing tabs with spaces in indentation

Consider the code snippet shown below:

Press + to interact
a = 0
b = 5
if a < b :
a = 10
b = 20

In the code above, the first statement in the if block has been indented using the “Tab” key, whereas the second has been indented using spaces. So on the screen, the snippet looks alright, but the Python interpreter will flag an error. These kinds of errors are difficult to spot, so always use four spaces for indentation.

Bug 2: Missing : in an if, loop, function, or class

Since other languages do not need a :, those who migrate to Python from other languages tend to forget to use :. ...