Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)
share|improve this question
1  
You ARE WRONG. Sorry. But. You really are. Do create .py files really. –  r4. Jan 24 '12 at 9:12
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 '12 at 9:13
1  
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... –  Sandro Dzneladze Jan 24 '12 at 9:56
add comment

4 Answers

up vote 6 down vote accepted

The trick is – similar to what you would do in bash, for example – to add a trailing backslash.

Example:

charon:~ werner$ python
>>> print 1
1
>>> print \
... 1
1
>>> 

If you write a \, it will prompt you with ... to enter code in the next line, so to say. This is however what automatically happens when you create a function or class definition, i.e. the tiems 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.


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 .

But, apart from that, you probably want to use proper source files if you want to execute more than one line of code at a time.

share|improve this answer
    
:) nice works for the first line, but what if I want more lines? second trailing backslash gives syntax error. –  Sandro Dzneladze Jan 24 '12 at 9:43
    
I did add, thx. –  Sandro Dzneladze Jan 24 '12 at 9:51
    
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 '12 at 9:54
    
I'd like to type these directly in terminal, rather than write it in editor and paste it there... seems like waste of time :) –  Sandro Dzneladze Jan 24 '12 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 '12 at 10:02
add comment

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.)

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
    
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. –  Sandro Dzneladze Jan 24 '12 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 '12 at 10:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.