Vignesh Nayak Manel
Vignesh Nayak Manel

Reputation: 383

Python interpretation difference in interactive mode and script mode

If I initialize a variable and just give the name of the variable without 'print' in interactive mode then it value will be printed, for example

>>>a=10
>>>a
10
>>>

But if I do this in a script, neither the value gets printed nor any error is generated, for example consider the below code in a script example.py

a=10
a

If I execute this script a blank line gets printed and not the value. Why there is a difference in interactive mode and script mode output?

Upvotes: 2

Views: 9533

Answers (2)

Amin Etesamian
Amin Etesamian

Reputation: 3699

Python has two basic modes: normal and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.

Upvotes: 1

user2357112
user2357112

Reputation: 281683

It's a convenience feature. "Show me what this thing is" is a much more important operation in interactive mode than in a program, and it'd get tiring to keep writing print(repr(...)) all the time. In a program, printing the value of every expression statement would more often just be awkward and require you to suppress the output manually, so you have to print things explicitly.

Upvotes: 1

Related Questions