2.Running Python Program-IDLE,Interactive Shell, Jupyter, How Python works

Download the latest release of Python from www.python.org depending on your OS Windows,Linux or Mac

Complete the installation.This will also install IDLE( Integrated Development Environment).
Visit https://docs.python.org/3/library/idle.html to get complete details of IDLE.

1.Open IDLE by clicking the application icon
2.Open File menu and click New File and type your first script ( Eg: print("welcome to Python")
3.Save your file with .py extension ( Eg:test.py)
4.From the Run menu click the Run Module( or press F5-short cut).This will run the script
5.From the File menu choose Exit to quit from IDLE

How to Run Python Code Interactively

A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in python, or python3 depending on your Python installation, and then hit Enter.

Here’s an example of how to do this on Linux:
Open Terminal ( Control-Alt-T)
$ python3 
Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. 
>>>
The standard prompt for the interactive mode is >>>, so as soon as you see these characters, you’ll know you are in.
Now you can test your commands here interactively
>>>print("Welcome to Python")
Welcome to Python
>>>2+3
5
On Windows, the command-line is usually known as command prompt or MS-DOS console, and it is a program called cmd.exe.
type cmd to get the command prompt then type python
c:\>python  ( need PATH set to python.exe directory)
or type python in the search program .This will open python prompt >>>.

You can run the script previously created( test.py ) by importing it in the python command line
>>>import test.py

Jupyter

Jupyter Notebook is a web application that allows you to create and share documents that contain:
live code (e.g. Python code)
visualizations
explanatory text (written in markdown syntax)

Jupyter Notebook is great for the following use cases:
learn and try out Python
data processing / transformation
numeric simulation
statistical modeling
machine learning

Jupyter Notebook is perfect for using Python for scientific computing and data analysis with libraries like numpy, pandas, and matplotlib.

Setting Up Jupyter Notebook
The first step to get started is to visit the project’s website at http://www.jupyter.org:

Here you’ll find two options:
Try it in your browser
Install the Notebook

With the first option Try it in your browser you can access a hosted version of Jupyter Notebook. This will get you direct access without needing to install it on your computer.

The second option Install the Notebook will take you to another page which gives you detailed instruction for the installation. There are two different ways:

Installing Jupyter Notebook by using the Python’s package manager pip
Installing Jupyter Notebook by installing the Anaconda distribution

Especially if you’re new to Python and would like to set up your development environment from scratch using the Anaconda distribution is a great choice. If you follow the link (https://www.anaconda.com/download/) to the Anaconda download page you can choose between installers for Windows, macOS, and Linux:

Having installed the Anaconda distribution we can now start Jupyter Notebook by using the following command:

$ jupyter notebook

or start anaconda navigator and click the Jupyter launch button

This will launch Jupyter note book as shown below


Working With The Notebook
From the New menu choose Python3 this will open Jupyter Note book file as below.Save the file by giving a name.Note book files are saved with extension .ipynb


The notebook itself consists of cells. A first empty cell is already available after having created the new notebook:
This cell is of type “Code” and you can start typing in Python code directly. Executing code in this cell can be done by either clicking on the run cell button or hitting Shift + Return keys:
The resulting output becomes visible right underneath the cell.
You can change the cell type from Code to Markdown to include explanatory text in your notebook. To change the type you can use the dropdown input control:

a cell is active two modes distinguished:
edit mode
command mode

If you just click in one cell the cell is opened in command mode which is indicated by a blue border on the left.
The edit mode is entered if you click into the code area of that cell. This mode is indicated by a green border on the left side of the cell.

Another cool function of Jupyter Notebook is the ability to create checkpoint. By creating a checkpoint you’re storing the current state of the notebook so that you can later on go back to this checkpoint and revert changes which have been made to the notebook in the meantime.

Jupyter Notebook gives you several options to export your notebook. Those options can be found in menu File → Download as.
Note: Python programs can also be run using several online compiler tools. Try executing a program using an online Python compiler .https://www.onlinegdb.com/online_python_compiler

How Does the Interpreter Run Python Scripts?
Most probably you have read somewhere that the Python language is an interpreted programming or a script language. The truth is: Python is both an interpreted and a compiled language. But calling Python a compiled language would be misleading.
A compiler is a computer program that transforms (translates) source code of a programming language (the source language) into another computer language (the target language). In most cases compilers are used to transform source code into executable program, i.e. they translate code from high-level programming languages into low (or lower) level languages, mostly assembly or machine code.

An interpreter is a computer program that executes instructions written in a programming language. It can either execute the source code directly or translate the source code in a first step into a more efficient representation and execute this code.

When you try to run Python scripts, a multi-step process begins. In this process the interpreter will:

1.Process the statements of your script in a sequential fashion
2.Compile the source code to an intermediate format known as bytecode

This bytecode is a translation of the code into a lower-level language that’s platform-independent. Its purpose is to optimize code execution. So, the next time the interpreter runs your code, it’ll bypass this compilation step.
3.Ship off the code for execution
At this point, something known as a Python Virtual Machine (PVM) comes into action. The PVM is the runtime engine of Python. It is a cycle that iterates over the instructions of your bytecode to run them one by one.
The whole process to run Python scripts is known as the Python Execution Model.


Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020