I am new to python. I use R. I'm using PyScripter for python. I can run a whole script, but often I just want to run one line or a small block. In R, I can just copy and paste directly into the session. Is there a way to do this in python? If I just copy and paste, then it doesn't recognize them as commands.

Python has an interactive console (similar to R's) which you can invoke py just typing "python" in your terminal (with no arguments). It's pretty basic compared to R's (you can get a more powerful console by installing ipython). You can paste the code into the interactive console to run individual lines (provided the right context exists, e.g. you've imported the required modules, etc). You may also look into ipython notebooks (installed with ipython).

Thanks. That works. It's quite basic. What do most people do if they just want to run selected lines of code?

If you just want to run these two lines, for example:

string2 = "b"print string2

If you paste them into the standard IDLE editor, nothing happens because it doesn't see the second statement. I agree that it works in the basic python editor that comes with python, but surely this is something that people do ? Python users must at times only run certain lines of code ?

ppaarrkk wrote:If you paste them into the standard IDLE editor, nothing happens because it doesn't see the second statement. I agree that it works in the basic python editor that comes with python, but surely this is something that people do ? Python users must at times only run certain lines of code ?

Python typically isn't used the same way as R -- people don't spend a lot of time in the interactive interpreter (usually it's just to try things out). If you are looking for a more advanced interactive experience, look into ipython and ipython notebooks.

ppaarrkk wrote:What do most people do if they just want to run selected lines of code?

Depends on your Python editor of choice. Most have an interactive shell. Then just type the line there, hit Enter, see it process immediately. If it gets more complicated, you may capsule it in a function which you can call. Or you indeed go into the editor and do something like "run single line" or "run until here" or what you need. I use the interactive shell a lot when I am prototyping and debugging my code.