Quickstart Python
Quickstart Python
Quickstart
Python
An Introduction to Programming
for STEM Students
essentials
Springer essentials
Springer essentials provide up-to-date knowledge in a concentrated form. They
aim to deliver the essence of what counts as “state-of-the-art” in the cur-
rent academic discussion or in practice. With their quick, uncomplicated and
comprehensible information, essentials provide:
Available in electronic and printed format, the books present expert knowledge
from Springer specialist authors in a compact form. They are particularly suitable
for use as eBooks on tablet PCs, eBook readers and smartphones. Springer essen-
tials form modules of knowledge from the areas economics, social sciences and
humanities, technology and natural sciences, as well as from medicine, psycho-
logy and health professions, written by renowned Springer-authors across many
disciplines.
Quickstart Python
An Introduction to Programming for
STEM Students
Christoph Schäfer
Institut für Astronomie und Astrophysik
Eberhard Karls Universität Tübingen
Tübingen, Germany
This book is a translation of the original German edition „Schnellstart Python“ by Schäfer, Chri-
stoph, published by Springer Fachmedien Wiesbaden GmbH in 2019. The translation was done
with the help of artificial intelligence (machine translation by the service [Link]). A subse-
quent human revision was done primarily in terms of content, so that the book will read stylistically
differently from a conventional translation. Springer Nature works continuously to further the
development of tools for the production of books and on the related technologies to support the
authors.
© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole
or part of the material is concerned, specifically the rights of reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt
from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, expressed or implied, with respect to the material contained
herein or for any errors or omissions that may have been made. The publisher remains neutral with
regard to jurisdictional claims in published maps and institutional affiliations.
With this essential we would like to introduce you to the great world of
programming with Python and give you a quick start to develop your own scripts.
• You will learn the basic ideas and principles of the programming language
Python.
• You will develop your own Python programs.
• You will understand Python scripts written by other programmers, adapt them
to your needs and integrate them into your code.
• You will get to know interesting extensions of Python especially for natural
scientists and data scientists.
• You can create meaningful diagrams and graphics with Matplotlib.
v
Contents
vii
viii Contents
In recent years, the programming language Python has established itself alongside
MATLAB and R as the standard for scientific workplaces in research and
development.
The great popularity of Python is based on its easy extensibility: It is very easy
to use modules from other developers in your own scripts and programs. Especi-
ally, the modules NumPy, SciPy, and Matplotlib offer scientists and engineers a
perfect development environment for scientific and technical computing, for app-
lications in physics, chemistry, biology, and computer science. Python is also used
in the latest applications in the modern fields of research Big Data Science and
Machine Learning.
Originally Python was developed in the early 1990s by Guido van Rossum
in the first version during his Christmas holiday. The pure teaching language
ABC served as a model for him. His main goals were clarity and easy com-
prehensibility. The first full version was released in 1994 and was designed by
van Rossum to be easily extensible by modules, and the language itself could be
easily embedded in other languages. Since Python itself uses a clear syntax and
can be programmed with simple structures, the language is particularly suitable
for beginners and is comparatively easy to learn.
• Support for Python 2.7 has expired on January 1, 2020. The active development
of Python 2.7 ended in mid-2010. All new features are only included in version
3 and only occasionally have been backported to 2.7.
• Python 3 has better Unicode support: All text strings are Unicode by default.
• Important modules only support version 3.
Other new features that we will not go into detail include a different syntax for the
print function, the use of views and iterators instead of lists, and changes for the
integer division. The use of version 2 is only justified in individual cases. The most
common individual case is a program already existing in version 2 for which porting
is too costly.
Installation of Python
2
Very few users compile Python directly from the source files from the central web-
site of the Python project, but use precompiled packages, some of which already
contain the important modules NumPy, SciPy, and Matplotlib. The following
describes the installation for the most common operating systems.
2.1 Windows
For Windows operating systems, the Anaconda Suite offers a free Python suite
with an easy installation in the basic version with over 1400 packages. A graphical
installer can be obtained for versions 2 and 3 from the website of Anaconda Inc.
The standard installation includes all modules relevant for scientific applications
and the development environment spyder.
2.2 Linux
In almost all Linux distributions the most important Python packages for versions
2 and 3 can be installed via the respective package manager of the distribu-
tion. Apart from that the Anaconda Suite for Linux is also available, but it is
recommended to use the distribution’s own packages. The call for installation on
Debian-based systems is
Any dependencies are resolved by the package manager and all packages are
installed accordingly.
2.3 macOS
Under the operating system macOS, there are also several possibilities for instal-
lation. On the one hand you can use the Anaconda Suite mentioned above, on the
other hand most Python packages are available through one of the better known
open source package managers fink, macports, homebrew. The installation
of the Python base package via homebrew is for example
With the help of Python’s own package manager pip (Pip Installs Packages), you
can then install additional Python packages
On Windows the interpreter is called with the call [Link], on Linux and
macOS with the command python if only version 3 is installed, otherwise pos-
sibly with python3. The interpreter reports directly to the terminal with the
version and waits for further input at the so-called Python prompt >>>
In practice, the pure interpreter is only used for short tests or as a calculator
replacement. As soon as a script is written for repeated use, it is recommended to
write and edit the program statements in a text editor and have them executed by
the interpreter. A text file [Link] with the content
The only difference between the [Link] script and the interactive call is the
additional print statement in the script. By default, return values of calls are
only printed in the interactive interpreter on standard output. Without calling the
print function we would not get any output of the results in the terminal.
Interactive working with Python has proven to be so efficient that an extension
called IPython (Interactive Python) has been developed in the SciPy community.
However, IPython is more than an extended command line interpreter and is more
like a small integrated development environment. IPython is not part of the stan-
dard Python installation and must be installed separately. Useful extensions to the
standard Python command-line interpreter are:
3.2 Development Environments 7
In combination with NumPy, SciPy, and Matplotlib, IPython is the perfect tool
for data processing and data visualization in the natural sciences.
We start with the typical first program for each programming language, the Hello-
World program. The following source code (for Linux or macOS) produces the
output Hello World on the standard output when called with the interpreter.
In the first line the Linux/Unix typical Shebang #! appears. This tells the opera-
ting system which interpreter to use to execute the contents of the script. If you
save the program as [Link] and make it executable using chmod +x [Link],
you can execute it directly without explicitly specifying the interpreter as requi-
red on Windows: On Linux/macOS ./[Link] vs. [Link] C:\[Link] on
Windows. The second line in the script tells the Python interpreter what character
set encoding (in this case utf-8) the characters are encoded in. This specification
is unnecessary in the case of utf-8, as this is the default character set. Finally,
in the fourth line of the script, the only statement the interpreter executes is the
output of Hello World to the standard output. Here, print is a Python func-
tion and Hello World is a string object (a string is a sequence of individual
characters) passed to the function.
Python is a so-called structured programming language. This means that the
program code can be divided into blocks. In Python, indentations are used to
identify the individual blocks. This means that Python does not need keywords
or parentheses to define blocks like other languages. It also means that the code
automatically becomes clearer and easier to read. However, it is important when
writing code that the programmer does not mix spaces and tab characters. Best
practice is: One indentation depth corresponds to 4 spaces and tabs are converted
to spaces. It is recommended to use this standard also in your own scripts.
Let us look at a more complicated program, which already contains almost all
the features we will discuss in the following chapters. Create a text file named
plot_sinc.py with the following content
Now call the script with a command-line parameter such as sinc, which is
used as the name of the output file. If we take sinc as an example, you will
find the newly created file [Link] in the directory whose contents show a
plot of the sinc function. This script is already much more complex than our
Hello-World script. The first two lines are identical, in line 4 the sys module is
loaded with the keyword import. This module provides important information
about system constants, functions, and methods of the Python interpreter. Next,
the keywords try and except are used to load the two modules for the Matplot-
lib and NumPy. If these modules cannot be loaded, the output “Cannot find
necessary modules. Exiting.” is an output on the standard error output
and the script ends with the return value 1.
Line 14 contains a conditional statement with the keyword if. If the length
of the list [Link] is not exactly 2, a hint is printed and the program ends
with a return value of 2. The [Link] list contains as elements the command
4 The Basic Structure of a Python Program 11
line arguments used to call the Python interpreter, the first element containing
the name of the script itself. The function len() returns the number of ele-
ments of the argument and we use them to check if the script was called with
the required parameter. On line 19, we can be sure that the [Link] list
has two entries and set the output file name to the second entry of the list.
In line 21 we call the function [Link] of the
module matplotlib with the string object ‘dark_background’ as an
argument. In lines 23 and 24, functions from the NumPy module are called
and two NumPy arrays are initialized. The function [Link] (<min>,
<max>, <step>) returns a NumPy array with the values from <min> to
<max> with the step size <step>. The NumPy function [Link]() cal-
culates the sine value of the argument, in this case for all elements of the
NumPy array x and returns a NumPy array with these values. Line 26 initia-
lizes our plot with the function [Link]() of the
matplotlib, which returns the two objects [Link] and
[Link]. We use the latter object in the following lines to
plot the values in the two NumPy arrays x and y with the plot function of the
object. With the parameter c = ‘r’ we set the line color to red. The axis labeling
is done using the two functions set_xlabel and accordingly set_ylabel.
The last line ensures that the plot is saved in the Portable Data Format (PDF) in
file [Link]. As you can see, even with a few lines of Python code, expressive
diagrams can be created.
Data Types, Variables, Lists, Strings,
Dictionaries, and Operators 5
The programming language Python distinguishes between the different data types
numbers, lists, tuples, strings, dictionaries, and sets. As the main feature and
biggest difference to other programming languages such as C or C++, type assi-
gnment in Python takes place during the runtime of the program and variable
types do not have to be declared. This is called dynamic typing, where the type
of the variable is set automatically when a value is assigned. In a strict sense,
there are no classic variable types in Python. Instead, variables are objects of a
particular type.
generates an object of the class int with the name i and assigns it the value 42.
In general, one speaks of an integer variable i of the data type int, but since
in Python everything is an object, this way of speaking is wrong. The type()
function can be used to find out the type of a variable
The assignment (with the assignment operator =) in line 1 creates a new object
of the class int with the name i. If there is a floating-point number on the right
side of the assignment operator, which is characterized by the decimal character,
as in line 4, an object of the class float is created automatically. While the
maximum displayable integer in Python version 2 was still limited by 64 bits, it
is no longer limited in Python version 3
Besides the type function, the id() function is important for handling
objects and variables. Each object receives its own integer, which is guaranteed
to be unique and constant for the runtime of the program. The id() function
returns the value of this integer.
In line 4 the object j of class int is created and referenced to the variable i.
Both objects point to the same memory area and therefore have the same id. They
are one and the same object. By the assignment in line 7 a new object with the
name j of class int is created, which also gets its own id. To check whether
two objects are identical, the is operator is available. To determine whether two
objects have the same value, the == operator must be used. The following example
with the Python interpreter illustrates the difference between the two operators
5.1 Numerical Data Types … 15
First two int objects are created, which both have the same value 512. Com-
paring the two values with the == operator, therefore, returns True. However,
since the two objects are not identical, that is, two different objects, each with its
own memory area, the test for equality of the objects with the operator is returns
False. In line 11 we now explicitly create a new reference i to the int object
j. The same location in memory is referenced and the two objects are thus identi-
cal, the is operator returns True. The properties of the two operators are given
in Table 5.1.
While integer variables in Python version 3 can have an unlimited length, the
value range for floating-point numbers is limited to 64 bits. The largest represen-
table number is approximately 1.8 × 10308 . All numbers greater than this value
are called inf
The smallest representable number greater than zero is 5 × 10−324 . All numbers
between 0 and this number are effectively considered 0.
Python also allows calculating with complex numbers. The data type complex
is used in the syntax (real part + j*imaginary part)
16 5 Data Types, Variables, Lists, Strings, …
Furthermore, Python provides the class bool, whose value range is given by
False or True.
The value ranges of the numerical data types are listed in Table 5.2.
By sequential data types, we mean types that contain several elements. These
include lists, tuples, and strings. NumPy extends this list by the extremely useful
datatype [Link], which we will discuss in detail later. The most versatile
sequential data type is the list: A list can contain elements of different data
types and can be changed after its creation (mutable). An element of a list can
itself be a list. The list is the most powerful sequential data type, and Python
allows many operations and methods on lists. The most important operators for
sequential data types are listed in Table 5.3. Often the advantages of lists are
not needed and more attention must be paid to speed or memory requirements.
5.2 Sequential Data Types 17
Table 5.3 The most important operations for sequential data types
Syntax Operation
a[i] Returns ith element of sequence a
a[i:j] Returns the range ith to (j-1)th element of sequence a
a[i:j:k] Returns the range ith to (j-1)th element
in steps of k from sequence a
a+b Concatenation of a and b, return is a new data sequence
a+=b Add b to a
n*a Creates new data sequence with n-fold content
e in a Checks if e is contained in a, return value True or False
e not in a Checks if e is not contained in a, return value True or False
In these cases, tuples are used instead of lists. In contrast to lists, the sequential
datatype tuple (tuple) is not changeable (immutable) after it has been created.
It is important to understand that although the tuple can no longer be changed, if
an element of a tuple is a list, the elements of this list can change again. In other
words, the elements of a tuple are references that can no longer be changed, but
if the reference points to a mutable object, then the object can change and so
can the element in the tuple. The following example illustrates this
The so-called slicing, with which you can address areas of sequential data
types, allows you to work with lists (and also tuples) quickly and conveniently.
First, we create a list of planets in our solar system as the sum of the two lists
inner_planets and outer_planets
18 5 Data Types, Variables, Lists, Strings, …
This generates a new list (with a new id). Alternatively, we can use a method of
the list to remove an element
The advantage of this version is that no new list is created, only one element is
removed from the list, which is much faster than creating a completely new list
with one element less. If we had chosen a tuple for our solar system instead of a
list, removing Pluto would not have been possible because the tuple object does
not provide a method for removing elements
The Python interpreter discreetly points out that our object tuple does not pro-
vide an attribute remove. We cannot simply change the name of the last planet
and set it to an empty value
5.2 Sequential Data Types 19
Unfortunately, in the last step we created a list within a list because we added the
list ngc3109_group to the list some_galaxies. If we only want to append
the elements of this list instead, we have to use the list-object function extend()
Since the object sg is identical to the object some_galaxies (check this with
the id() function), we have also changed the list some_galaxies with the
20 5 Data Types, Variables, Lists, Strings, …
remove() function. Only when we explicitly create a new list object sg, we can
change its elements without modifying some_galaxies. For this, we need a
so-called shallow copy, which we get with the copy() function.
Another special feature of copying objects is when these objects in turn refe-
rence other objects. Take a list, for example, whose elements are in turn
lists. We generate our solar system from the two lists inner_planets and
outer_planets
Now we create a new list with reference to this list by a shallow copy
Pluto was also removed from the old_solar_system list. The reason is that
the two list elements of solar_system are themselves lists and contain refe-
rences. When creating old_solar_system we copied these references and
not the values in the memory areas that are referenced. You can see this when
using the id() function
5.2 Sequential Data Types 21
This means that the list copied by shallow copy is a copy and not a reference,
but the elements of this list are still references and no copies. To actually get real
copies and no references in the list elements, we must perform a so-called deep
copy. Python provides the copy module for this.
Strings in Python are like tuples not changeable (immutable), single elements of
a string can be accessed with square brackets like lists, but slicing can also be
used
Strings can be easily joined together and even multiplied by integer values
22 5 Data Types, Variables, Lists, Strings, …
The string object class has some methods that make working with strings in
Python very convenient and we will illustrate them with the following examples
Especially with the module re for regular expressions Python offers a powerful
tool for working with strings.
Python allows the elegant creation of lists from existing lists by so-called list
comprehension
5.3 Dictionaries
Dictionaries consist of key-value pairs. The key can basically be any immutable
Python object, usually either a string or an integer value. The value associated
with the key can be any Python object. In the following example, the densities of
the individual planets are stored in a dictionary and floating point numbers and a
string is used as values. Curly braces {} are used for dictionaries.
5.4 Quantities 23
The individual elements can be accessed by the respective key or the sequence
number of the key
The dictionary object has methods for outputting all keys and values, or iterating
over keys and values.
You often want to create a dictionary from two lists, whose keys are elements of
one list and whose values are elements of the second list. This is done with the
function zip(), which can be used like a zipper to join the two lists into one
dictionary.
5.4 Quantities
For the sake of completeness, we want to mention Python sets set(). Similar
to the notion of sets in mathematics, a set can contain arbitrary elements, but
there is no sequence of elements. However, elements can only occur once in a
set. A set is defined like a dictionary by curly brackets or alternatively by the
function set(). Empty curly braces create an empty dict, not an empty set.
Python supports some mathematical set operations for sets
24 5 Data Types, Variables, Lists, Strings, …
Conditional Statements and Loops
6
We can use control statements like loops and conditional statements in Python to
control the flow of our scripts and programs. In Python, if-else branches, for
and while loops are available. Python does not have the switch statement or
do-while loop that you might know from other programming languages. But
you will not have to miss them.
Here the program code in the indented block in lines 2–4 is only executed if the
condition in the first line is true. If you want to check for more than one condition,
several conditions can be queried one after the other using the keyword elif.
The keyword else can be used to specify program code that is executed if none
of the other conditions in these if-elif statements apply.
Loops are control structures with which program code can be executed repeatedly.
A while loop repeats a statement block iteratively as long as the loop condition
is valid. It is defined as follows
The interpreter first checks whether the condition in line 1 is true. If this is true,
the code in lines 2–4 is executed one after the other. At the end of the loop, the
interpreter jumps back to line 1 and checks if the condition is still true and restarts
the execution of the program code in line 2 accordingly. If the condition is not
true, the interpreter jumps behind the end of the loop. If this condition is always
true, we speak of a so-called endless loop. The simplest example of an endless
loop is
With the keyword else, program code can be executed if the loop condition is
not true
Here the code in the indented block under else is only executed if the condition
is not (or no longer) fulfilled. Often it is necessary to end a loop before it has run
completely to the end again and checks the loop condition again. The keyword
break is used for this purpose.
If the condition in the first line is fulfilled, the program code programmcode1
is executed. The if query in the third line checks for a further condition
condition2. If this condition is met, the loop is aborted by the break state-
ment in line 4. The interpreter jumps out of the loop, the block programmcode3
6.3 Repetition … 27
and the else block are not executed anymore. If condition2 is not fulfil-
led during the loop pass, the programmcode3 block is executed and the loop
repeats itself as long as condition1 is true and condition2 is false. If
condition1 is false at the beginning of another loop pass, the else block is
executed with programmcode4. Therefore, the else block is only executed
if the break statement is never triggered.
The for loop in Python iterates over elements of any sequence such as a list or
string in the order of the elements in the sequence.
The indented program code is executed for each element planet in the list
inner_planets. As with the while loop, a break statement can also
terminate the for loop early.
The output of this program code now ends after the Earth since the for loop is
terminated prematurely.
28 6 Conditional Statements and Loops
Another important instruction is continue. With this statement, the loop jumps
to the next element in the sequence without executing the remaining code of the
block. The following program code
skips the output for the planet Earth and moves to the next element in the list.
The output changes to
Curious for many programmers is the potential else block of a for loop.
Here the code in the else block is executed when the list is empty (remem-
ber: an empty list is equivalent to the boolean value False). Since the condition
in the first line also returns False as soon as element was the last element
in the list, the else block is also executed after the loop has been success-
fully completed. Only a potential break statement prevents the execution of the
programmcode2 block. Since else statements in conjunction with loops are
unfamiliar to most programmers, you will rarely find them in practice.
Functions
7
Python has some built-in functions that are available in every Python program.
We have already learned about the id() and type() functions. Other functions
like str() are used for type conversion between different objects. The number of
built-in functions varies depending on the Python version and currently (version
3.7) is 69. Table 7.1 lists the most important ones.
Functions are declared with the keyword def followed by the unique function
name:
The function arguments are given in brackets. The function can optionally have
a return value, which is specified with the keyword return. Unlike other pro-
gramming languages such as C, a Python function can have multiple return values.
When programming functions, the scope of the variables must be taken into
account. Let us assume that a variable rho already exists outside the function
calculate_density
32 7 Functions
With the keyword global, you can also access variables from the global context.
However, if you try to write to a global variable without the keyword global, the
variable is created locally in the function. This allows us to set up the function
even without a return value
7.4 Iterators and Generators, Functional Programming 33
As you can see when you run it, you get the same result as with the two former
versions. However, the first version of our density calculation function should
clearly be preferred for programming. Here, the interface between the main pro-
gram and the function is clearly defined by function arguments and return values,
and variables from the global context are not inadvertently overwritten in the func-
tion. The function arguments make it easy for other programmers to see which
variables the function requires. This automatically creates a better overview and
clarifies the dependencies. In particular, global variables should only be used for
constant quantities such as natural constants or similar.
Iterators and generators allow functional programming with Python. Iterators are
objects over whose elements it is possible to iterate. In order to iterate over ele-
ments of an object, it must be a so-called iterable object that has implemented the
function next() and iter(). You already know the iterable objects lists and
tuples.
34 7 Functions
At the end of the iteration, the next function returns StopIteration, which
for example gives a for-loop the signal to end the iteration.
Generators are special functions that allow creating iterators in a simple way. A
function calculates one or more values and returns them. In contrast, a generator
does not return a value but creates an iterator that can return a chain of data. The
keyword equivalent to return in a generator is yield. The following script
implements a simple generator
If you call the script, you will get the following output
our example, the respective character strings for the planets are only generated
when the loop is processed. Alternatively, imagine here not a list of planets, but
a huge file that cannot be loaded completely into memory. A generator can then
be used to read and edit this file line by line.
Another example, to explain the connection, are the following two implemen-
tations to calculate the density of inner planets. Python allows you to create simple
generators similar to list abstraction by using normal brackets ()
When you run the script, you will get the following output
Another example for the support of functional programming in Python is the use
of the lambda operator. You can use the lambda operator to create anonymous,
nameless functions. Lambda functions are usually very short, one-line functions
with only one statement. To declare a lambda function, use the syntax lambda
followed by the function arguments, a colon, and a single statement. The result of
the statement is the return value of the function. We can thus also implement the
density calculation from the last section as follows using the Lambda function
With the values of the Earth for radius and mass we get the desired result
As a rule, Lambda functions are used for small, short functions that are only nee-
ded at one point in a program and only perform exactly one task. More complex
functions, which are called several times, should not be implemented in practice
by a Lambda function but by using def, function body, and, if necessary, a return
value per return.
How the magic function argument *args works is explained in the next section.
We specify this decorator function in the line before the function definition of
calculate_density with the special decorator syntax @
7.7 The Functional Arguments … 37
If we now call the function with a function argument that is neither an integer nor
a floating-point number, we get the message
With the help of the decorator we have thus modified the original function of
our function without touching the function itself. In addition, we can also use the
decorator function test_for_floating_argument for other functions.
The two somewhat magic function arguments *args and **kwargs allow you
to pass any number of function arguments to a function. The two names of the
arguments are arbitrary, and by convention *args and **kwargs are generally
used, only the use of the asterisk *, or ** is important. The argument *args
stands for any number of non-keyword arguments and **kwargs for keyword
arguments. They can be used if, when implementing the function, it is not clear
how many function arguments are passed to the function. The *args argument
is passed to the function as a tuple. In the example from the last section, we used
*args in the decorator function because it should be usable for all functions that
require integer and floating-point numbers as function arguments, regardless of the
number of arguments. Like the keyword for non-keyword arguments, **kwargs
can stand for any number of keyword pairs. If, for example, you want to imple-
ment a function that is to add any number of values, the magic function argument
*args is used
38 7 Functions
When you run this script, you will get the following output
Structuring with Modules
8
Modules in Python are basically functions and objects that can be included and
used in a program or script. For example, if we want the sine function value of a
variable, it is not necessary to implement a function that calculates this value but
we use the function sin implemented in the module math. In the same way, it
is common practice in larger programs to structure the individual functions into
modules and load them as needed. By default, Python has extensive help for all
kinds of needs in the form of modules. The analog of Python modules are libraries
in other programming languages like C and C++. Modules are usually included
at the beginning of the program code with the following syntax
This call imports the entire functionality of a module. Generally, the command
import followed by the name of the module is sufficient. You can also specify
the namespace using the keyword as. The content of the module is then accessed
using namespace_name. We import the math module to calculate the sine value
of π/4.
As an alternative to including them with the described syntax, modules can also
be included in the current namespace using the following, obsolete syntax
This has the tempting advantage that the respective elements of the module can
be accessed directly without having to specify the namespace
and until a few years ago, it was still common practice. I strongly advise against
importing objects without a namespace, since many objects now have functions
of the same name and you will run into insoluble difficulties when debugging,
since the unique assignment to the module is no longer obvious.
8.1 Structuring the Code with Own Modules 41
Interesting are modules to structure your own code: We can outsource func-
tions into modules and thus easily reuse them in other scripts and programs.
To do this, we have to write our function in a separate file and make sure
that Python finds it. By default, Python searches in the current directory, in
the directories of [Link] and in the directories stored in the environment
variable PYTHONPATH. In the following example we outsource the function
for calculating the density to our own module physics_tools: The file
physics_tools.py contains the following lines
We start the interactive Python interpreter in the same directory where this file is
located and import the module with the name pt. The interpreter automatically
generates a meaningful help text
In this section we look at the most popular modules of the Python standard library
and explain their functionality.
Module os
The os (operating system) module is the interface for calling operating system func-
tions. This module is indispensable for projects that are to be used on more than one
platform such as Windows and macOS. Access to files at the file system level is com-
pletely abstracted by this module and you can, for example, copy, delete, or change
file names without using the respective different operating system commands. This
module is certainly one of the most important for system engineers. Some of the
basic functions are listed in Table 8.2. As an illustrative example of the module’s
powerful functionality, let us look at the following script that is used to search for a
file. As an argument of the function the top directory is specified (/data) and the
script searches all subdirectories until the file with the name [Link]
is found. Finally, the script outputs the path to the found file. The script ends as soon
as a file with the name you are looking for is found. You can modify the script as
an exercise to find all files with that name.
8.2 Some Important Modules … 43
Module re
The re module (for regular expressions, usually abbreviated as regex) provides the
methods and functions in Python for working with regular expressions. A regular
expression is a term from theoretical computer science and in principle a string
representing several elements of a set described by certain syntactical rules. In
programming practice, regular expressions are mostly used to define certain string
patterns for searching or filtering. In connection with Big Data, regex are used to
filter data.
Module sys
The sys module provides functions and variables related to or allowing interaction
with the Python interpreter. The default use of the module is to access the command
line parameters using [Link]. Furthermore, information about system proper-
ties can be retrieved via the module: For example, all loaded modules are stored in
[Link], the path searched for modules is stored in [Link], and the
float_info object contains information about floating point numbers such as
the largest number that can be displayed and the machine accuracy
Extensions for Scientists: NumPy, SciPy,
Matplotlib, Pandas 9
In this chapter, we want to explain the most important modules for all scientists.
A detailed description, however, would go beyond the scope of this essential. We
look at some illustrative examples and the basic ideas of the individual modules.
The numpy module is the most helpful extension for scientists in Python. NumPy
was created in 2005 from the unification of two different Python modules for the
fast calculation of numerical data. The most important innovation is the intro-
duction of the data structure [Link]. NumPy functions are specially
optimized for this new data structure and enable Python scripts with runtimes
in the order of magnitude of equivalent C and Fortran programs. You can think of
a [Link] as a Python list with elements of the same type. Because the
interpreter knows the size of the elements, it can optimize better than compared
to the more generic Python lists. The functions implemented in numpy operate
on this new data structure and can perform vectorial operations. This leads to the
far-reaching reduction of the runtime.
NumPy also offers many functions that make working with arrays much easier.
One example is the function [Link]. It creates a NumPy array with
elements equidistantly distributed over a certain interval. The function [Link]
then calculates all function values of the elements in this NumPy array and not
only of one element like the comparable function [Link] from the math
module as in the following example
We have thus saved one for loop. The handling of multidimensional arrays is
the basis for working with NumPy. The main differences between a Python list
and a NumPy array are as follows
In the first case when using [Link], the interpreter must create a new
object and delete the old one. This is significantly slower than using a regular
list to which an element can be added.
The already mentioned functions linspace(start, stop, num=
50, endpoint= True, retstep= False, dtype= None) can be
used to create 1D [Link]. Besides there are also the functions
logspace(start, stop, num= 50, endpoint= True, base=
10.0, dtype= None) and arange([start,] stop[, step,],
dtype= None)
To access the individual elements, we use the same syntax as for lists and tuples
The last two lines are especially interesting: Both x[1][2] and x[1,2] return
the same element. However, the latter syntax should be preferred to the first one,
since the Python interpreter creates a temporary intermediate object x[1] in the
first variant, from which the third element is then returned. Basically the following
happens
which in turn requires more memory and computing time than referencing with
x[1,2].
9.1 Fast Numerical Calculations with Python: NumPy 49
To find more functions, NumPy’s own search function is helpful. With the func-
tion lookfor the NumPy help can be searched. If you want to perform a Fourier
transformation, you can search for it specifically
It is advisable for every NumPy beginner to work through the basic principles
and the tutorial on the central website for NumPy.
Python alone also knows the data type array, which is provided by the
module array. However, it is no longer used in practice. Usually, a NumPy
array is meant when talking about an array.
In the next section, we will look at the module SciPy, which extends NumPy
by many mathematical functions.
50 9 Extensions for Scientists: NumPy, SciPy, Matplotlib, Pandas
The scipy module contains modules, functions, and methods for many different
areas which are important in the processing of scientific data or the modelling
of scientific processes. This includes linear algebra, numerical integration, signal
and image processing, numerical solution of ordinary differential equations. Like
NumPy, SciPy offers enough content for extensive books. In the following exam-
ple, we use the curve_fit function from the module [Link] to
fit a curve to artificially generated data.
We first generate the function arguments x using the NumPy function
linspace and get a NumPy array of length 50 with values from 0 to 3, cal-
culate the function values f (x) and add some noise to these function values to
generate our test data for fitting. Then we use the curve_fit function from
the [Link] module to fit our function f with the two free parame-
ters omega and phi to this data. The module uses the least-squares method to
determine the fit parameters. Finally, we plot both our artificial data and the fitted
curve and save the output in the [Link] file. If you run this script, you will
get a file with a similar content to Fig. 9.1. It is recommended that every SciPy
beginner should work through the basic principles and the tutorial on the central
SciPy website.
We have already used the matplotlib module in the previous sections without
explaining it in detail. It allows us to create high-quality 2D diagrams and plots
with Python and to save them in appropriate formats such as the raster format png
(portable network graphics) or the vector format pdf (portable document format).
In the following, we will load and plot the data of the file [Link]. The
file contains the semi-major axes and orbital periods of the planets in our Solar
System, column by column, as floating-point numbers
We can use the NumPy function loadtxt to conveniently load this data and
plot it with the scatter function from the [Link] module.
You get the file [Link] with the contents analogous to Fig. 9.2. The
additional second, dashed curve, are the orbital times for the respective semi-
major axis calculated according to Kepler’s 3rd law (“The square of a planet’s
52 9 Extensions for Scientists: NumPy, SciPy, Matplotlib, Pandas
Semi-major axis in au
Fig. 9.2 The periods of the planets in the solar system as a function of their semi-major axes
as data points and Kepler’s 3rd law
orbital period is proportional to the cube of the length of the semi-major axis
of its orbit.”). The initializing call is fig, ax= [Link]() which
generates a figure and axes object that can be used to generate the graphics. In
our example, we use the so-called scatter plot, where x–y values are displayed
as points and plot as a x–y line plot. The type of plot, the size of the individual
data points, and the line thickness are changeable parameters. The Matplotlib can
be used to generate curves, histograms, scatter plots, contour plots, and even 3D
plots. For text inserts in the diagrams LATEX can be used. It is recommended that
every Matplotlib beginner works through the basic principles and the tutorial on
the central website of Matplotlib.
of 2D tables whose individual columns and rows can be processed like series
objects. Finally, a panel is a 3D table whose individual levels are dataframe
objects. The module offers special functions for working with time series data
and their evaluation, that is, sorting functions and statistical functions. As soon
as you come into contact with very large amounts of data, the use of this module
could become interesting. You can find more information on the website of the
pandas project.
What You Learned from this essential
• You know the basic ideas and principles of the programming language Python.
• You can develop your own Python programs and structure your program in
functions and modules.
• You can understand foreign Python scripts, adapt them to your needs and
integrate them into your program code.
• You know how to use NumPy for fast numerical calculations with Python and
can implement special functions for scientific applications with SciPy.
• You can use Matplotlib to create scientific diagrams and graphics.
There is a lot of Python literature worth reading. Besides the online sources of the
individual Python, NumPy, SciPy, Matplotlib, and pandas projects, the following
works are recommended.
1. Langtangen, Hans Petter. 2016. A Primer on Scientific Programming with
Python. Berlin, Heidelberg: Springer.
2. Nelli, Fabio. 2018. Python Data Analytics with Pandas, Numpy, and Matplotlib.
New York City: Apress.
3. Johansson, Robert. 2018. Numerical Python: Scientific computing and data
science applications with NumPy, SciPy and Matplotlib. New York City: Apress.
4. Newman, Mark. 2012. Computational physics. Scotts Valley: CreateSpace
Independent Publishing Platform.