(Spyder maintainer here) There is a trick not many people know to introduce breakpoints wherever you want in your Python code: import pdb; pdb.set_trace().
In your case, you need to replace your code with
def foo():
names = ['A', 'B', 'C']
values = [11,12,13]
i = 0
import pdb; pdb.set_trace()
for n in names:
variable = str(n) + ' = ' + str(values[i])
print(variable)
i += 1
foo()
and then run your file with Run file/F5 or also as a cell with Ctrl + Enter. With this simple change you'll get a debugger session exactly at the line pdb.set_trace() is placed and enter the for cycle below it to iterate line by line through it.
Note: Since Python 3.7 this is even simpler because it adds a new builtin function called breakpoint to replace pdb.set_trace(), but with the exact same effect.
(Spyder maintainer here) There is a trick not many people know to introduce breakpoints wherever you want in your Python code: import pdb; pdb.set_trace().
In your case, you need to replace your code with
def foo():
names = ['A', 'B', 'C']
values = [11,12,13]
i = 0
import pdb; pdb.set_trace()
for n in names:
variable = str(n) + ' = ' + str(values[i])
print(variable)
i += 1
foo()
and then run your file with Run file/F5 or also as a cell with Ctrl + Enter. With this simple change you'll get a debugger session exactly at the line pdb.set_trace() is placed and enter the for cycle below it to iterate line by line through it.
Note: Since Python 3.7 this is even simpler because it adds a new builtin function called breakpoint to replace pdb.set_trace(), but with the exact same effect.
Visual Studio Community Edition
(with some Add-Ins not relevat to python):
Disclaimer: I use it because im familiar with it (C#) and it is free to use - not because it's especially pythonic.
Coding (Layout configureable):
Left: Code
Left-low: Output (normally I collaps that down to tabs only at the bottom to get more coding screen estate. Maybe downsized and switched to tab
Error Listwhich lists errors in your code (doh). Your code was perfect, so you do not see any of the sqigglies pointing out errors in the code.Right: Solution explorer - essentially what files are inside this special python solution.
Right-low: Properties of a selected file
All the windows can be removed, resized, un-pinned to become "free floating windows" or collapsed into vertical tabs at left/right side of the window to get more screen estate for whats important to you)

See the red dots on the side of the lines? You place them before running the code. They mark break points, code execution automatically stops running when hitting one (basic use) or you can configure them to
- not stop running but print out trace info of variables
- stop only conditionally on f.e. variable value becomes 5 (
loop indexafter that your program crashes all the time or other "conditions") - they can also only stop
on 4th time hitand some other things:

Debugging Code:
F5 to start code running changes the layout (see above, you can change it and changes persist for next run):

When stopped you can hover over variables that are already used and inspect them, you can pin those information to the side - if one changes the text of the pin will get red to visualize this.
You can also put stuff in the "watch window" or use "quick watch" to inspect it - window lower half of screenshot. From a break you can continue wiht F5 to run till next breakpoint, or use F10 or F11:
F10"steps over" statements line by lineF11"steps into" statements - i.e. if your statement is function call it would execute the function wihtF10and step into the function withF11
If inside a function you can use SHIFT+F11 complete the function and step out again and go from there.
I used F5 and F10 twice and I am currently at the line indicated by the arrow-thingy. You can also dragg the arrow some lines up to reexecute code, but this can have sideeffects - I rarly use it, most times I just restart the program Ctrl+Shift+F5 or stop debugging Shift+F5
The print output of your program is shown in a normal Console window thats cropped from the screenshots.
AddOn1: You can also simply execute all code, no debugging with CTRL+F5 and the IDE has 'a few' other features for python.
AddOn2: VS naturally comes with a lot of other features useful in an IDE, like integration to git etc. - if you use those they are "usable" from within the IDE via plugins -although some things are better done on the git.bash.
Videos
You could do this while in pdb to launch a temporary interactive Python session with all the local variables available:
(pdb) !import code; code.interact(local=vars())
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
When you're done, use Ctrl-D to return to the regular pdb prompt.
Just don't hit Ctrl-C, that will terminate the entire pdb session.
In python3 ipdb (and pdb) have a command called interact. It can be used to:
Start an interactive interpreter (using the code module) whose global namespace contains all the (global and local) names found in the current scope.
To use it, simply enter interact at the pdb prompt. Among other things, it's useful for applying code spanning multiple lines, and also for avoiding accidental triggering of other pdb commands.
You have 2 options:
- Write the command in a new editor window, then simply copy and paste the code in the debug console and press
Enter - Write the command directly in the debug console. When you want to enter a new line, press
Shift+Enter. When the command is complete, execute withEnter
Another option:
2.1. Write the command directly in the debug console. Type {} at the end of current line, then press shift+enter. After complete writing all lines, press enter to execute.
for i in [1,2,3]: {'''press shift+enter here'''} for i in [1,2,3]: { print(i) #press enter here }
Reference https://youtu.be/p8zMXKFlkqk?t=50
Breakpoint could have a condition. When the condition evaluates to True debugger stops otherwise skips it. See the documentation.
Perhaps you could do something like this:
for x in range(20):
if x == 17:
print 'hello'
... do stuff ...
Then in PyCharm, mark the print line as a breakpoint.