
Debugging in Python | CBSE Class 11 | Computer Science
Dear Class 11th STUDENTS, ! Welcome to this tutorial of Debugging Errors in Python from your CBSE class 11 of Computer Science Syllabus .
In this tutorial, we shall be learning our chapter-6: Debugging Errors in Python programming Language from Unit 2: Programming and Computational Thinking (PCT-1) as CBSE BOARD suggested to learn about computer system and its organisation to complete this section.
Unit 2: Programming and Computational Thinking (PCT-1)
Chapter 6: Debugging in Python programming Language:
I advice you to check the latest syllabus given by CBSE Board at its Official website: www.cbseacademic.nic.in
Also, in this tutorial we will covers all necessary topics/concepts required to complete your exams preparations in CBSE classes 11th.
NOTE:
- We are also giving some important Questions & Answers for better understanding as well as preparation for your examinations.
- You may also download PDF file of this tutorial from our SHOP for free.
- For your ease and more understanding, we are also giving the video explanation class of each and every topic individually, so that you may clear your topics and get success in your examinations.
Debugging refers to the process of locating the place of error, cause of error, and correcting
the code accordingly.
Introduction to Debugging in Python
Debugging in simple English means to remove ‘bugs’ from a program. An error causing disruption in program’s running or in producing right output, is a ‘program bug’. Debugging involves rectifying the code so that the reason behind the bug gets resolved and thus bug is also removed.
In this section, we shall talk about general debugging techniques and how you can debug code in Python.
Before we talk about debugging techniques, it is important for you to know the errors, error types, what causes them etc. So, let us first talk about errors and exceptions.
READ FROM HERE Errors in python
- Syntax errors
- Logical errors
- Run-time errors
Debugging Methods and Tools:
Debugging in Python involves identifying and resolving errors or unexpected behavior in your code. Several methods and tools are available for this purpose. Some of important are listed below.
- Print Statements:
- Python Debugger (pdb):
- Integrated Development Environment (IDE) Debuggers:
- Breakpoints:
- Step-through execution:
- Variable inspection:
- Call Stack:
- Tracebacks:
- Logging:
- Unit Testing:
Now, Let’s discuss all above methods one by one
Print Statements:
The simplest method involves strategically placing print() statements throughout your code to output the values of variables and track the program’s flow. This helps in understanding where the code deviates from expectations.
Python Debugger (pdb):
Python’s built-in pdb module provides an interactive debugger. You can insert import pdb; pdb.set_trace() (or simply breakpoint() in Python 3.7+) at specific points in your code to pause execution and enter the debugger’s command-line interface. Within pdb, you can inspect variables, step through code line by line, and evaluate expressions.
import pdb
def calculate_sum(a, b):
result = a + b
pdb.set_trace() # Execution will pause here
return result
sum_val = calculate_sum(5, 3)
print(sum_val)
Integrated Development Environment (IDE) Debuggers:
Modern IDEs like Visual Studio Code, PyCharm, and others offer robust debugging features. These typically include:
Breakpoints:
Setting breakpoints allows you to pause execution at specific lines of code.
Step-through execution:
You can step over, step into, or step out of functions to control the flow of execution during debugging.
Variable inspection:
IDE debuggers provide a visual interface to inspect the values of variables at different points in time.
Call Stack:
View the sequence of function calls that led to the current point of execution.
Tracebacks:
When a runtime error occurs, Python generates a traceback, which is a detailed report of the error’s location and the sequence of function calls that led to it. Understanding and interpreting tracebacks is crucial for quickly pinpointing the source of errors.
Logging:
For more complex applications, using the logging module allows you to record events and variable states throughout the program’s execution, which can be invaluable for diagnosing issues, especially in production environments.
Unit Testing:
Writing unit tests for your code helps in catching errors early in the development cycle and ensures that individual components of your program function as expected. Running tests regularly can prevent regressions and provide confidence in code changes.
Debugging using Code Tracing
Most common technique to debug an error is to find the point of error and origin of error. This is often done by printing the values of every intermediate result and of all values. For this purpose, code tracing is one most commonly used technique..
Common useful technique for debugging is code tracing. Code tracing means executing code one line at a time and watching its impact on variables. One way of code tracing is using Dry run, which you have already learnt in an earlier chapter. Code tracing can also be done by debugging tools or debugg available in software form.