1. Interactive Mode :
The way of running single line or blocks of python code is known as interactive python execution mode.
Interactive mode is also known as REPL.
REPL provides simple and quick way of running blocks or single line of Python code.
Python codes executes via the Python shell
i.e >>> triple greater than sign, which comes by default with Python installation.
Interactive mode is one of the simple and very handy execution mode.
This mode is used to execute Python Basic commands.
Lets understand interactive mode which is also known as REPL.
REPL stands for ‘Read-Eval-Print-Loop’.
REPL shell provides the results with single line Python Commands.
Lets understand what is ‘Read-Eval-Print-Loop’ in Detail.
Read:
This Read function is used to accept an input from user and stores it in python variable or memory.
Eval:
The Eval function in python evaluates the ‘input’ from memory.
Print:
Print function prints the output from the eval function.
Loop:
This function creates loop and terminates automatically itself when the program ends.
Interactive mode is command line shell executes python statements one by one and gives immediate feedback for each statement.
This mode is very convenient when you simply wish to execute Python Basic Commands.
Lets get hands on this beautiful Language.
Run python in interactive mode:
Open command prompt in windows.
On other OS for e.g Linux or macOS open the terminal and type “Python” and press the enter key.
This will open the python shell in interactive mode.
# Python Script to get input from user and display it on console
Microsoft Windows [Version 10.0.19045.2604]
(c) Microsoft Corporation. All rights reserved.
C:\Users\HP>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
When you see this prompt means that the python shell, interactive execution mode is ready.
Now we are ready to perform our task or run simple command or python script. Our you can just test Python functions using this mode.
Let us have some example on the interpreter :
The Print() Function
:
Prints the specific message to console or screen or other standard devices.
The message can be any objects or strings.
>>> print(" I am print() and prints something i.e message in interpreted mode.")
Output:
I am print() and prints something i.e message in interpreted mode.
>>>
Performing some Mathematical operations:
>>> 1+1
2
>>> 5*5
25
>>> 600-34
566
>>> 56//67
0
>>> 56/5
11.2
>>> 56//5
11
Storing The value and Performing some operations:
>>> p="Python"
>>> p*3
'PythonPythonPython'
>>> y="Language"
>>> print("I am not " +p+ " but I am a" +y)
I am not Python but I am a Language
Getting Help from Interactive Shell:
Every beginners or professional programmers needs help to understand or work better.
Getting help from python interpreter is simple.
Simply type help() in shell and press enter key.
>>> help()
Welcome to Python 3.11's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the internet at https://docs.python.org/3.11/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit
help>
help>print
Help on built-in function print in module builtins:
print(*args, sep=' ', end='\n', file=None, flush=False)
....
..
.
Type q to exit from help.
Advantages interactive mode.
1. Helpful when your script is extremely short and you want immediate results.
2. Faster as you only have to type a command and then press the enter key to get the results.
3. Good for beginners who need to understand Python basics.
Disadvantages interactive mode.
1. Editing the code in interactive mode is hard as you have to move back to the previous commands or else you have to rewrite the whole command again.
2. It's very tedious to run long pieces of code.
2. Script Mode :
Script mode is the execution mode used when user is working with block of code or more than one lines of code.
Script mode is used when user want to execute one or more lines of code. One or more number of lines of code is executed in one execution.
If someone want to write long piece of code, then python interactive mode is not recommended.
To write piece of code we need to save(store) in the python file with .py extension and this file is known as script.
Text editor is used to write python script.
Number of popular text editors are available to write python script. E.g Sublime, Atom, notepad++,etc.
Upon execution the python script, all commands executes in one go.
Write and Execute Python Script:
1. From python IDLE shell goto File Menu.
2. Then press new file or Ctl+N ,after it. new file dialog opens up.
3. Write python script.
e.g
print(“Wellcome to Python”)
print(“Developed by Guido Van Rossum”)
4. Save the script by .py extension.
e.g welcome.py
5. Execute the script(or the file welcome.py).
a. go to run menu and the press Run to execute module.
You will get the Following output.
Wellcome to Python
Developed by Guido Van Rossum
Or
Just press F5 to execute the script.
b. python script can be executed in interactive mode.
i. on windows :
it is possible to run python script either of ways on recent windows operating system.
C:\users\python> python welcome.py
Or
C:\users\python> welcome.py
Wellcome to Python
Developed by Guido Van Rossum
ii. linux/unix:
python script executed with following commands.
$#Assign execution permissions
$chmod + x hello.py
$ # Run the script by using its filename
$ ./welcome.py
Wellcome to Python
Developed by Guido Van Rossum
Advantages of Script mode.
1. Simple and Easy to run large number of code.
2. Editing and modification in script is easier in script mode.
3. Good for both beginners and experts.
Disadvantages of Script mode.
1. Script mode will be tedious when you need to run only a single or a few lines of code.
2. We need to create and save a file before executing script or code.
previous topic TOKENS || Next Topic VARIABLES and IDENTIFIERS