2

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?

1
  • You need print(a) to print the value of a in a script. The interpreter automatically prints it for ease of use. Commented Nov 28, 2016 at 18:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain the way that this happens .. ( I mean the verbose way in depth of programming)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.