Entering multiple lines of code to be run later at the Python REPL -
this question has answer here:
- python, writing multi line code in idle 4 answers
i picked basic book today on programming. coding language python , have been trying out few hours i'm stuck, because can't figure out how write multiple lines of code. example, when write print("one") , hit enter, runs , prints word, one. how can have print word, one, , word, two, on line below it? also, when hit tab moves on 4 spaces, or so. can't figure out how have not run first command, , give me '>>>' on next line. guess i'm asking is: keystrokes need use like:
>>> print("one") >>> print("two")
thanks much!
(sorry such basic question, i'm totally confused on one.)
the python repl automatically executes each command typed in. why called "read-eval-print loop". accepts 1 input, evaluates it, , prints result.
if want execute 2 complete commands @ once, can put semicolon between them, this:
print("one"); print("two")
i said "completely typed in" above, because commands inherently require multiple lines, python must accept several lines of input before command "completely typed in". 3 types of command work this: flow-control commands (def
, while
, if
, for
, etc., apply several indented lines below them), multiline expressions (calculations inside parentheses or brackets), or statements use backslash (\
) @ end of line indicate continued on next line. if type in of blocks below, python wait until block finished before evaluating it.
if 1 + 1 == 2: print "true" else: print "false" print( 1 + 1 ) print \ 1 + 1
you combine these 2 strategies , type this:
print("one"); \ print("two")
python wait both commands typed , run them both @ once. i've never seen write code way.
alternatively, type several commands in different text editor, , paste them python repl, e.g., copy , paste following repl (but results printed between commands):
print("one") print("two")
alternatively, can behavior expecting using different interface python. ipython notebook choice, or try spyder or pycharm editors, let select few lines of code , run them.
or, if have longer script want run @ once, best option type in text file (e.g., script.py), , tell python run it, e.g., type python script.py
system command prompt (not python interpreter), or press f5 in idle editor.
Comments
Post a Comment