Sunday, June 19, 2011

Interactive vs Script

Here I want to discuss about the differences in interactive mode and script mode. Some changes in code must be done in order to execute the same program in both modes.

If you type an expression in interactive mode, the interpreter evaluates it and displays the result:

>>> 1 + 1

But in a script, an expression all by itself doesn’t do anything!

We can solve our confusion by an example.

The author of Think Python has given a problem for solving this.

Type the following statements in the Python interpreter to see what they do:
x = 5
x + 1
Now put the same statements into a script and run it. What is the output? Modify the script by transforming each expression into a print statement and then run it again.

When I run the code in interactive mode I got the output as usual.

When I run the same in script code I got no output. So I tried the same program with print statements and I got the results.

In script mode we must use the print statement in order to display some outputs whereas in interactive mode print statement is not necessary.



No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.