Skip to content

[Python] A Line Breaking In The IPython Interface

Last Updated on 2023-12-18 by Clay

Introduction

IPython is a system that provided a interactive interface, it could be integrated with shells or graphical interface; for example we can use ipython (if you had been installed it), or you can use some GUI editor/IDE such as VS Code and PyCharm.

The core of famous Jupyter Notebook is used IPython. It is a very useful tool, sometimes I need for compute something, I always open it, Not to mention that I have many requirements to try toy codes.

But a problem is confuse me: How should I make a line breaking in it? I do not like my code running is line by line.

Search Internet, I found two solutions and share at here.


Solution

The first solution is add the ; \ at the end of your code, yes it does.

The python programmer have been know, Python allow us write many codes in one line, and use ; to separate them.

And the \ is another syntax, it can make the one line code to be wrap and make our code clean and pretty.

Line breaking in IPython is to combine these two methods: first enter ; which means that the line you declared has ended. But at this time, because you pressing enter directly will execute this line, adding a \ symbol means that the next code needs to work with the current line.

So it becomes that, in fact you don't need the next line together, but you "trick" IPython by adding this line so that it doesn't let the program start running.

In [1]: a = 1; \
   ...: b = 2; \
   ...: a + b
Out[1]: 3



It was a bit of weird but worked.

The second method is, you can enter Ctrl+o or Alt+o to do line breaking. But it would not worked in some device.


References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version