Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
146 views

Ways of Working in Python: Interactive Mode

Interactive Mode allows users to type Python commands directly into the interpreter and get immediate feedback, but sessions cannot be saved as reusable programs. Script Mode allows users to write, save, and run Python programs by writing code in a file with a .py extension. When the file is run, all commands are executed together rather than individually. This avoids the "command-output sandwiching" of Interactive Mode and allows programs to be reused. Printing output requires the print() function in Script Mode, unlike Interactive Mode which displays output automatically.

Uploaded by

Shivanshu Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Ways of Working in Python: Interactive Mode

Interactive Mode allows users to type Python commands directly into the interpreter and get immediate feedback, but sessions cannot be saved as reusable programs. Script Mode allows users to write, save, and run Python programs by writing code in a file with a .py extension. When the file is run, all commands are executed together rather than individually. This avoids the "command-output sandwiching" of Interactive Mode and allows programs to be reused. Printing output requires the print() function in Script Mode, unlike Interactive Mode which displays output automatically.

Uploaded by

Shivanshu Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ways of Working in Python

Interactive Mode

• In Interactive Mode, the user types one command at a time, and the Python Interpreter executes
the command there and then and gives the output.

Script Mode

• In the Script Mode, the user can write a Python programme in a file, save it and then use the
interpreter to execute it.

1
1
Interactive Mode in IDLE

These three
greater than
signs >>>
are the As soon as you type the command and press enter, Python returns the Output
Python
Command
This way of giving name or expression in front of the prompt is called displaying.
Prompt
asking the
user to type
the
command

This indicates an error in the command typed.

2
2
Why is printing Hello World the first programme in
every language?
• “Hello, World!” is known as the first example in nearly every programming
language for every programmer, where did this message come from?

• As a function, the computer program simply tells the computer to display the
words “Hello, World!” Traditionally, it’s the first program developers use to test
systems. For programmers, seeing the two words on the screen means their code
can compile, load, run and they can see the output.

• Brian Kernighan, author of one of the most widely read programming books, "C
Programming Language", used "Hello, World". He first referenced ‘Hello World’
in the C Programming Language book’s predecessor: A Tutorial Introduction to
the Programming Language B published in 1973.

3
3
Why is printing Hello World the first programme in
every language?

• Unfortunately, the legend himself can’t definitely pinpoint when or why he chose
the words “Hello, World.” When asked what sparked the idea for the name
“Hello, World” in interview with Forbes India, he said his memory’s dim.

• “What I do remember is that I had seen a cartoon that showed an egg and a
chick and the chick was saying, “Hello, World.”

4
4
Limitations of the Interactive Mode
• The Interactive Mode proves very useful for testing a code; the user can type the
commands one by one and get the result or error one by one.

• The Python Shell allows the user to save the entire session (commands followed
by their results – as they appear on the screen).

• However, since the output is sandwiched between the command lines, the saved
file cannot be used a programme file for future use.

• The solution to this problem is the Script Mode.

5
5
Working in the Script Mode
• In this, we create and save a module/script/programme file.

• To do so, we go to File -> New in the IDLE Python Shell.

• In the New Window that opens, type the commands you want to save in the form
of a programme (or script)

• Click File -> Save and then save the file as filename.py (Python Files have .py
extension)

• Next, open the file you created in the previous step by using IDLE’s File->Open
Command.

6
6
Working in the Script Mode

• If the file is already open, you can directly move to the next step.

• Click Run->Run Module command in the file’s window. Alternatively, you can
press the F5 key.

• This will execute all the commands stored in the file in the Python Shell Window.

• Thus, in the script mode, we can store all commands together in the form of a
module/programme/script and can get all output lines together. Hence, there is no
more command-output sandwiching.

7
7
8
8
9
9
• Note that in a script environment, the “print” command must be explicitly given in order for the result to be printed
out. Hence, simply writing 3+5 won’t give an output. You will have to write print(3+5). Similarly, simply writing a won’t
display the value of the variable a.
• Python is a Case-Sensitive Language. The command here should be print() and not Print().
• Since Python uses an interpreter, as soon as it encounters an error, the execution stops. Hence, the commands
after the error is encountered are not executed.
10
10

You might also like