Module 2
Module 2
1
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Environent
Python is available on a wide variety of platforms including Linux and Mac OS X. Let's
understand how to set up our Python environment.
2
© Nex-G Exuberant Solutions Pvt. Ltd.
Downloading and installing
3
© Nex-G Exuberant Solutions Pvt. Ltd.
Windows Installation:
• Go to the following link and follow the steps to install Python 3.8:
https://docs.python.org/3/using/windows.html
• Alternatively, I recommend using Anaconda again to install Python 3.8
https://www.anaconda.com/products/individual
• When installing Anaconda make sure to check the box “ADD ANACONDA3 TO MY
ENVIRONMENT PATH VARIABLES” to use python from command prompt.
4
© Nex-G Exuberant Solutions Pvt. Ltd.
Macintosh Installation:
• Latest Mac OS’s come with Python preinstalled but in case yours doesn’t use the
following link to install it.
https://www.dummies.com/programming/python/how-to-install-python-on-a-mac/
• Again, I highly recommend you get Anaconda as well.
https://www.anaconda.com/products/individual
5
© Nex-G Exuberant Solutions Pvt. Ltd.
Starting Python
A Python script can be executed at command line by invoking the interpreter on your
application, as in the following:
or
or
7
© Nex-G Exuberant Solutions Pvt. Ltd.
(3) Integrated Development Environment :
You can run Python from a Graphical User Interface (GUI) environment as well, if you have
a GUI application on your system that supports Python.
• Windows: PythonWin is the first Windows interface for Python and is an IDE with a GUI.
• Macintosh: The Macintosh version of Python along with the IDLE IDE is available from
the main website
There are other IDE’s as well: PyCharm, Spyder. Feel free to use any you like.
To practice Python or get into Data Science: Jupyter Notebook (comes with Anaconda) is
highly recommended.
8
© Nex-G Exuberant Solutions Pvt. Ltd.
Running Python Programs
Python Interpreter:
– Interpreter
– Library
9
© Nex-G Exuberant Solutions Pvt. Ltd.
Your First Script
HELLO WORLD
• Make a file hello.py with the following line.
print("Hello world!")
• The extension .py is not required but for consistency, Python file usually has that
extension.
• If we run the file on a Terminal :
python(hello.py)
• When we instruct Python to run our script, there are a few steps that Python carries out
before our code actually starts crunching away:
• It is compiled to bytecode.
• Then it is routed to virtual machine.
10
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Byte Code
• When we execute a source code, Python compiles it into a byte code. Compilation is a
translation step, and the byte code is a low-level platform-independent representation of
source code. Note that the Python byte code is not binary machine code (e.g.,
instructions for an Intel chip).
• Actually, Python translate each statement of the source code into byte code instructions
by decomposing them into individual steps. The byte code translation is performed to
speed execution.
• Byte code can be run much more quickly than the original source code statements. It
has .pyc extension and it will be written if it can write to our machine.
11
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Byte Code
• So, next time we run the same program, Python will load the .pyc file and skip the
compilation step unless it's been changed. Python automatically checks the timestamps
of source and byte code files to know when it must recompile. If we resave the source
code, byte code is automatically created again the next time the program is run
• If Python cannot write the byte code files to our machine, our program still works. The
byte code is generated in memory and simply discarded on program exit. But
because.pyc files speed startup time, we may want to make sure it has been written for
larger programs.
12
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Byte Code
In summary:
• When a Python executes a program, Python reads the .py into memory, and parses it in
order to get a bytecode, then goes on to execute.
• For each module that is imported by the program, Python first checks to see whether
there is a precompiled bytecode version, in a .pyo or .pyc, that has a timestamp which
corresponds to its .py file.
• Python uses the bytecode version if any. Otherwise, it parses the module's .py file,
saves it into a .pyc file, and uses the bytecode it just created.
• Byte code files are also one way of shipping Python codes. Python will still run a
program if all it can find are .pyc files, even if the original .py source files are not there.
13
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Virtual Machine (PVM)
• Once our program has been compiled into byte code, it is shipped off for execution to
Python Virtual Machine (PVM).
• Actually, the PVM is just a big loop that iterates through our byte code instruction, one
by one, to carry out their operations.
• The PVM is the runtime engine of Python. It's always present as part of the Python
system.
• It's the component that truly runs our scripts. Technically it's just the last step of what is
called the Python interpreter.
14
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Performance
• Unlike other compiled languages, Python code runs immediately after it is written. Byte
code is a Python-specific representation.
• Since byte code is not a binary machine instructions, it requires more work than CPU
instructions.
• On the other hand, unlike other interpreters, there is still an internal compile step -
Python does not need to reanalyse and reparse each source statement repeatedly.
• So, Python code runs at speeds somewhere between those of a traditional compiled
language and a classic interpreted language.
15
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Development
• This even includes operation such as the creation of functions and classes and the
linking of modules. Such events occur before execution in more static languages, but in
Python, they happen as programs execute.
• This structure is why Python lends itself to customization. Because Python code can be
changed on the fly, users can modify the Python parts of a system onsite without
needing to compile the entire system's code.
16
© Nex-G Exuberant Solutions Pvt. Ltd.
Python Interpreter
>>> 3 + 7
10
>>> 3 < 15
True
>>> 'print me'
'print me'
>>> print('print me’)
print me
>>>
17
© Nex-G Exuberant Solutions Pvt. Ltd.
for Using Interactive Prompt
There are a few tips for the beginners when using interactive prompt.
Be sure to terminate multiline compound statements like for loop at the interactive prompt
with a blank line. We must press the Enter key twice to terminate the whole multiline
statement and then make it run. For example
>>> if True: We needed an indent before
... print("hello") print, so…
File "<stdin>", line 2
print("hello") >>> if True:
^ ... print("hello")
IndentationError: expected an indented block ...
>>> hello
>>>
# Python script
import sys #loading a library module
import math
print(sys.platform)
print(math.sqrt(1001))
x = "hi"
print(x*3)
Once saved, we can ask Python to run it at the system shell prompt:
Python script1.py
• Python Shell – running 'python' from the Command Line opens this interactive shell
• PyCharm
• For the exercises, we'll use VS Code/Sublime or Jupyter Notebook, but you can try them
all and pick a favorite
22
© Nex-G Exuberant Solutions Pvt. Ltd.
IDLE – Development Environment
23
© Nex-G Exuberant Solutions Pvt. Ltd.
Example Python
Hello World
print(“hello world”)
Prints hello world to standard out
24
© Nex-G Exuberant Solutions Pvt. Ltd.
First Python Program
Invoking the interpreter without passing a script file as a parameter brings up the
following prompt:
25
© Nex-G Exuberant Solutions Pvt. Ltd.
First Python Program
Let us write a simple Python program in a script. Python files have extension .py. Type the
following source code in a test.py file:
print("Hello, Python!“)
We assume that you have Python interpreter set in PATH variable. Now, try to run this
program as follows −
$ python test.py
This produces the following result:
26
© Nex-G Exuberant Solutions Pvt. Ltd.