How do I write code of more than 1 line in the Python interpreter?
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.
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
I was just going through the answer which you have got.I kept on experimenting by putting different symbols.I finally got the correct syntax to write it.try the following
print("more string") ; print(3)
this will give you a result
more string
3
with no any error
i have just used ';' to make it write in another line
i hope my answer may help you
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.