44

I have a problem coding Python in terminal. I'm just learning basics so I have no need to create .py files.

In terminal I can run one line of code in the Python interpreter, but how do I write more than one line?

Obviously if I hit enter, it enters the command and doesn't go down a line.

I just want to test following in terminal:

my_age = 35
my_eyes = 'Blue'
print "my age is %d and my eye color is %s" % (my_age, my_eyes)
2
  • 1
    I suspect you are having problems with 'indentation'. A. type 'python'. B. >>> comes up. C. To run a script the script needs to be properly indented with 'tabs'.
    – r4.
    Jan 24, 2012 at 9:13
  • 3
    I didn't ask if my way if learning is correct or not... I asked a specific question, if you know the answer please... if not...
    – user98645
    Jan 24, 2012 at 9:56

9 Answers 9

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.

6
  • 1
    :) nice works for the first line, but what if I want more lines? second trailing backslash gives syntax error.
    – user98645
    Jan 24, 2012 at 9:43
  • So what exactly is not working? Just select and copy those lines, then paste them in the terminal. They will execute from top to bottom just like in a script, since the newline is parsed by the interpreter. What is the issue?
    – slhck
    Jan 24, 2012 at 9:54
  • 1
    I'd like to type these directly in terminal, rather than write it in editor and paste it there... seems like waste of time :)
    – user98645
    Jan 24, 2012 at 9:58
  • Then you have to just type one line after another. There's no other way. It's an interpreter. It will prompt you for continuation lines only when you really need them.
    – slhck
    Jan 24, 2012 at 10:02
  • 1
    @mgae2m Sure: i.stack.imgur.com/Dd495.png
    – slhck
    Feb 13, 2021 at 19:02
11

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
5

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

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.

2
  • 1
    Yes, I agree. But I'm on a basic level. I don't write anything useful, just stupid variable testing and math etc. Nothing worthy of even creating a file. Thanks for the advice though.
    – user98645
    Jan 24, 2012 at 9:56
  • Even simple math is worth putting in a file - if it's 3 or more lines I would do it in vi myself. Even just so it's there if I wonder what I did later.
    – Rory Alsop
    Jan 24, 2012 at 10:06
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.)

1

I had the same beginner issue and the solution is simple. When you get the next line (...) hit ENTER again with nothing on the line and it will execute your code, whatever you have entered

1
  • This was my exact issue and is the simplest answer
    – DBADon
    Jan 1, 2022 at 18:11
0

Just leave the shell and open a new editor file. I.e. go to file and open new file. Write as many lines of codes as you want.

0

go to file

open new file

THen New file will open

enter the commands

save it

THen press "run" in the main menu

enter image description here

New contributor
Francis Philomenraj is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 13 at 7:49
-2

At the three dots make sure to hit "tab" key before you enter next command. You can continue writing as many commands that way. So when you hit enter after writing your second line of code, interpreter lets you enter third line of code... like in Fibonacci example below (from the tutorial):

a,b=0,1 while b<10: ... print(b) # If you enter print command immediately after the dots and hit enter key at the end, u will get an indentation error. Instead hit the tab after the three dots, then write your code and hit enter key, it will let u enter third line of code and so on. Look at below example (from tutorial):

a,b=0,1 # Followed by enter key while b<10: # Followed by enter key ... print(b) # Followed by enter key. note the tab after the three dots ... a,b=b,a+b # Followed by enter key ...enter key # No more commands to enter

you will see the result of the above prog

You must log in to answer this question.