How To Debug a Bug in Python Code

Python is a programming language. Debugging Python does not mean getting a bug out from the stomach of a python. As we have mentioned at starting that Python is high-level computer language used for programming, debugging means finding out the bug that has been creating some problems in the whole process and rectifying that error. In a line bug means a coding error in Python programming, that’s all. There is not a tough shell to crack.

What is a bug in Programming?

In computing, a bug means some serious technical glitches that are creating a lot of issues in real life applications of the software or process you have made. These bugs create abnormal behaviors in the software while in operation and sometimes these shows up many errors which may cause severe issues. That’s why debugging is required to fix these issues.

Debugging- What it is

Well, till now we were talking about bugs, let’s come to know little about debugging. It’s very tough to write a clean bug-free program from scratch. Having bugs in software and apps is normal and our ultimate goal is to locate the bugs, analyze the errors and rectify it. The prime goal of debugging is nothing but help the software or app function as we want.

Python handles errors with exceptions.

There is statement try which is used for handling exceptions. It has two different forms; these are: try/except and try/finally. At final case, the try clause which we have mentioned already can be followed by another one except or even more than one sometimes. But all of these can be followed by a finally single clause in the later cases.

Try/except

Before getting into the deeper section, we need to get a superficial idea of what the try/except clause is. The try/except syntax is given below:

1

2

3

4

try:

# try block code

except:

# except block code

The main body of the try clause includes codes that have all ability to form an exception, provided that if an exception had generated already, all the coded statements in the blocks are skipped at once.

If we look at the other part, the main body of the except clause is called the “exception handler”, because it is used to catch all the exceptions coded inside the program. The except codes can be executed only if an “exception” has been generated by the program itself. Otherwise the whole block of codes will be skipped at a time. For that, you can use some built-in exceptions given in the Python Standard Library.

To make everything clear like daylight, we take an example here. If we are asked to ENTER a denominator in a proper division formula (Dividing by zero is not allowed as per standard division method), let’s try to write a  try/except coded of statement which automatically checks whether there is a division by zero or not. Print the message if it occurs.

1

2

3

4

5

6

denominator = input(‘Enter a denominator value: ‘)

try:

formula = 15/denominator

print ‘The result is ‘ + str(formula)

except ZeroDivisionError:

print ‘You attempted to divide by zero which is not allowed’

try/finally

There is another way to write the try statement in Python and that is try/finally.

Here we need to remember one thing, that is- finally clauses are called clean up or termination clauses in Python.

Here in below we have written an example with finally clause

1

2

3

4

5

6

denominator = input(‘Enter a denominator value: ‘)

try:

formula = 15/denominator

print ‘The result is ‘ + str(formula)

finally:

print ‘You attempted to divide by zero which is not allowed’

The Raise Keywords

This is another major way to handle exceptions on Python programming. If in case you get the exceptions you wanted- that is the exceptions which are raised while the scope of expected error in programming occurs.

Look at the example of  raise keyword to get the idea of a clearer string-

1

2

3

4

5

6

7

8

try:

x = input(‘Enter a number in the range 1-10: ‘)

if x<1 or x>10:

raise Exception

print ‘Great! You listened to me and entered a valid number’

except:

print ‘Your number seems to be outside the range 1-10’

These are some regular debugging methods of Python programming. Share your opinions with us and of course do share your experiences of Python debugging.  Pour your suggestions in comment sections for more improved ideas.