Executing multi-line statements in python in interactive mode

后端 未结 2 717
我在风中等你
我在风中等你 2021-01-27 15:37

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

         


        
相关标签:
2条回答
  • 2021-01-27 16:08

    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
    
    0 讨论(0)
  • 2021-01-27 16:13

    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
    
    0 讨论(0)
提交回复
热议问题