0

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
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • Can you clarify what you mean by not seeing the random number? In the Python prompt, on Python 2.7, doing `random.random()` just prints out a random number [0.0, 1.0). – Miket25 Dec 24 '17 at 21:33
  • @Mike- I just wanted to print a random number and the sqrt. However, Python only printed the sqrt. I didn't see random number at all. – watchtower Dec 24 '17 at 21:58
  • 1
    You provided also your input, but could you also provide your expected output in the format you desire? Perhaps that'll clear up some confusion – Miket25 Dec 24 '17 at 22:15
  • @Miket - Thanks again for your help. I have added expected output and input, and keyboard shortcut in PyCharm. – watchtower Dec 24 '17 at 22:26
  • I wasn't able to duplicate your behavior in pycharm, even in the console. Have you tried this code in the python console through a basic Terminal window? – Miket25 Dec 24 '17 at 23:26
  • @Miket - You are right. In basic python console, it would execute every line by itself, but not so in PyCharm. Do you want me to post screenshot from PyCharm? – watchtower Dec 25 '17 at 06:23

2 Answers2

1

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
SigmaPiEpsilon
  • 678
  • 5
  • 15
  • Thank you so much for your answer. I liked the approach for `InteractiveShell.ast_node_interactivity = "all"`. Is there any way I can add this code to profile file or in PyCharm so that I don't have to copy-paste the same code again for every script? I come from R world, and executing a block of code is very common in R Programming. I tried adding `ipython_config.py` to `..\.ipython\profile_default` on Win machine, but nothing happened. I even restarted my PyCharm, but no luck. Today is my first day with Python. I'd appreciate your help. I am using Anaconda on PyCharm. – watchtower Dec 25 '17 at 02:02
  • If you generated the `ipython_config.py` file in a standard way by entering `ipython profile create` from cmd then that file should contain a commented line like `# c.TerminalInteractiveShell.ast_node_interactivity = 'last_expr'`. Change `last_expr` to `all` and uncomment the line. Then restarting ipython should work. If the above line is not present then type it in manually. There should be an uncommented line like `c = get_config()` at the top of this file for all this to work though. – SigmaPiEpsilon Dec 25 '17 at 02:17
  • very respectfully, I have been trying to search and understand how to run `ipython profile create`. When I run that `ipython` command (i.e. Win + R and then I would type above command), I get Windows cannot find `ipython`. Is there anything you could point me to? This is my first day. So, I am very sorry. – watchtower Dec 25 '17 at 02:27
  • I do not use Windows but if you installed ipython through anaconda `conda install ipython` it should put the ipython command in your path. Type Win + R and cmd to open a DOS shell first. Then type the above command. Normally the executable (`ipython.exe`) is located inside the `Scripts` folder in the Anaconda installation folder. If not in your path you can navigate there and run it as `ipython profile create`. – SigmaPiEpsilon Dec 25 '17 at 02:44
0

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
Miket25
  • 1,895
  • 3
  • 15
  • 29
  • Thanks Miket. Is there any way to execute these statements without using separator with the exception of new line character? As you can see in my post, I didn't add any separator except newline character between lines – watchtower Dec 24 '17 at 21:57