[Tutor] getting started

Magnus Lycka magnus@thinkware.se
Thu Nov 14 18:16:25 2002


Hi John, Welcome to the world of programming. I
hope you will like it.

At 18:39 2002-11-14 +0000, john gennard wrote:
>How to get from ........x"    to    commence print  "yyyyy..........
>'Enter' key of course executes the command. I've re-read everything
>I have and can find no answer, and have tried also many keystrokes
>without result.

Statements ARE executed immediately in the interpreter.

 >>> print "xxx"
xxx
 >>> print "yyy"
yyy
 >>> print "zzz"
zzz
 >>>

It's supposed to be like that. You don't write programs
in the interactive mode, you use it to test things quickly,
or to make small calculations etc. (I don't use any other
calculator.)

If you want things to be executed all at once, put it in a
file. It's no big deal. Just select "File -> New Window" in
IDLE, and save as test.py or whatever. Type your code, press
Ctrl-X Ctrl-S to save (just Ctrl-S in Windows), and then
Ctrl-F5 to run it (just F5 in Windows).

If you are really desperate to run a few lines at once
in interactive mode, you can do this with a little trick:

 >>> if 1 == 1:
         print "aaa"
         print "bbb"
         print "ccc"


aaa
bbb
ccc

Block statements aren't executed until the block is
finished. In a file with python code, blocks end with
a dedent, i.e. when the next line is indented less,
or when the file ends (which ever comes first ;).

But in interactive mode there is no end of file in
that sense, so an empty line is interpreted as the
end of the "program", and what's been typed so far
will be executed.

(Actually you could type just "if 1:" instead of
"if 1==1:" but I don't know if you've reached a
point where that makes sense to you yet.)

Anyway, the immediate execution might not be as pretty
as you like, but apart from output being mixed with
your typed code and the fact that you can't put empty
lines wherever you want, things will work just the same
as in any python program. As soon as you define functions,
do loops or if statements etc, python will wait with
execution as long as it has to.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se