Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
54 views

Lesson 2

This document provides an introduction to using Python for computational science. It describes Python as a dynamic, object-oriented programming language that is well-suited for scientific computing due to its powerful syntax and large number of open-source modules. The document then discusses why Python is popular for scientific uses in terms of versatility, simplicity, portability, and availability of packages. It also outlines intended learning outcomes and provides details on installing and configuring Python.

Uploaded by

therealsirjhun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Lesson 2

This document provides an introduction to using Python for computational science. It describes Python as a dynamic, object-oriented programming language that is well-suited for scientific computing due to its powerful syntax and large number of open-source modules. The document then discusses why Python is popular for scientific uses in terms of versatility, simplicity, portability, and availability of packages. It also outlines intended learning outcomes and provides details on installing and configuring Python.

Uploaded by

therealsirjhun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Learners’ Module in Computational Science

PYTHON FOR COMPUTATIONAL


SCIENCE
2
INTRODUCTION
In this lesson, you will be introduced to Python for computational science.
Python is a dynamic object-oriented programming language. Because of its powerful
and flexible syntax, Python excels as a platform for scientific computing.
Versatility, simplicity of use, high portability and the large number of open-source
modules and packages make it very popular for scientific use.

You will also learn how to install and configure the Python language. Finally, we
will be testing the installed Python.

INTENDED LEARNING OUTCOMES

Upon the completion of this lesson, you should be able to:

1. describe Python as the programming language for


computational science;

2. explain the reasons why Python can be used for


computational science; and

3. perform the installation, configuration and testing of


the Python programming language.

LESSON PROPER

Lesson 2: Python for Computational Science 13 | Page


Learners’ Module in Computational Science

What is Python?

Python was written by Guido van Rossum as a hobby project, starting in December 1989.
It was fully functional on Feb. 20, 1991, and was made generally available—as open-
source—in 1992. Rossum chose the name Python because of his appreciation of a BBC
television comedy series called Monty Python’s Flying Circus. The creators of that show
toyed with other titles including Owl Stretching Time and The Toad Elevating Moment.
Had they settled on one of those, who knows what Python might have been called.

Python was designed with simplicity in mind. Rossum wanted the code to be English-like
and easy to read, write, and understand. The syntax is simple and approachable for
beginners, and seasoned programmers can come to Python from other languages
without any struggle.

Interpreters and Compilers

Python is an interpreted language. You write your program source code into files, and the
Python interpreter reads the files and executes the commands you’ve entered. Compiled
languages such as C require additional steps between writing the program and running
the program.

A piece of software called a compiler reads the program files and generates a binary file
containing the low-level instructions that the computer understands. In other words, it
takes what you’ve written—the C source code—and creates a copy of it that has been
translated into the computer’s native tongue. With a compiled program, it’s the output
from the compiler—the binary file—that is executed.

The advantage of a compiled program is that they execute faster than an interpreted
program because the code doesn’t need to be interpreted every time it is run. But the
advantage of interpreted languages is the absence of the compiling step. And
compilation can be time-consuming. With Python, you can change a few lines of code
and instantly run your program.

Python is easiest to work with in an integrated development environment (IDE), and


there are many IDEs for Python—Idle was one of the first. Idle lets you type your code,
type Ctrl+S to save it, then press F5 to run it. Your program runs in a Python shell. You
can type any Python command in the shell, and have it executed for you immediately.
This gives you the classic read, evaluate, print loop, or REPL, which aids development.

Why Python for Computational Science?

Lesson 2: Python for Computational Science 14 | Page


Learners’ Module in Computational Science

Python is a dynamic object-oriented programming language. Because of its powerful


and flexible syntax, Python excels as a platform for scientific computing. Versatility,
simplicity of use, high portability and the large number of open-source modules and
packages make it very popular for scientific use.
Although pure Python is generally slower compared to traditional language (C or
Fortran), there are various techniques and libraries that allow you to obtain performance
absolutely comparable to those of the most common compiled languages, assuring a
good balance between computational performance and time investment.

Six Reasons to Use Python for Computational Science


