An ultra-quick primer to Python
th
Lecture 1c, February 24 , 2021
Simon Scheidegger
Outline of this mini-course in Python
I. Motivation – why Python.
II. First steps in Python.
III. Nonlinear equations and optimization.
VI. Pointers to tutorials and literature.
I. Motivation
Computational science in general
- Computational science: a rapidly growing multidisciplinary
field that uses advanced computing capabilities to understand
and solve complex problems.
- It is an area of science which spans many disciplines (comp.
finance, comp. econ, comp. physics, comp. biology).
→At its core it involves the development of models and
simulations to understand complex systems.
→computational science aims to make the complexity of those
systems tractable.
I. Motivation
Basics: von Neumann Architecture
https://computing.llnl.gov/tutorials/parallel_comp
Virtually all computers have followed this basic design.
Comprised of four main components:
→ Memory, Control Unit, Arithmetic Logic Unit, Input/Output.
Read/write, random access memory is used to store both program
instructions and data:
→ Program instructions are coded data which tell the computer to do something.
→ Data is simply information to be used by the program.
Control unit:
→ fetches instructions/data from memory, decodes the instructions and
then sequentially coordinates operations to
accomplish the programmed task.
Arithmetic Unit:
→ performs basic arithmetic operations.
Input/Output
→ interface to the human operator.
I. Motivation
Hardware and how to use it
- The hardware can execute only simple low-level instructions (load,...)
- Complex applications (abstract models and their representation as high-
level code) are separated from simple instructions by multiple layers.
- System Software
Operating System:
- I/O operation.
- Storage & memory.
- Protection among concurrent applications.
Compilers:
- Translate from high level language
such as C++ to hardware instructions.
I. Motivation
From a programming language to hardware
http://cse-lab.ethz.ch/index.php/teaching/42-teaching/classes/577-hpcsei
A computer is a “stupid” device, only understands “on” and
“off”
The symbols for these states are 0 and 1 (binary)
First programmers communicated in 0 and 1.
Later programs where developed to translate from symbolic
notation to binary. The first was called “assembly”.
> add A, B (programmer writes in assembly language)
> 1000110010100000 (assembly translates to machine language)
Advanced programming languages are better than
“assembly” for
→ programmer thinks in a more natural language
→ productivity of software development
→ portability
I. Motivation
“There are only two kinds of (programming)
languages: the ones people complain about
and the ones nobody uses.”
― Bjarne Stroustrup (designer of C++)
Lets complain about...
I. Motivation
What is Python?
- Python is a general purpose programming
language conceived in 1989 by Dutch
programmer Guido van Rossum.
- Python is free and open source, with
development coordinated through the Python
Software Foundation https://www.python.org/psf/).
- Python has experienced rapid adoption in the last
decade, and is now one of the most popular
programming languages.
I. Motivation
Common uses
Python is a general purpose language used in almost all application
domains:
- communications
- web development
- graphical user interfaces
- games, multimedia, data processing, security, etc.
- Machine Learning, Artificial Intelligence
Used extensively by Internet service and high tech companies such as
- Google
- Dropbox
- Reddit
- YouTube
- Walt Disney Animation,...
→ Often used to teach computer science and programming
→ For reasons we will discuss, Python is particularly popular within the
scientific community
I. Motivation
The 2020 Top programming languages
- Python is one of the most popular programming languages worldwide.
- Python is a major tool for scientific computing, accounting for a rapidly
rising share of scientific work around the globe.
https://spectrum.ieee.org/computing/software/the-2020-top-programming-languages
I. Motivation
Relative popularity
- The following chart, produced using Stack Overflow Trends, shows one measure of the
relative popularity of Python.
- The figure indicates not only that Python is widely used but also that adoption of Python
has accelerated significantly since 2012.
- We suspect this is driven at least in part by uptake in the scientific domain, particularly in
rapidly growing fields like data science.
I. Motivation
Matlab vs. Python – a replacement?
I. Motivation
Statistics from supercomputer users
Source: CSCS annual report
Even high-end users start to move there
I. Motivation
Some features
- Python is a high level language suitable for rapid
development.
- It has a relatively small core language supported by many
libraries.
- Other features:
A multi-paradigm language, in that multiple programming
styles are supported (procedural, object-oriented, functional,...)
- Interpreted rather than compiled.
I. Motivation
Syntax and design
One nice feature of Python is its elegant syntax — we’ll see many examples
later on.
Elegant code might sound superfluous but in fact it’s highly beneficial
because it makes the syntax easy to read and easy to remember.
Remembering how to read from files, sort dictionaries and other such routine
tasks means that you don’t need to break your flow in order to hunt down
correct syntax.
Closely related to elegant syntax is elegant design.
Features like iterators, generators, list comprehensions, etc. make Python
highly expressive, allowing you to get more done with less code.
I. Motivation
Get Python
You can download and install
Python directly from
https://www.python.org
Since we’re going to use several
libraries for numerical
computation (numpy), data analysis (pandas),
machine learning (scikit-learn), and
visualization (matplotlib),
it is easier to install Anaconda,
which bundles all things required
https://www.continuum.io/downloads
I. Motivation
https://docs.python.org/
I. Motivation
Install Python
https://www.python.org/downloads/
→ Find the installation you need (Linux, MacOS, Windows)
II. First steps in Python
→ action required
II. First steps
List of examples
2.0. Setting up your environment.
2.1. Python basics.
2.2. Loops and Lists.
2.3. Functions and Branching.
2.4. Reading/writing Data.
II. First steps
How to install & setup Python
https://www.python.org/downloads/
→ Today, use some on-line cloud version:
https://www.pythonanywhere.com/try-ipython/
https://repl.it/languages/python
https://trinket.io/python Probably try those
II. First steps
Python setup
II. First steps
Another helpful on-line tool
The Online Python Tutor is a free tool to visualize the
Online execution Python Tutor of - Visualize small program Python execution
programs step-by-step.
Feel free to use it for the course exercises and your own code:
http://pythontutor.com/visualize.html
II. First steps
2.1. Python basics
II. First steps
Python basics (II)
II. First steps
Basic types
No type declaration needed – Python does that for you on the fly
II. First steps
Type conversions
→ check the type of a variable by typing >>>type(a)
II. First steps
String literals
II. First steps
String literals II
II. First steps
Operators
II. First steps
Your first exercise to compute
II. First steps
Operators II
>>>‘U’ + ‘NIL
‘UNIL?
II. First steps
Operators III
II. First steps
Assignments
II. First steps
Assignments II
II. First steps
The “is” operator
II. First steps
The help function
II. First steps
Functions
Attention – rounding towards ZERO
II. First steps
How to execute source code
example0_hello.py
→ Store a file hello.py with this content:
print('Hello, world!')
→ Execute it with
$ python hello.py
II. First steps
2.2. Loops and lists
example3_while.py
The while loop:
→ is used to repeat a set of statements as long as a condition is true.
Example:
The task is to generate the rows of the table of C and F values. The C value starts at
−20 and is incremented by 5 as long as C ≤ 40. For each C value we compute the
corresponding F value and write out the two temperatures. In addition, we also add a
line of hyphens above and below the table. We postpone to nicely format the C and F
columns of numbers and perform for simplicity a plain print C, F statement
inside the loop.
$python example3_while.py
------------------
-20 -4.0
-15 5.0
( )
-10 14.0
-5 23.0
0 32.0
5 41.0
( ) 10 50.0
15 59.0
( )
20 68.0
25 77.0
30 86.0
35 95.0
40 104.0
------------------
II. First steps
Loop Implementation of a Sum
example4_sum.py
( )
II. First steps
Lists
Up to now a variable has typically contained a single number.
Sometimes numbers are naturally grouped together.
A Python list can be used to represent such a group of numbers in a program.
With a variable that refers to the list, we can work with the whole group at once, but we
can also access individual elements of the group.
The figure illustrates the difference between an int object and a list object.
In general, a list may contain a sequence of arbitrary objects in a given order. Python
has great functionality for examining and manipulating such sequences
of objects.
Fig. From H.-P. Langtangen
II. First steps
Basic operations in Lists → type
To create a list with the numbers from the first column in our table,
we just put all the numbers inside square brackets and separate the
numbers by commas:
Two lists can be added:
New elements can be inserted anywhere in the list:
With del C[i] we can remove an element with index i from the list C.
II. First steps
How to represent Vectors
A Python program may use a list or tuple to
represent a vector:
II. First steps
Basic operations (II) – “for loop”
- When data are collected in a list, we often want to perform the same
operations on each element in the list.
- We then need to walk through all list elements. Computer languages
have a special construct for doing this conveniently.
- This construct in Python and many other languages called a “for loop”.
example6_forloop.py
example5_forloop.py
( )
( )
( )
II. First steps
2.3. Functions and Branching
II. First steps
How to define new functions (II)
II. First steps
How to define new functions (III)
II. First steps
How to define new functions (IV)
example7_greet.py
II. First steps
How to define new functions (V)
Try this:
>>>help(greet)
II. First steps
Modules
II. First steps
Modules II
II. First steps
Conditionals
II. First steps
Branching – example “hat” function
example9_branching.py
Branching in general
II. First steps
Exercises
II. First steps
2.4. Input Data.
()
In this program, C is input data in the sense that C must be known
before the program can perform the calculation of F.
The results produced by the program, here F, constitute the output data.
Input data can be hard-coded in the program as we do above.
We explicitly set variables to specific values (C = 21).
This programming style may be suitable for small programs.
In general, however, it is considered good practice to let a user of the
program provide input data when the program is running.
→ There is then no need to modify the program itself when a new set of
input data is to be explored.
II. First steps
Reading Keyboard Input
We may ask the user a question C=? and wait for the user to enter
a number. The program can then read this number and store it in a
variable C.
example10_read.py
( )
The raw_input function always returns the user input as a
string object. That is, the variable C above refers to a string object.
If we want to compute with this C, we must convert the string to a
floating-point number: C = float(C).
II. First steps
Reading from Command line
Inside the program we can fetch the text “number” as sys.argv[1].
The sys module has a list argv containing all the command-line arguments to the program,
i.e., all the “words” appearing after the program name when we run the program.
Here there is only one argument and it is stored with index 1. The first element in the
sys.argv list, sys.argv[0], is always the name of the program.
A command-line argument is treated as a text, so sys.argv[1] refers to a string object.
Since we interpret the command-line argument as a number and want to compute with it, it
is necessary to explicitly convert the string to a float object.
import sys
print "This is the name of the script: ", sys.argv[0]
print "please write the degrees celcius outside:"
example11_readsys.py C = sys.argv[1]
F= 9*float(C)/5 + 32
Run as: print "it is ", F , " degrees F"
$python example11_readsys.py 2
print ("Number of arguments: ", len(sys.argv))
print ("The arguments are: " , str(sys.argv))
II. First steps
Reading from a file
example12_readfile.py
→ We have a text file containing numbers: say data.txt
→ We want to get first column in a1, second in a2 and so on.
→ Note: there are plenty of option on how to read from files → RTFM
a1 = []
a2 = []
a3 = []
a4 = []
1234
5678 with open('data.txt') as f:
for line in f:
9 10 11 12 data = line.split()
13 14 15 16 a1.append(int(data[0]))
a2.append(int(data[1]))
a3.append(int(data[2]))
a4.append(int(data[3]))
print (a1, a2, a3, a4)
f.close()
II. First steps
Other things to learn about Python
II. First steps
Do not re-invent the wheel – NumPy
II. First steps
Do not re-invent the wheel – SciPy
II. First steps
Pandas
II. First steps
Writing to a file – e.g. with numpy
example12_readfile.py
import numpy as np
mat=np.matrix([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print(mat)
np.savetxt('matrix.txt',mat,fmt='%.2f')
II. First steps
Curve Plotting
- Visualizing a function f (x) is done by drawing the curve y = f (x) in
an x-y coordinate system.
- When we use a computer to do this task, we say that we plot the curve.
- Technically, we plot a curve by drawing straight lines between n points
on the curve.
- The more points we use, the smoother the curve appears.
- Suppose we want to plot the function f (x) for a ≤ x ≤ b.
II. First steps
Matplotlib:
publication quality plotting library
II. First steps
Matplotlib: a basic example
example13_plot.py
Let us plot the curve s for values between 0 and 2. First we generate equally spaced
coordinates for t. Then we compute the corresponding s values at these points, before
we call the plot(t,s) command to make the curve plot.
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()
To include the plot in electronic documents, we need a hardcopy of the figure in
PostScript, PNG, or another image format. The savefig function saves the plot to files in
various image formats.
II. First steps
Want more?
217,612
III. Nonlinear equations & optimization.
- Our course heavily relies on solving large
systems of nonlinear equations or (un-) constraint
optimization problems.
→ In Python, you have plenty of options, e.g.:
- SciPy.org
- PyOpt.org
- IPOPT
(https://www.coin-or.org/Ipopt; https://github.com/xuy/pyipopt)
III. Opt. & Nonlinear Eqs.
Constrained optimization with SciPy
The minimize function also provides an interface to several constrained
minimization algorithm.
As an example, the Sequential Least SQuares Programming optimization
algorithm (SLSQP) will be considered here.
This algorithm allows to deal with constrained minimization problems of
the form:
III. Opt. & Nonlinear Eqs.
Constrained optimization – example
As an example, let us consider the problem of optimizing the function:
subject to an equality and an inequality constraints defined as:
III. Opt. & Nonlinear Eqs.
Example for Optimization
example1_opt.py
III. Opt. & Nonlinear Eqs.
Root finding (nonlinear equations)
Finding a root of a set of non-linear equations can be achieve using the
root function.
Several methods are available, amongst which hybr (the default) and lm
which respectively use the hybrid method of Powell and the Levenberg-
Marquardt method from MINPACK.
Consider a set of non-linear equations
III. Opt. & Nonlinear Eqs.
Example for Nonlinear Equations
IV. Pointers to tutorials and literature
There is an unlimited amount of tutorials and source codes on the web available. Here is an incomplete list:
The Python tutorial:
http://docs.python.org/tutorial/
The Zen of Python in 3 days:
http://pixelmonkey.org/pub/python-training/
Python for Java programmers:
http://python4java.necaiseweb.org/Main/TableOfContents
Books:
A Primer on Scientific Programming with Python (Hans Petter Langtangen)
Youtube:
https://forums.kjdelectronics.com/blog/?page_id=48 (links inthere)
Questions?
1. Advice – RTFM
https://en.wikipedia.org/wiki/RTFM
2. Advice – http://lmgtfy.com/
http://lmgtfy.com/?q=introduction+to+python