Question:
A question about python prompt/script mode?
Zeroxis
2013-04-29 20:21:20 UTC
Hi all.

Why is it that if i type in 1 + 1 in the python prompt, it evaluates and shows 2 like this:

>>> 1 + 1
2

but if i type it in the script and press run it shows:

*** Remote Interpreter Reinitialized ***
>>>
>>>

Why is the resulting value not showing in script mode?
Three answers:
?
2013-05-01 01:22:57 UTC
- - - - - - - - - -

Long answer

- - - - - - - - - -



When you run a python script (a file with python code), you invoke the Python Interpreter and pass it the name and path of your file, perhaps with some other optional arguments. This is done in the shell (the command line).



Python runs your script and if you have some print statements in there it will print whatever it's supposed to print in the same console window. When it's done executing all the code the Python Interpreter closes itself and the console window that it opened.



Now there is another mode which you mentioned called the Interactive Mode. This interactive mode is a program just like the Python Interpreter is a program. What it does is it displays a prompt called the primary prompt ">>>" and when you give it some code, it executes that code and displays the result on the screen, then it prompts you again ">>>". This all happens in a loop called a read-eval-print loop, because it reads the code you type in, eval means it executes it, and print means it displays the result back to you even if you didn't explicitly ask it to print.



So basically, the interactive mode is just a feature of Python, a tool for running code interactively and one of the differences between that and simply running a script is that for it to be interactive it has to constantly communicate with you and it does that by printing the result on the screen, by design.



When you run a script the reason it doesn't show up is because you have to explicitly tell it to print, it's not interactive anymore so it's going to behave like any normal programming language and do only what you tell it to.



- - - - - - - - - -

Short answer

- - - - - - - - - -



Normally, Python will display something on the screen only if you tell it to or if it encounters and error.



Pythons interactive mode (prompt mode) on the other hand is a small program/tool for using Python in an interactive way. The word interactive implies a higher level of feedback and this means that it will take it's own initiative to uphold the definition of interactive by printing the repr() (object representation) of a value on the screen if you don't explicitly tell it what to print.
2017-01-13 22:35:45 UTC
The "script" is only a textual content record with Python statements in it. In different words, that is a Python source application. So, open up an editor, style in those strains, and save the end result as "runme.py", or %. your own call. The ".py" suffix means that the record incorporates Python source code. Run that source record ("script") from the a terminal or command prompt window with: python runme.py in case you do not see any output, do not problem. that is predicted. Python doesn't instantly print out result values for each line in a operating application the way it does at the same time as unmarried strains are executed interior the "interpreter". flow ahead and convert the statements to print statements, then re-save and run. you should verify then.
James Miller
2013-04-29 20:50:51 UTC
Check out the print function that is built into python and every other programing lang.



print("1 + 1 is ", 1+1)



Will display: 1+1 is 2



print("print displays things on the screen")



Also is the file saved as filename.py

Did you chmod 755 filename.py

Or chmod +x filename.py



Are you running the file like such

python filename.py or ./filename.py


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...