How to get to a new line in Python Shell?
Asked Answered
R

8

28

I want to write the following two lines on the console:

x = 3

print x**5

But when I type x = 3, I press enter, it then executes the assignment. How to get it to execute only after typing the second line?

Runoff answered 19/6, 2011 at 13:59 Comment(1)
@Wooble Because if I want to write a 100-words program, I have to type all of them in one line...Runoff
D
28

End lines with ;\:

>>> x=3;\
... print x**5
243
>>>
Danieldaniela answered 19/6, 2011 at 14:2 Comment(4)
Blah, that's nasty (but legal).Nahamas
Yeah it works, but two questions: 1.where did you learn this, in Python tutorial? 2.This is too unnatural, is there a way i can just type Enter to go to a new line, like in most IDEs?Runoff
@asunnysunday: You're working in an interactive interpreter. If you want to enter more than one statement before its executed, you'll need to use a file. In IDLE create a new file, then you can execute the file through IDLE. The window that's likely titled "Python Shell" is an interactive interpreter.Nahamas
@Runoff 1. Right there in the manual: docs.python.org/reference/… . 2. No. Instead, use a regular (or IDLE's) editor, and execute the whole Python program.Danieldaniela
J
37

Use the Ctrl-J key sequence instead of the Enter key to get a plain newline plus indentation without having IDLE start interpreting your code.

You can find other key sequences that make IDLE easier to use for this type of learning under the Options->Configure IDLE menu.

Jabe answered 16/8, 2012 at 19:4 Comment(4)
Does not seem to work with the original question though, if I type "x=3[C-j]print x**5" nothing is printed.Tallinn
Seems you can also use Shitf+Enter too (tried in Mac terminal)Toothpick
Note: some shells will require you to escape the Ctrl-J with a preceding Ctrl-V.Symposiac
when adding new lines.. how do you eventually run the multiple lines? cmd/crl-c returns in a KeyboardInterrupt and doesn't run the scriptLombardy
D
28

End lines with ;\:

>>> x=3;\
... print x**5
243
>>>
Danieldaniela answered 19/6, 2011 at 14:2 Comment(4)
Blah, that's nasty (but legal).Nahamas
Yeah it works, but two questions: 1.where did you learn this, in Python tutorial? 2.This is too unnatural, is there a way i can just type Enter to go to a new line, like in most IDEs?Runoff
@asunnysunday: You're working in an interactive interpreter. If you want to enter more than one statement before its executed, you'll need to use a file. In IDLE create a new file, then you can execute the file through IDLE. The window that's likely titled "Python Shell" is an interactive interpreter.Nahamas
@Runoff 1. Right there in the manual: docs.python.org/reference/… . 2. No. Instead, use a regular (or IDLE's) editor, and execute the whole Python program.Danieldaniela
L
10

after every line put \ mark and press enter

Langur answered 7/3, 2015 at 7:13 Comment(0)
S
7

To code group of statements, ;\ will work

   list1=[1,2,3]
   list2=[4,5,6]

   print list1;\
   print list2

Will print both the lists

Where as, for Indentation statement, you need :\

  for i in list1:\
  print i
  //double enter

Will show all the elements in the list

Silverplate answered 11/7, 2018 at 5:34 Comment(0)
J
2
x = 3; print x ** 5

should help, but it doesnt matter that its executed the way it is in IDLE.

Jerky answered 19/6, 2011 at 14:3 Comment(0)
P
1

Just open a new file: File > New window. You can run it by clicking run > run module.

Potbellied answered 19/6, 2011 at 14:3 Comment(0)
A
0

the "\" line at the end of the line works for me. in windows 10 cmd.

Edit: I've also noticed that you have to be consistent with its use, other wise you still get syntax error

Aile answered 21/3, 2021 at 21:16 Comment(0)
M
0

Go into editor mode with the F2 key. Depending on your terminal config this will put you in vim or nano. You can edit a snippet of code and then exit editor mode to get back to the ipython cli with your code and any newlines.

Marquetry answered 8/2 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.