ipython: Multiple commands on the same line and newlines

Phil Winder philipwinder at gmail.com
Sat Apr 16 12:37:32 EDT 2011


On Apr 16, 5:29 pm, Andrea Crotti <andrea.crott... at gmail.com> wrote:
> Phil Winder <philipwin... at gmail.com> writes:
> > Hi,
> > I'm having a go at using ipython as a command prompt for data
> > analysis. Coming from Matlab, I'm used to typing multiple commands on
> > the same line then using the up arrow to go through my history.
> > How can I write multiple python commands on the same line?
> > E.g. "x = 0; while x < 10: x = x + 1;" returns an "invalid syntax"
> > error on the 'e' in while.
>
> > Also, how can I produce a new line, without it running the command? I
> > would have expected a ctrl-enter or shift-enter to produce the
> > expected results.
> > E.g. I want:
> > "x = 0; <ctrl-enter>
> > while x < 10: <ctrl-enter>
> >     x = x + 1; <ctrl-enter>
> > " <enter to run>
> > It seems to work automatically for the "while xxx:", but combinations
> > of keys+enter do not work for "normal" lines.
>
> > Cheers,
> > Phil
>
> Well when you do something like
>
> while x < 10:
>
> it doesn't execute anything, but goes to newline and waits for the rest.
>
> for
> x = 10
>
> what's the difference for you if it gets evaluated before or after?
> Anyway you can you also %cpaste if you want to write more code
>
> Anyway to me this works perfectly:
> In [1]: x = 0
>
> In [2]: while x < 10: print x; x += 1
>    ...:
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9

Yes, that does not produce an error, but it does not "work". Please
refer to my first post. Try the first code, you will get a syntax
error. Placing things on one line makes for easy history scrollback.
In your version you will have 2 lines of history for the x = 0 term
and the while ... term. I don't want to have to press up twice,
especially when the code was in the distant past! Also cpaste might be
ok for scripting, but it looks too clumsy to use at the command line.

Cheers,
Phil



More information about the Python-list mailing list