Python Language Skill UP
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Python Execution Modes.

In this tutorial, we will Learn about Execution Modes of Python i.e command mode and Script mode.
We will also learn the Advantage and Disadvantage of running Python in different type of execution modes.


Execution Mode: “The Way or the option or the method of processing the python script is called execution mode”

Here in This tutorial we are going to learn python script execution modes. Python script can be executed in two ways.
1. Interactive Mode.
2. Script Mode.

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

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python.
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple 'Python' Scripts
    • Namespace in 'Python'
    • Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • Assignments
  • Input Output in Python
    • Accept Input
    • Output Formating
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • short hand IF-ELSE
    • Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • while loop assignments
    • for Loop
    • For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • break,continue and else in Loops
    • Difference between for and while loop
    • for each loop
    • for each Assignments
  • List
    • List in Python
    • Access List elements
    • List functions
    • Iterate(loop)List
    • List comprehension
    • List Assignments
  • Tuple
    • Tuple in Python
    • Access tuple elements
    • tuple functions
    • Iterate(loop)tuple
    • Unpack Tuple
    • tuple comprehension
    • tuple Assignments
  • set
    • Set in Python
    • Access set elements
    • set Methods
    • Iterate(loop)set
    • Pack/Unpack Tuple
    • tuple comprehension
    • set Assignments
    • Set comprehension
  • Dictionary
    • Dictionary
    • Access dictionary Items
    • dictionary method
    • Iterate(loop)Dictionary
    • formating Dictionaries
    • Nested Dictionaries
    • dictionary comprehension
    • dictionary Assignments
  • Diff list tuple set dictionary
    • list vs tuple
    • list vs set
    • list vs dictionary
    • tuple vs set
    • tuple vs dictionary
    • dictionary vs set
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • assignments
  • Functions
    • Introduction
    • Modular Programming
    • Type of functions
    • Inbuilt Function
    • Need For User-Defined Function
    • Elements Of User Defined Function
    • Function Argument
    • Nesting of Function
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • Assignments
  • Python Module
    • introduction to module
    • Inbuilt Module in Python
    • User Defined Module in Python
    • Assignments
  • File Handling
    • Introduction to files
    • Create File
    • Read files
    • Write to File
    • Rename File
    • Copy File
    • Move file
    • List files in Directory
    • Binary files
    • Zipping and unzipping files
    • Assignments
  • Strings
    • Basics of Strings
    • String Special Operator
    • String Formatting operator
    • String methods
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • Diff object oriented and procedural oriented programming
    • difference between classes and object
    • Constructors
    • destructor
    • built class methods and attributes
    • class and instance variable
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in python
    • Abstract class vs interface
    • Public in python
    • Private in python
    • Protected in python
    • Overloading Vs Overriding
    • inheritance vs composition
    • Encapsulation
    • Polymorphism
    • inner classes
    • Opps Assignments
  • Mysql database in python
    • introduction
    • DBMS vs file
    • connecting to database
  • Mysql database Operation
    • Select
    • Operators
    • DDL
    • DML
    • subqueries
    • joins
    • Case study
  • Graphics
  • Iterator in python
  • Generator in python
  • decorator in python
  • Threads in Python
    • introduction
    • process and threads
    • concurrent programming and GIL
    • Uses of threads
    • creating Threads
    • Single tasking
    • multi tasking
    • thread synchronization
  • Frequently Asked Interview Questions (FAQ)
  • Case Studies
  • Multiple Choice Questions

Get in touch

  • tech2dsm@gmail.com

© tech2dsm. All rights reserved.