0% found this document useful (0 votes)
3 views

Module 2

The document provides a comprehensive guide on setting up the Python environment across various platforms including Unix/Linux, Windows, and Mac. It details installation steps, how to start Python, and run scripts, as well as the functioning of the Python interpreter and bytecode. Additionally, it introduces various development environments and tools for Python programming, emphasizing the use of Anaconda and IDEs like VS Code and Jupyter Notebook.

Uploaded by

ahad siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 2

The document provides a comprehensive guide on setting up the Python environment across various platforms including Unix/Linux, Windows, and Mac. It details installation steps, how to start Python, and run scripts, as well as the functioning of the Python interpreter and bytecode. Additionally, it introduces various development environments and tools for Python programming, emphasizing the use of Anaconda and IDEs like VS Code and Jupyter Notebook.

Uploaded by

ahad siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Module 2- The Python Environment

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.

Python Installation- : Python distribution is available for a wide variety of platforms.


And is available by default in Linux Ubuntu and Mac OS X, and most Linux and Mac
distributions.
Here is a quick overview of installing Python on various platforms:

2
© Nex-G Exuberant Solutions Pvt. Ltd.
Downloading and installing

Unix & Linux Installation:


• Use the following link and follow the steps provided:
https://opensource.com/article/20/4/install-python-linux
• Ubuntu 20.04 LTS comes with Python 3.8 pre-installed and Ubuntu 21 comes with Python
3.9
• You can also install Anaconda and use the Python version 3.8 from that too
https://www.anaconda.com/products/individual

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

Starting Python: There are three different ways to start Python -


1) Interactive Interpreter:
You can start Python from Unix, DOS, or any other system that provides you a command-
line interpreter or shell window.
Enter python the command line.

Start coding right away in the interactive interpreter.


python3 # Unix/Linux
or
python # Unix/Linux
or
C:>python # Windows
Note: Check python version by using the commands “python --version” or “python3 --
version”.
6
© Nex-G Exuberant Solutions Pvt. Ltd.
(2) Script from the Command-line:

A Python script can be executed at command line by invoking the interpreter on your
application, as in the following:

python script.py # Unix/Linux

or

python3 script.py # Unix/Linux

or

C:>python script.py # Windows

Note: Be sure the file permission mode allows execution.

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.

• Unix: IDLE is the very first Unix IDE for 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.

I recommend using the following text editors:


 Visual Studio Code (Windows) or just Code (Linux)
 Sublime Text
Visual Studio Code is a very modular code editor and has a lot of useful plugins.

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:

• The Python code we write must always be run by the Interpreter.


To enable it, we must install a Python interpreter on our machine.

• When the Python package is installed on our machine, it generates number of


components:

– Interpreter

– Library

These come preinstalled with Anaconda.

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)

Output is Hello World

• 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).

• The PVM is not a separate program. It need not be installed by itself.

• 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

• There is no distinction between the development and execution environments. In other


words, the compiler is always right there at runtime and is part of the system that runs
the program. All we have in Python is runtime. There is no initial compile-time phase at
all and everything happens as the program is running.

• 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

• Python is an interpreted language

• The interpreter provides an interactive environment to play with the language

• Results of expressions are printed on the screen

>>> 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.

Python commands only.


We can only type Python code, not system commands. Though there are ways to run
system commands (e.g., with os.system), but they are not as direct as typing the command
itself.
>>> import os
>>> os.system
<built-in function system>
>>>
print statements are required in files
We must use print to see our output when we write a file. Thanks to automatic echo feature,
we do not have to use the print to see our result.

© Nex-G Exuberant Solutions Pvt. Ltd.


Don't indent at the interactive prompt.:
Any blank space to the left of our code is considered as nested statement. A leading space
generates an error message.

Compound (multiline) statement :

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
>>>

© Nex-G Exuberant Solutions Pvt. Ltd.


A Script File

Here is a script file named script1.py:

# 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

© Nex-G Exuberant Solutions Pvt. Ltd.


Here is a brief description of the script:
• Imports a Python module - to fetch the name of the platform and to get sqrt() from math
functions.
• Runs three print function calls.
• Uses a variable x, created when it's assigned, to hold a string object.
• The sys.platform is just a string that identifies the computer. It is in a standard Python
module, sys, which we must import to load.
• The name of the module could be just stript1 without .py suffix. But files of code we want
to import into should end with a .py. Because we may want to import the script file later,
it's recommended to use .py suffix.

© Nex-G Exuberant Solutions Pvt. Ltd.


Python Interfaces

• IDLE – a cross-platform Python development environment

• PythonWin – a Windows only interface to Python

• Python Shell – running 'python' from the Command Line opens this interactive shell

• PyCharm

• Spyder - A data science focused IDE

• VS Code - All in one Editor

• 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

IDLE helps you program in Python by:


 color-coding your program code
 debugging
 auto-indent
 interactive shell

23
© Nex-G Exuberant Solutions Pvt. Ltd.
Example Python

Hello World

print(“hello world”)
Prints hello world to standard out

Open terminal and try it out yourself


Follow along using terminal


24
© Nex-G Exuberant Solutions Pvt. Ltd.
First Python Program

Interactive Mode Programming :

 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

Script Mode Programming :

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.

You might also like