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
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:
If you want both values to be printed contiguously, you can write multiple-line statements separated by the semi-colon operator:
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 likerandom.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.
2) Change the default behavior by changing the
ast_node_interactivity
parameter as answered hereIPython 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 calledtest_interpreter.py
.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 forreadline
which python REPL uses.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.