You are on page 1of 35

An Introduction to Python for Scientists

Hands-On Tutorials

Ahmed Attia

Statistical and Applied Mathematical Science Institute (SAMSI)


19 TW Alexander Dr, Durham, NC 27703
attia@ {samsi.info || vt.edu}
Department of Mathematics, North Carolina State University,
amttia2@ncsu.edu

SAMSI
Undergraduate Workshop on Optimization; February 27, 2017

Hands-On Tutorials [1/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Ahmed Attia [attia@samsi.info, attia@vt.edu]
NCSU, Math Department & SAMSI

Sercan Yildiz [syildiz@samsi.info ] Peter Diao[pdiao@samsi.info]


UNC Chapel Hill & SAMSI Duke & SAMSI

Missy Gaddy [mrgaddy@ncsu.edu ] Jeff LaComb [ jml109@duke.edu ]


NC State University Duke University, Math Department
Hands-On Tutorials [2/35]
February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Outline
Getting Started with Python
Basic numerical, string, and boolean operations
Iterables, and basic data structures
Branching, and Looping
Functions
Python Modules and Packages
Scientific Packages: Numpy & Scipy
File I/O
Plotting: matplotlib
OOP: Python Classes
Additional topics

Hands-On Tutorials [3/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Getting Started with Python [4/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

I According to (Python.org ),
“Python is an interpreted, object-oriented, high-level programming language
with dynamic semantics.
Its high-level built in data structures, combined with dynamic typing and
dynamic binding, make it very attractive for Rapid Application Development,
as well as for use as a scripting or glue language to connect existing
components together.”

I Why Python:
1. https://www.continuum.io/why-python
2. https://www.python.org/doc/essays/comparisons/
3. http://www.bestprogramminglanguagefor.me/why-learn-python
4. https://www.codefellows.org/blog/
5-reasons-why-python-is-powerful-enough-for-google/

Hands-On Tutorials Getting Started with Python [5/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Learn Python: where should I start?
I Textbooks (for Scientists):
1. Python for Scientists
2. A Primer on Scientific Programming with Python
3. Python Scripting for Computational Science

I Phone/Tablet Apps: Learn Python


- Android: https://play.google.com/store/apps/details?id=com.sololearn.python&hl=en
- IPhone/IPad: https://itunes.apple.com/us/app/learn- python- pro/id953972812?mt=8
I Python online Documentation: https://docs.python.org/2/

I Practice, Practice, Practice,


Hands-On Tutorials Getting Started with Python [6/35]
February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Basic Concepts

I Python script files have extension (.py), and compiled code files
(executables) have extension (.pyc).

I Python scripts can be written using any plain text editor.

I Avoid using SOLID Tabs.


Make sure Tabs are converted to SPACES (check your editor settings).

I INDENTATION is Extremely important. (For now, make sure all script line
start at the beginning of the line.)

I Python is CASE-SENSITIVE.

Hands-On Tutorials Getting Started with Python [7/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Running Python code
I IPython : is an interactive command shell originally developed for Python,
that supports tab completion, and history amongst other features. This
makes working with Python very similar to working with MATLAB .

I To run a Python script:


1. from a terminal using the command: $ python [script-name.py]
2. from IPython: $ run [script-name.py]

I Jupyter Notebook : (Formerly known as IPython Notebook) is an


interactive rich computational environment, built around IPython, best suited
for learning, teaching, and designing tutorials.

I Task:
1. → http://people.cs.vt.edu/~attia/Tutorials/samsi_02_27/
2. write a “Hello World” program in:
I a python script and run it,
II in IPython shell,
III in a Jupyter notebook.

Hands-On Tutorials Getting Started with Python [8/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Terminal output; print
I Now, given the last task, you know that to print anything to the screen, we
will need to use the “print” function (or statement)

I Python’s ‘‘print’’ function can be used to print a “string representation”


of an ‘‘object’’ , e.g. string, number, etc.:
print(<object>) or print <object>

I The most obvious object to be printed to a screen is a String; one of the


main datatypes in Python is str . (Next)

I IF you like to complicate things, check:


print(str([object])), vs. print([object]. str ())

I Task: open the script lesson0 terminal output.py

Hands-On Tutorials Getting Started with Python [9/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Data types, and variable assignment I
I Python supports a variety of data types, including:
1. Strings: str
2. Numerical types: int, long, float, complex
3. Boolean: bool
I To check/view the data type of an object, you can use:
type([object name])

I Python, by default, allocates memory dynamically, and does not require


data type declaration; unlike Fortran, C, etc.
I Variables are created with dynamic binding; assignment is done using
assignment operator ‘‘=’’.
variable-name = variable value
I Multiple assignment (with dynamic binding) is allowed , e.g. x = y = 3.

Hands-On Tutorials Getting Started with Python [10/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Data types, and variable assignment II
I Variable naming rules:
1. Variables names must start with a letter or an underscore,
2. the remainder of the name may consist of letters, numbers, or underscores,
3. variable name is case sensitive.
4. variable name (identifier) length can be one character or more, and should be
meaningful.

I Examples:
Correct Incorrect
x, i, 1x, ,
x , radius, radius, radius, radi us, radi − us
par 1, peremeter 0 2 , etc. x$, s #, etc.

I Variable names starting with underscore will play vital role when we learn
how to import data from Python modules.

I Task: open the script lesson1 datatypes.py

Hands-On Tutorials Getting Started with Python [11/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Basic numerical, string, and boolean operations [12/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Numerical arithmetics

I Python can apply one or more numerical arithmetic operations, to integers,


real numbers, or both.
1. Addition, subtraction: [+, −]
2. Multiplication, division, quotient, remainder(modulo): [∗, /, //, %]
3. Unitary: [+, −]
4. Exponentiation: [**]
Priority (precedence) is from “lowest” to “highest”. Override, and arrange
operations using round braces ( ).

I Question: What is the result of the following operation:


−3 ∗ ∗2 + 24//12%4/3 − 5 ∗ (2/3) ∗ (1/4.)
Try by hand, then use IPython, and check your answer!

Hands-On Tutorials Basic numerical, string, and boolean operations [13/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
String operations
I You should know by now, that strings are defined, using single, double, or
triple-double quotes (“docstring”).

I Strings are saved into variables of “str” data type. Try the following:
x = ’this is a string’
print(type(x))
I Simple string operations:
I You can use addition ’+’ to add two strings,
I you can replicate a string using ’*’ (multiply a string by an INTEGER)

I A string can be formatted easily using escape characters, e.g. \n, \r, \t.
You can also use %[datatype] to insert something in a string,

I Task:
What is the result of the following expression?
" Lesson%d \n is \t pointless%s " % (0, ’: ’*2)

Hands-On Tutorials Basic numerical, string, and boolean operations [14/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Boolean (logical) operations
I Logical operations include:

>, < , ==, >=, <=, !=


in, not in, is, is not
not, and, or

I You can also use double expressions such as: a < x < b.
I The priority of all these is less than arithmetics, and is ranked from highest to
lowest as follows:
1. >, < , ==, >=, <=, !=, in, not in, is, is not
2. not
3. and
4. or

I For full operator precedence, check:


https://docs.python.org/2/reference/expressions.html

Hands-On Tutorials Basic numerical, string, and boolean operations [15/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Basic numerical, string, and boolean operations

Task:
1. Open the script lesson2 numbers arithmetics.py
2. Open the script lesson2 string operations.py
3. Open the script lesson2 boolean expressions.py

Hands-On Tutorials Basic numerical, string, and boolean operations [16/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Iterables, and basic data structures [17/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Iterables, and basic data structures

I An “iterable” is a Pythonic term, refers to any object capable of returning its


members one at a time.
I Basic examples include:
1. lists,
2. tuples,
3. dictionaries,
4. sets,
5. strings.

I Task:
To learn how to create, access, or modify these objects:
1. Open the script lesson3 lists.py
2. Open the script lesson3 tuples.py
3. Open the script lesson3 dictionaries.py

Hands-On Tutorials Iterables, and basic data structures [18/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Branching, and Looping [19/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Branching, and Looping: if, for, while

I We will learn this lesson directly from the code examples.

I Task:
1. Open the script lesson4 if statement.py
2. Open the script lesson4 loops.py

Hands-On Tutorials Branching, and Looping [20/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Functions [21/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Functions

I We will learn this lesson directly from the code examples.

I Task:
Open the script lesson5 functions.py

Hands-On Tutorials Functions [22/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Python Modules and Packages [23/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Using Python Modules
I The components of a Python module, or a (.py) file can be imported into
the current working space, using the ’import’ statement. Examples
(discussed further in class)
1. import [module name]
2. import module1, module2
3. import module1 as alias
4. from [module name] import [variable, function, class, submodule] <as alias>
I Once a module is imported, its contents can be accessed using the [.]
operator
I The following statement imports everything(ish) from a module:
from [module name] import *
I Popular packages for scientific computing: math, numpy, scipy, matplotlib,
statsmodel, re, etc.
I Check this: https://pythontips.com/2013/07/30/
20-python-libraries-you-cant-live-without/

I Task: Open the script lesson6 modules.py

Hands-On Tutorials Python Modules and Packages [24/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Scientific Packages: Numpy & Scipy [25/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Numpy

I According to (http://www.numpy.org/):
NumPy is the fundamental package for scientific computing with Python.

I Numpy contains, among other things:


1. a powerful N-dimensional array object
2. sophisticated “broadcasting ” functions
3. tools for integrating C/C++ and Fortran code
4. useful linear algebra, Fourier transform, and random number capabilities

I Task: Open the script lesson7 numpy intro.py

Hands-On Tutorials Scientific Packages: Numpy & Scipy [26/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Scipy: sparsity and more!

I Once you become familiar with Numpy, you will realize that some linear
algebra and statistical tools are not provided.
I Amongst others, Numpy does not provide sparse linear algebra functionalities.
This is the time to start learning/using Scipy
I According to (https://docs.scipy.org/doc/scipy/reference/),
SciPy (pronounced Sigh Pie) open-source software for mathematics, science,
and engineering.

I Task: Open the script lesson8 scipy intro.py

Hands-On Tutorials Scientific Packages: Numpy & Scipy [27/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials File I/O [28/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Files, input, and output operations
I You can open a file, to read ’r’, write ’w’, modify ’r+’, or append ’a’ .
file id = open([file name or path], mode=’r’)
<Do stuff with the file>
file id.method()
<Do stuff with the file>
file id.close()
I A better approach:
with open([file name or path]) as file id:
Do stuff with the file>
file id.method()
<Do stuff with the file>

I Matlab (.mat) files:


import scipy.io as file io
file contents = file io.loadmat([matlab mat file name or
path])
I Task: Open the script lesson9 fileIO.py

Hands-On Tutorials File I/O [29/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Plotting: matplotlib [30/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Plotting: matplotlib

I According to (http://matplotlib.org/):
Matplotlib is a Python 2D plotting library which produces publication quality
figures in a variety of hardcopy formats and interactive environments across
platforms. Matplotlib can be used in Python scripts, the Python and
IPython shell, the jupyter notebook, web application servers, and four
graphical user interface toolkits.

I Task: Open the script lesson10 matplotlib.py

Hands-On Tutorials Plotting: matplotlib [31/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials OOP: Python Classes [32/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
OOP: Python Classes

I Task:
Open the script lesson11 classes intro.py

Hands-On Tutorials OOP: Python Classes [33/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Getting Started with Python

Basic numerical, string, and boolean operations

Iterables, and basic data structures

Branching, and Looping

Functions

Python Modules and Packages

Scientific Packages: Numpy & Scipy

File I/O

Plotting: matplotlib

OOP: Python Classes

Additional topics

Hands-On Tutorials Additional topics [34/35]


February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)
Additional topics
I Optimization:
1. scipy.optimize: (https://docs.scipy.org/doc/scipy-0.18.1/reference/
tutorial/optimize.html)
2. PyOpt: (http://www.pyopt.org/)
3. CVXOPT: (http://cvxopt.org/)
I Neural Networks, and Deep Learning:
1. Neural Networks in Scikit-learn: (http://scikit-learn.org/stable/
modules/neural_networks_supervised.html)
2. Deep Learning:
2.1 http://deeplearning.net/tutorial/
2.2 https:
//elitedatascience.com/keras-tutorial-deep-learning-in-python
2.3 http://caffe.berkeleyvision.org/
I Wrappers:
1. SWIG: (http://www.swig.org/)
2. F2PY: (https://docs.scipy.org/doc/numpy-dev/f2py/)
I Statistical Analysis:
(http://www.scipy-lectures.org/packages/statistics/index.html)
I Inverse Problems and Data Assimilation: (Next Tutorial)
Hands-On Tutorials Additional topics [35/35]
February 27, 2017: An Introduction to Scientific Python, Ahmed Attia. (http://samsi.info)

You might also like