Search code examples
pythonpython-idle

Python Function acts differently when run in shell and when run as a .py file


Python function acts differently when run in shell vs when run as .py file

def hell():
    return 'hello people'

hell()

when I run this program in python shell I am getting output 'hello people' and when i run this program after saving as .py file and running it I am not getting any output and also no errors. what is happening here can anybody explain? Thanks in advance.


Solution

  • In idle you enter one command, and it executes and prints the results to stdout. Idle always prints the returned value (unless it is none). Try this from idle:

    >>>5==5
    True
    

    It prints True because the equality operator returns true. Wheras if you run this as a script you get no result. To get the same result outside of idle add the print function.

    print(5==5)
    

    Now it prints to stdout.