Debug Python code in PyCharm with conditional breakpoint

One reason why I’m so big fan of PyCharm (or any IDE from JetBrains) is because of its smart debugger.

Consider a simple example:

import random

for i in range(1000):
x = random.randint(1,100)

To pause execution at some point (say, the last line), just click beside the line number:

And your breakpoint is set!

There is one problem though. Say, you want to trace the case only when x = 50. Now in this loop, 50 will appear roughly once in every 100 times. If you set a breakpoint there and keep stepping over, it will be so hectic.

One lazy way is putting an if block after line#4:

if x==50:
#Set breakpoint here

But debugging should not affect your code! In no time, your code will be unreadable with a bunch of blocks like this.

Here is the smart way. After setting the breakpoint, right-click the red dot. A dialog will appear. Write there:

x == 50

as shown in this screenshot:

It will pause only when x is 50! 😀

Jack of a few trades, master of none.