PRACTICAL 1
Getting started with Python and IDLE in interactive
and batch modes
In Python, there are two options/methods for
running code:
Interactive mode
Script mode/ Batch mode
Interactive Mode
Interactive mode, also known as the REPL provides
us with a quick way of running blocks or a single
line of Python code. The code executes via the
Python shell, which comes with Python installation.
Interactive mode is handy when you just want to
execute basic Python commands or you are new to
Python programming and just want to get your
hands dirty with this beautiful language.
To access the Python shell, open the terminal of
your operating system and then type "python".
Press the enter key and the Python shell will
appear.
C:\Windows\system32>python Python
3.5.0 (v3.5.0:374f501f4567, Sep 13 2015.
02:27:37) [MSC v.1900 64 bit (AMD64)] on
win32 Type "help", "copyright". "credits"
or "license" for more information.
>>>
The >>> indicates that the Python shell
is ready to execute and send your
commands to the Python interpreter.
The result is immediately displayed on
the Python shell as soon as the Python
interpreter interprets the command.
To run your Python statements, just
type them and hit the enter key. You will
get the results immediately, unlike in
script mode. For example, to print the
text "Hello World", we can type the
following:
>>print("Hello World")
Hello World
>>>
Script Mode/ Batch mode
If you need to write a long piece of
Python code or your Python script spans
multiple files, interactive mode is not
recommended. Script mode is the way to
go in such cases. In script mode, you
write your code in a text file then save it
with a .py extension which stands fo
r "Python". Note that you can use any
text editor for this, including Sublime,
Code Block, notepad++, Dev C++ etc.
If you are in the standard Python shell,
you can click "File" then choose "New" or
simply hit "Ctrl + N" on your keyboard to
keyboard to open a blank
script in which you can
write your code. You can
then press "Ctrl+S" to
save it.
Let us create a new file
from the Python shell and
give it the nam
e "hello.py". We need to
run the "Hello World"
program. Add the
following code to the file:
print("Hello World")
Click "Run" then choose "Run Module".
This will run the
program:offi
Output
hellopy-C/Users/admin/hello.py (3.5.0)
File Edit
Format Run
Options
Window
Help
LLC
print("Hello World")
Python 3.5.0 Shell
File Edit Shell Debug Options
Window
Help
Python 3.5.0 (v3.5.0:374150114567, Sep
13 2015, 02:27:37) (MSC
D64)] on win32
Type "copyright", "credits" or "license()"
for more informatic
or a thon you
Hello World
>>>
RESTART: C:/Users/admin/hello.py
mon".
15 t
Chon eter
ults the
Key Differences Between Interactive
and Script Mode
Here are the key differences between
programming in interactive mode and
programming in script
mode:
1. In script mode, a file must be created
1. In script mode, a file must be
created and saved before
executing the code to get results.
In interactive mode, the result is
returned immediately after
pressing the enter key..
2. In script mode, you are
provided with a direct way of
editing your code. This is not
possible in interactive mode.
■ Conclusion
There are two modes through
which we can create and run
Python scripts: interactive mode
and script mode. The interactive
mode involves running your
codes directly on the Python shell which
can be accessed from the terminal of
the operating system. In the script mode,
you have to create a file, give it a name
with a py the extension then runs your
code. The interactive mode is suitable
when running a few lines of code. The
script mode is recommended when you
need to create large applications.