Executing multi-line statements in python in inter

2020-05-03 10:06发布

I am new to the world of Python, and this is my first program in Python. I come from the world of R so this is a bit unintuitive to me.

When I execute

In[15]: import math
   ...: import random
   ...: random.random()
   ...: math.sqrt(85)
   ...: 

in interactive mode (i.e. on >>> prompt), I get the output of math.sqrt(). I don't see the random number at all. However, when I execute random.random() and math.sqrt() separately, I do get both the results. Why is this? How do I get both the results? I am asking this because I have the habit of selecting multiple lines in R and executing them in interactive mode, especially when I am trying to debug code. Executing only one line at a time will be excruciating when trying to debug, say, 2000-line code.

Here's what happens when I select multiple lines in R and then execute them:

runif(1,0,1)
  sqrt(85)

It automatically parses these lines and generates the following output:

> runif(1,0,1)
[1] 0.01597949
>   sqrt(85)
[1] 9.219544

I researched on SO and found Write multi-line statement on single line. I am unsure how to use the official answer from this thread.


Here's the output on PyCharm:

import math
import random
random.random()
math.sqrt(85)
Out[15]: 9.219544457292887

I am looking for some mechanism to see a random number and the sqrt of 85 without having to add any separator , or ;, as I did in R code. I'd appreciate any thoughts.

Expected Input: One would run the code using "Execute Selection using Console" in PyCharm [Keyboard Shortcut: Alt + Shift + E]

import math
import random
random.random()
math.sqrt(85)

[Please note that I haven't used any separator ";" or ","--just as we do in R]

Expected Output:

random.random()
Out[16]: 0.183145720117748
math.sqrt(85)
Out[17]: 9.219544457292887

2条回答
家丑人穷心不美
2楼-- · 2020-05-03 10:50

In the Python 2.7.10 console, doing the following: importing math and random, getting a random number, and taking a square root of a number outputs as such:

>>> import math
>>> import random
>>> random.random()
0.52350453737542
>>> math.sqrt(85)
9.219544457292887 

If you want both values to be printed contiguously, you can write multiple-line statements separated by the semi-colon operator:

>>> import math
>>> import random
>>> random.random(); math.sqrt(85)
0.9031053664569808
9.219544457292887
查看更多
霸刀☆藐视天下
3楼-- · 2020-05-03 10:53

One of the reasons you are probably seeing the output of math.sqrt() only is because by default ipython/jupyter will only give you output of the last line of a multiline command. Python interpreter executes code line by line and any statement like random.random() which are not explicitly printed are just evaluated and thrown away. Ipython/Jupyter by default gets the result of the last line and displays it. I have not used pycharm but it probably does the same thing. To see the output of random.random() you have two options:

1) Use a print statement like below.

In [1]: import math
import random
print random.random()
math.sqrt(5)
   ...: 
0.145504928627
Out[1]: 2.23606797749979

In [2]: 

2) Change the default behavior by changing the ast_node_interactivity parameter as answered here

In [1]: from IPython.core.interactiveshell import InteractiveShell

In [2]: InteractiveShell.ast_node_interactivity = "all"

In [3]: import math
import random
random.random()
math.sqrt(5)
   ...: 
Out[3]: 0.9772320535532782
Out[3]: 2.23606797749979

IPython has a variety of magic commands like %edit and %load which allows you to directly edit the commands in your favorite editor or load specific lines of code from a file like below. This can help with your requirement of selecting specific sections of your long code and running them separately. Below I have saved the above lines in a file called test_interpreter.py.

In [1]: !cat test_interpreter.py
import math
import random
print random.random()
math.sqrt(5)

In [2]: load -r 1-4 test_interpreter.py

In [3]: # %load -r 1-4 test_interpreter.py
import math
import random
print random.random()
math.sqrt(5)
   ...: 
0.719244573423
Out[3]: 2.23606797749979

The python REPL in unix shells by default does not support multiline commands (as far as I know of) but you can use ; to terminate and start a new line with \ which is escape character for readline which python REPL uses.

>>> import math;\
... import random;\
... random.random();\
... math.sqrt(5)
0.10298483416372617
2.23606797749979

In both normal python interpreter and ipython you can however directly copy paste lines from your editor and they will be evaluated separately like below.

#Copy paste four lines together
>>> import math
>>> import random
>>> random.random()
0.16039452147586075
>>> math.sqrt(5)
2.23606797749979
查看更多
登录 后发表回答