1. Python has built-in support for scientific computing. Most Python distributions
include the SciPy ecosystem (open source) which includes SciPy (a SciPy library),
a numerical computation package called NumPy, and multiple independent
toolkits, each known as a Scikits. Matplotlib, a 2D plotting library for
visualization, is also part of the SciPy ecosystem. Matplotlib is much like MATLAB
in terms of its functionality and usage but is open source.

2. Python has bridges to MATLAB or Octave. Python can install the MATLAB Engine
API so that Python programs can call MATLAB as a computational engine.
MATLAB programs can also call Python functions with some limitations. Some
distributions of Python may use Python packages such as Pymatbridge, which
can support both MATLAB and Octave and allows the use of MATLAB within
Jupyter Notebooks.

3. Python is a highly extensible language. Developers have used Python wrappers


for C/C++ programs for many years. Python uses the C Foreign Function
Interface for Python (cFFI) to interact directly with C code. Tools such as SWIG
make this easy to accomplish. Developers can also call Fortran subroutines from
Python by using the Fortran to Python interface generator package, F2Py, which
is now a part of NumPy.

4. Python has very good input/output (I/O) options. Until recently, Fortran I/O has
traditionally been records based. In contrast, Python has long supported
multiple options for I/O and many additional packages to support all types of
I/O formats, including real-time and streaming formats.

5. Python has strong support for task automation. Python’s built-in scripting
features and multiple packages have strong support for task automation.
Automation of repetitive tasks and performing data logging are easy and takes
little effort.

Lesson 2: Python for Computational Science 15 | Page


Learners’ Module in Computational Science

6. Python can use a web front end. Python packages such as Django and Flask
make it possible to develop and use Python as an API with a web front end. This
functionality is particularly useful when using a cloud-based infrastructure as a
platform to access high-performance computing (HPC) back ends.
Returning to the crux of the matter, let’s focus on how these basic strengths become
more pragmatic reasons to adopt Python for scientific computing. The biggest driver for
using Python in scientific computing is the evolution of problem-solving approaches.

New Scientific Problem-Solving Paradigms


Over the years, the scientific problem-solving toolkit has evolved. Early approaches
relied on mathematical modeling and simulation to understand the universe around us.
As our understanding of the universe has improved, the body of knowledge has grown
so much that models have become extremely complex and difficult to simulate easily.
Scientists have developed new approaches to solve scientific problems at scale. Let’s
look at a couple of them.
 The data-driven approach to scientific research: To better handle the data
deluge, scientists have begun to shift to data-driven scientific research
approaches. Many scientific problems use statistical or Bayesian analysis tools to
solve specific classes of problems. Although traditional usage was based on R
programming, Python has become the de facto programming language for data
scientists.

 The discovery-based approach to scientific research: Cognitive approaches


such as machine learning and deep learning to look for patterns and discover
correlations provide scientists another discovery-based approach to scientific
research. Discovery is an approach much favored in life sciences research.
Machine learning and deep learning frameworks are mostly Python-based.

 The quantum computing approach to scientific research: The advent of


quantum computing has opened entirely new problem-solving approaches to
problems that were previously not possible to solve, even with supercomputers.
Most quantum computer implementations use a form of Assembly language for
programming. Python makes an ideal high-level wrapper and API for these
implementations that allow communication between a scientific research
application and the quantum computing system back-end.

Installing Python
Installing and using Python is very simple. The installation procedure involves just three
steps:

Lesson 2: Python for Computational Science 16 | Page


Learners’ Module in Computational Science

1. Download the binaries


2. Run the Executable installer
3. Add Python to PATH environmental variables
To install Python, you need to download the official Python executable installer. Next,
you need to run this installer and complete the installation steps. Finally, you can
configure the PATH variable to use python from the command line.

“You can choose the version of Python you wish to install. It is


recommended to install the latest version of Python, which is 3.7.3
at the time of writing this material.”

Step 1: Download the Python Installer binaries


