Skip to content

Code Debugging in Python




Python – Code Debugging


As we read earlier about Python Typecasting how to cast one data type to another data type. Now we will study about Python Code Debugging and we will use PyCharm’s Python pdb. We will cover how to do debugging code in Python, how to use Python breakpoint, breakpoint codes, watch window etc. and if you are stuck somewhere while coding and you don’t understand the error then you can fix your error by using Python debug mode. Lets start.




What is Debugging in Python 3.8 ?

We know that debugging mode plays an important role when we want to find a error or a bug in a particular line of code. Here, Python programming comes with the latest built-in function i.e. Breakpoint in Python 3.8 version.

Debugging mode finds the error or bug in the code line by line where we add the Breakpoint (red circle mark) , if a bug is found then the program stops temporarily then you can fix/remove the error and start to execute the code again.

Python-Code-Debugging



How to debug python code ?

  • Break Point : – First you put the break point in the left side of the coding.

     

    Python-Code-debugging
  • Step Running : Start debugging your code.

     

    ⇒ Press F7 ( Step into )

    ⇒ Press F8 ( Step over )

     

  • Watch window and Variable window :  Now you see your code getting debugged in your watch window  or variable window and find the error.
     
     

    Python-Code-Debuging




Steps :

  • Break point is also known as check point that is used to Break over code at given line number from running mode to debug code.

  •  After that we can start step running using F7 and F8 shortcut key.

  • Then watch window is used to check values of variables at run time in debug mode.



How to check the size of python object ?



General syntax :

_sizeof_( )



Example :

x = 100, 200, 300
c = x.__sizeof__()
print(c)



Output :

48




How to read Address in Python ?



General Syntax :

id ( expression )



Example :

x = 100, 200, 300
c = x.__sizeof__()
print(id(c))



Output :

140712505154688




How to find hex( ) value in python ?



General syntax for hex( ) :

hex ( expression )



Example :

x = 100, 200, 300
c = x.__sizeof__()
print(hex(id(c)))



Output :

0x7ffa1fa29c80
>