How do I write code of more than 1 line in the Python interpreter?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 How Do I Write Code Of More Than 1 Line In The Python Interpreter?
00:30 Accepted Answer Score 37
02:38 Answer 2 Score 2
03:05 Answer 3 Score 12
03:27 Answer 4 Score 2
04:30 Thank you
--
Full question
https://superuser.com/questions/381719/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#terminal #python
#avk47
ACCEPTED ANSWER
Score 37
Add a trailing backslash (\
)
The trick is – similar to what you would do in bash
, for example – to add a trailing backslash. For example, if I want to print a 1
:
charon:~ werner$ python
>>> print 1
1
>>> print \
... 1
1
>>>
If you write a \
, Python will prompt you with ...
(continuation lines) to enter code in the next line, so to say.
Side note: This is what automatically happens when you create a function or class definition, i.e. the times when you really need a new line, so there's never a really good use for that, or at least none that I know of. In other words, Python is smart enough to be aware that you need continuation lines when you are entering a new function definition or other similar constructs (e.g. if:
). In these automatic cases, do note that you need to enter an empty line using \
to tell Python that you are done.
For everything else, you need to write one line after another. The way an interpreter works is that it, well, interprets every line that you feed it. Not more, not less. It will only "act" when it sees a newline, therefore telling the interpreter to execute what you gave it. The single backslash will prevent the interpreter from ever receiving a newline character (i.e. it won't know that you actually pressed Enter), but it will eventually receive one.
Python's interpreter has advanced capabilities when you use GNU readline, such as Emacs or vi-style keybindings to navigate within a line (e.g. Ctrl-A). Those however work only in the one current line. History is there as well, just try and press ↑.
What if I want to run complicated lines over and over?
You probably want to use proper source files if you want to execute more than one line of code at a time.
Or, use Jupyter notebooks, which offer a great, interactive way to create Python code with a built-in interpreter. You can write code as you would in a source code editor, but you can choose which lines are interpreted together. You can then run only parts of the code selectively. The best way is to just try and see if that fits your workflow.
ANSWER 2
Score 12
How about using ;\
? The semicolon signals the end of a command and the backslash signals that we are continuing on the next line. For example, type python
at command line to get into Python interpreter, then
>>> x=0 ;\
... print(x) ;\
... x=4 ;\
... print(x)
should give an output of
0
4
ANSWER 3
Score 2
Simply put, if you want to learn and want to run more than one line you write it into a .py file.
The trailing backslash method is good when you quickly want to run a series of commands, but it doesn't help when you are learning.
You will be able to develop code better, edit individual commands without worrying about spelling mistakes, and reuse code snippets you find useful if you write them into a small file.
ANSWER 4
Score 2
I just typed the following at my shell prompt, and it worked just fine:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> my_age = 35
>>> my_eyes = 'Blue'
>>> print "my age is %d and my eye color is %s" % (my_age, my_eyes)
my age is 35 and my eye color is Blue
>>>
The way to type more than one line of code in the interactive Python interpreter is, well, to type more than one line of code in the interactive Python interpreter. I'd think that would be good enough for your purposes.
It's true that you'll get a new prompt after each line, which means that if two of your lines of code produce output, that output will be separated by prompts. I guess that's what you're concerned about, though the example in your question doesn't suggested that:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "first line"
first line
>>> print "second line"
second line
>>>
If that's a problem, you can enclose your multiple statements in a (properly indented!) if
statement:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1:
... print "first line"
... print "second line"
...
first line
second line
>>>
(I'd suggest, even though it doesn't answer your question, that if you're writing code that's complex enough for this to matter, you should be writing scripts. Perhaps you've started doing so in the year and a half since you posted the question.)