1. Open the Python website (https://www.python.org/downloads/windows) in
your web browser. Navigate to the Downloads tab for Windows.
2. Choose the latest Python 3 release. In our example, we choose the latest Python
3.7.3 version.
3. Click on the link to download Windows x86 executable installer if you are
using a 32-bit installer. In case your Windows installation is a 64-bit system, then
download Windows x86-64 executable installer.

Step 2: Run the Executable Installer

1. Once the installer is


downloaded, run
the Python installer.
2. Check the Install
launcher for all
users check box.
Further, you may

Lesson 2: Python for Computational Science 17 | Page


Learners’ Module in Computational Science

check the Add Python 3.7 to path check box to include the interpreter in the
execution path.

3. Select Customize installation.

Choose the optional features by checking the following check boxes:

4. Documentation
5. pip
6. tcl/tk and IDLE (to install
tkinter and IDLE)
7. Python test suite (to
install the standard library
test suite of Python)
8. Install the global launcher
for `.py` files. This makes
it easier to start Python
9. Install for all users.

Click Next.

10. This takes you


to Advanced
Options available while
installing Python. Here,
select the Install for all
users and Add Python
to environment
variables check boxes.

Optionally, you can


select the Associate
files with Python, Create shortcuts for installed applications and other
advanced options. Make note of the python installation directory displayed in this
step. You would need it for the next step.

After selecting the


Advanced options,
click Install to start
installation.

Lesson 2: Python for Computational Science 18 | Page


Learners’ Module in Computational Science

11. Once the installation is over, you will see a Python Setup Successful window.

Step 3: Add Python to environmental variables

The last (optional) step in the installation process is to add Python Path to the System
Environment variables. This step is done to access Python through the command line. In
case you have added Python to environment variables while setting the Advanced
options during the installation procedure, you can avoid this step. Else, this step is done
manually as follows.

In the Start menu, search for “advanced system settings”. Select “View advanced
system settings”. In the “System Properties” window, click on the “Advanced” tab and
then click on the “Environment Variables” button.

Locate the Python installation directory on your system. If you followed the steps exactly
as above, python will be installed in below locations:

 C:\Program Files (x86)\Python37-32: for 32-bit installation


 C:\Program Files\Python37-32: for 64-bit installation

The folder name may be different from “Python37-32” if you installed a different version.
Look for a folder whose name starts with Python.

Append the following entries to PATH variable as shown below:

Lesson 2: Python for Computational Science 19 | Page


Learners’ Module in Computational Science

Step 4: Verify the Python Installation

You have now successfully installed Python 3.7.3 on Windows 10. You can verify if the
Python installation is successful either through the command line or through the IDLE
app that gets installed along with the installation.

Search for the command prompt and type “python”. You can see that Python 3.7.3 is
successfully installed.

Lesson 2: Python for Computational Science 20 | Page


Learners’ Module in Computational Science

An alternate way to reach python is to search for “Python” in the start menu and clicking
on IDLE (Python 3.7 64-bit). You can start coding in Python using the Integrated
Development Environment (IDLE).

Hurray! You are ready to start developing Python applications in your Windows 10
system.

SUMMARY

Lesson 2: Python for Computational Science 21 | Page


Learners’ Module in Computational Science

 Python is an interpreted language.

 A complier is a piece of software that reads the program files and generates a binary
file containing the low-level instructions that the computer understands.

 Python has built-in support for scientific computing.


 Python has bridges to MATLAB or Octave.
 Python is a highly extensible language.
 Python has very good input/output (I/O) options.
 Python has strong support for task automation
 Python can use a web front end packages such as Django and Flask that make it
possible to develop and use Python as an API with a web front end

Lesson 2: Python for Computational Science 22 | Page


Learners’ Module in Computational Science

SELF-LEARNING ASSESSMENT

Let us see how much you have learned from this lesson.
Answer the following questions.

TEST 1. Essay: Based on your understanding, discuss the following items using your own
words. Each item is 10 points.

1. Explain three reasons why Python is suitable for use in computational science.

Lesson 2: Python for Computational Science 23 | Page


Learners’ Module in Computational Science

ENRICHMENT ACTIVITY

1. In the laboratory, you will perform the following:

 Install and configure the Python programming language.


 Test the installed application.

2. Write the steps in doing it.

Lesson 2: Python for Computational Science 24 | Page

You might also like