Debugging Python
Debugging is one of the best skills you can learn, as it will help you in many difficult situations.
Every language has its debugger. Python has pdb, available through the standard library.
You debug by adding one breakpoint into your code:
breakpoint()
You can add more breakpoints if needed.
When the Python interpreter hits a breakpoint in your code, it will stop, and it will tell you what is the next instruction it will run.
Then and you can do a few things.
You can type the name of any variable to inspect its value.
You can press n to step to the next line in the current function. If the code calls functions, the debugger does not get into them, and consider them “black boxes”.
You can press s to step to the next line in the current function. If the next line is a function, the debugger goes into that, and you can then run one instruction of that function at a time.
You can press c to continue the execution of the program normally, without the need to do it step-by-step.
You can press q to stop the execution of the program.
Debugging is useful to evaluate the result of an instruction, and it’s especially good to know how to use it when you have complex iterations or algorithms that you want to fix.
download all my books for free
- javascript handbook
 - typescript handbook
 - css handbook
 - node.js handbook
 - astro handbook
 - html handbook
 - next.js pages router handbook
 - alpine.js handbook
 - htmx handbook
 - react handbook
 - sql handbook
 - git cheat sheet
 - laravel handbook
 - express handbook
 - swift handbook
 - go handbook
 - php handbook
 - python handbook
 - cli handbook
 - c handbook
 
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.