Why does typing a variable (or expression) print the value to stdout?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












17















Take this example:



>>> 5+10
15
>>> a = 5 + 10
>>> a
15


How and why does Python do this without an explicit print statement?



If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:



In[1]: 5+10
1

Out[1]: 1


Why does this happen?










share|improve this question




























    17















    Take this example:



    >>> 5+10
    15
    >>> a = 5 + 10
    >>> a
    15


    How and why does Python do this without an explicit print statement?



    If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:



    In[1]: 5+10
    1

    Out[1]: 1


    Why does this happen?










    share|improve this question


























      17












      17








      17


      0






      Take this example:



      >>> 5+10
      15
      >>> a = 5 + 10
      >>> a
      15


      How and why does Python do this without an explicit print statement?



      If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:



      In[1]: 5+10
      1

      Out[1]: 1


      Why does this happen?










      share|improve this question
















      Take this example:



      >>> 5+10
      15
      >>> a = 5 + 10
      >>> a
      15


      How and why does Python do this without an explicit print statement?



      If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:



      In[1]: 5+10
      1

      Out[1]: 1


      Why does this happen?







      python ipython read-eval-print-loop python-interactive






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 25 at 12:41









      Peter Mortensen

      13.8k1987113




      13.8k1987113










      asked Feb 25 at 4:19









      Chayan GhoshChayan Ghosh

      1507




      1507






















          2 Answers
          2






          active

          oldest

          votes


















          35














          When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.




          If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.



          sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.




          You can modify this behavior:



          >>> import sys
          >>> def shook(expr):
          ... print(f'can haz expr?')
          ...
          >>> sys.displayhook = shook
          >>> 123
          can haz 123?
          >>> False
          can haz False?
          >>> None
          can haz None?


          And also set it back to normal:



          >>> sys.displayhook = sys.__displayhook__
          >>> 3
          3


          In the default Python repl, sys.displayhook is



          >>> import sys;
          >>> sys.displayhook
          <built-in function displayhook>


          but in IPython it's



          In [1]: import sys

          In [2]: sys.displayhook
          Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>


          So that's why you see different behavior between Python and IPython.






          share|improve this answer
































            2














            That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:



            >>> 'blah'
            'blah'
            >>> print('blah')
            blah
            >>>


            Look at the quotes.



            Also see this:



            >>> print(repr('blah'))
            'blah'
            >>>


            repr does the same.






            share|improve this answer




















            • 1





              any comment on the IPython behavior?

              – Chayan Ghosh
              Feb 25 at 4:25






            • 2





              Let's say CPython in interactive mode works like that.

              – Klaus D.
              Feb 25 at 4:25










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859437%2fwhy-does-typing-a-variable-or-expression-print-the-value-to-stdout%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            35














            When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.




            If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.



            sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.




            You can modify this behavior:



            >>> import sys
            >>> def shook(expr):
            ... print(f'can haz expr?')
            ...
            >>> sys.displayhook = shook
            >>> 123
            can haz 123?
            >>> False
            can haz False?
            >>> None
            can haz None?


            And also set it back to normal:



            >>> sys.displayhook = sys.__displayhook__
            >>> 3
            3


            In the default Python repl, sys.displayhook is



            >>> import sys;
            >>> sys.displayhook
            <built-in function displayhook>


            but in IPython it's



            In [1]: import sys

            In [2]: sys.displayhook
            Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>


            So that's why you see different behavior between Python and IPython.






            share|improve this answer





























              35














              When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.




              If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.



              sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.




              You can modify this behavior:



              >>> import sys
              >>> def shook(expr):
              ... print(f'can haz expr?')
              ...
              >>> sys.displayhook = shook
              >>> 123
              can haz 123?
              >>> False
              can haz False?
              >>> None
              can haz None?


              And also set it back to normal:



              >>> sys.displayhook = sys.__displayhook__
              >>> 3
              3


              In the default Python repl, sys.displayhook is



              >>> import sys;
              >>> sys.displayhook
              <built-in function displayhook>


              but in IPython it's



              In [1]: import sys

              In [2]: sys.displayhook
              Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>


              So that's why you see different behavior between Python and IPython.






              share|improve this answer



























                35












                35








                35







                When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.




                If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.



                sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.




                You can modify this behavior:



                >>> import sys
                >>> def shook(expr):
                ... print(f'can haz expr?')
                ...
                >>> sys.displayhook = shook
                >>> 123
                can haz 123?
                >>> False
                can haz False?
                >>> None
                can haz None?


                And also set it back to normal:



                >>> sys.displayhook = sys.__displayhook__
                >>> 3
                3


                In the default Python repl, sys.displayhook is



                >>> import sys;
                >>> sys.displayhook
                <built-in function displayhook>


                but in IPython it's



                In [1]: import sys

                In [2]: sys.displayhook
                Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>


                So that's why you see different behavior between Python and IPython.






                share|improve this answer















                When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.




                If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.



                sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.




                You can modify this behavior:



                >>> import sys
                >>> def shook(expr):
                ... print(f'can haz expr?')
                ...
                >>> sys.displayhook = shook
                >>> 123
                can haz 123?
                >>> False
                can haz False?
                >>> None
                can haz None?


                And also set it back to normal:



                >>> sys.displayhook = sys.__displayhook__
                >>> 3
                3


                In the default Python repl, sys.displayhook is



                >>> import sys;
                >>> sys.displayhook
                <built-in function displayhook>


                but in IPython it's



                In [1]: import sys

                In [2]: sys.displayhook
                Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>


                So that's why you see different behavior between Python and IPython.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 25 at 13:52

























                answered Feb 25 at 4:33









                kojirokojiro

                54.1k1390142




                54.1k1390142























                    2














                    That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:



                    >>> 'blah'
                    'blah'
                    >>> print('blah')
                    blah
                    >>>


                    Look at the quotes.



                    Also see this:



                    >>> print(repr('blah'))
                    'blah'
                    >>>


                    repr does the same.






                    share|improve this answer




















                    • 1





                      any comment on the IPython behavior?

                      – Chayan Ghosh
                      Feb 25 at 4:25






                    • 2





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      Feb 25 at 4:25















                    2














                    That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:



                    >>> 'blah'
                    'blah'
                    >>> print('blah')
                    blah
                    >>>


                    Look at the quotes.



                    Also see this:



                    >>> print(repr('blah'))
                    'blah'
                    >>>


                    repr does the same.






                    share|improve this answer




















                    • 1





                      any comment on the IPython behavior?

                      – Chayan Ghosh
                      Feb 25 at 4:25






                    • 2





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      Feb 25 at 4:25













                    2












                    2








                    2







                    That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:



                    >>> 'blah'
                    'blah'
                    >>> print('blah')
                    blah
                    >>>


                    Look at the quotes.



                    Also see this:



                    >>> print(repr('blah'))
                    'blah'
                    >>>


                    repr does the same.






                    share|improve this answer















                    That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:



                    >>> 'blah'
                    'blah'
                    >>> print('blah')
                    blah
                    >>>


                    Look at the quotes.



                    Also see this:



                    >>> print(repr('blah'))
                    'blah'
                    >>>


                    repr does the same.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 25 at 12:43









                    Peter Mortensen

                    13.8k1987113




                    13.8k1987113










                    answered Feb 25 at 4:21









                    U9-ForwardU9-Forward

                    16.9k51643




                    16.9k51643







                    • 1





                      any comment on the IPython behavior?

                      – Chayan Ghosh
                      Feb 25 at 4:25






                    • 2





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      Feb 25 at 4:25












                    • 1





                      any comment on the IPython behavior?

                      – Chayan Ghosh
                      Feb 25 at 4:25






                    • 2





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      Feb 25 at 4:25







                    1




                    1





                    any comment on the IPython behavior?

                    – Chayan Ghosh
                    Feb 25 at 4:25





                    any comment on the IPython behavior?

                    – Chayan Ghosh
                    Feb 25 at 4:25




                    2




                    2





                    Let's say CPython in interactive mode works like that.

                    – Klaus D.
                    Feb 25 at 4:25





                    Let's say CPython in interactive mode works like that.

                    – Klaus D.
                    Feb 25 at 4:25

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859437%2fwhy-does-typing-a-variable-or-expression-print-the-value-to-stdout%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown






                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?