0% found this document useful (0 votes)
5 views61 pages

Quickstart Python

The document is a quickstart guide to Python programming specifically designed for STEM students, introducing them to the basics of the language and its applications in scientific fields. It covers installation, execution, and fundamental programming concepts, as well as extensions like NumPy and Matplotlib for data science. The book is a translation of a German edition and aims to provide concise, state-of-the-art knowledge in programming for academic and practical use.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views61 pages

Quickstart Python

The document is a quickstart guide to Python programming specifically designed for STEM students, introducing them to the basics of the language and its applications in scientific fields. It covers installation, execution, and fundamental programming concepts, as well as extensions like NumPy and Matplotlib for data science. The book is a translation of a German edition and aims to provide concise, state-of-the-art knowledge in programming for academic and practical use.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Christoph Schäfer

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:

• an introduction to a current issue within your field of expertis


• an introduction to a new topic of interest
• an insight, in order to be able to join in the discussion on a particular topic

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.

More information about this subseries at [Link]


16761
Christoph Schäfer

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

ISSN 2197-6708 ISSN 2197-6716 (electronic)


essentials
ISSN 2731-3107 ISSN 2731-3115 (electronic)
Springer essentials
ISBN 978-3-658-33551-9 ISBN 978-3-658-33552-6 (eBook)
[Link]

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.

Responsible Editor: Lisa Edelhaeuser


This Springer imprint is published by the registered company Springer Fachmedien Wiesbaden
GmbH part of Springer Nature.
The registered company address is: Abraham-Lincoln-Str. 46, 65189 Wiesbaden, Germany
What You Can Find in This essential

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

1 Overview of the Programming Language Python . . . . . . . . . . . . . . . . . . 1


2 Installation of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Execution of a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Interactive Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Development Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 The Basic Structure of a Python Program . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Data Types, Variables, Lists, Strings, Dictionaries, and
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1 Numerical Data Types int and float, bool
and complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.2 Sequential Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.4 Quantities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6 Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6.1 Conditional Statement with if-else . . . . . . . . . . . . . . . . . . . . . . . . 25
6.2 Repetition with while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6.3 Repetition with for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.1 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.2 Declaration of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

vii
viii Contents

7.3 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31


7.4 Iterators and Generators, Functional Programming . . . . . . . . . . . . . 33
7.5 Anonymous Functions with the Lambda Operator . . . . . . . . . . . . . . 36
7.6 Functions Can be Decorated: Decorators . . . . . . . . . . . . . . . . . . . . . . 36
7.7 The Functional Arguments *args and **kwargs . . . . . . . . . . . . 37
8 Structuring with Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.1 Structuring the Code with Own Modules . . . . . . . . . . . . . . . . . . . . . . 41
8.2 Some Important Modules and What to Do with Them: Math
os re sys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
9 Extensions for Scientists: NumPy, SciPy, Matplotlib, Pandas . . . . . . . 45
9.1 Fast Numerical Calculations with Python: NumPy . . . . . . . . . . . . . . 45
9.2 For Scientists: SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
9.3 Creating Diagrams and Graphics with Matplotlib . . . . . . . . . . . . . . . 51
9.4 Big Data Science with Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Overview of the Programming
Language Python 1

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.

Python Versions 2 and 3


Python was recently developed in two different major versions that are incompatible
with each other. It is very likely that you will come across older Python scripts of
version 2 as soon as your work is built on older, existing scripts. In general, the
number of projects with Python 2 still predominates at present, a situation which
will, however, have reversed by 2021.
In this Springer essential we focus mainly on Python version 3, but if you are
presented with an older Python script in your scientific career, your knowledge of

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 1


C. Schäfer, Quickstart Python, essentials,
[Link]
2 1 Overview of the Programming Language Python

version 3 will be sufficient for you to understand it and to modify it according to


your needs.
For new Python projects, use only the new version 3; all modules of interest for
the natural sciences are now available for version 3, while support for older versions
is discontinued. A particularly important example is the Matplotlib module, which
in its latest version is only available for Python 3. If you need to convince someone
to use the new version 3 instead of the outdated version 2.7, argue with the following
points.

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

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 3


C. Schäfer, Quickstart Python, essentials,
[Link]
4 2 Installation of Python

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

The command pip list can be used to display installed packages.


Execution of a Python Program
3

In order to execute a program on a computer, the program code of a higher pro-


gramming language, such as Python in our case, must either be executed directly
by an interpreter, or it must first be translated into machine language by a com-
piler. Machine language consists of instructions that are executed directly by the
processor. The difference between an interpreter and a compiler is that the compi-
ler creates a new file from the source files, while the interpreter reads the source
files and executes them directly at runtime. There are some intermediate stages
between compiler and interpreter which we will not go into detail. Basically, the
Python interpreter also generates a so-called byte code with the typical extension
.pyc while the program is executed. This brings considerable speed advantages
when using modules, because modules already used do not have to be reinter-
preted if they are already available in bytecode format. The Python interpreter,
therefore, first checks whether the source file has changed and only then generates
new byte code if necessary.
A Python program can be started in different ways. The Python interpreter is
called directly on the command line to work interactively. In general, you will
either write a Python script with your favorite text editor to execute it afterward,
or you can use a development environment like spyder. In the following we
will look at the different possibilities.

3.1 Interactive Python

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

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 5


C. Schäfer, Quickstart Python, essentials,
[Link]
6 3 Execution of a Python Program

The interactive interpreter can also be used as a practical calculator on the


command line

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

produces the following output when called by the interpreter on Linux

For Windows the interpreter must be called accordingly

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

• IPython offers automatic tab completion of variables, functions, classes,


modules.
• IPython supports the common modules for programming graphical user
interfaces (GUIs).
• The output is in color and more detailed than the standard interpreter.
• Some operating system commands (ls, cp, …) are available on the IPython
command line.

In combination with NumPy, SciPy, and Matplotlib, IPython is the perfect tool
for data processing and data visualization in the natural sciences.

3.2 Development Environments

For larger projects and scripts it is recommended to use an integrated development


environment (IDE). There are a variety of different IDEs and the final choice
depends heavily on personal preferences.
The cross-platform development environment Spyder (Scientific PYthon Deve-
lopment EnviRonment), written entirely in Python, provides both beginners and
advanced programmers with all the tools needed for successful development: An
integrated editor with colored syntax highlighting and auto-completion, a variable
explorer, a Python or, if applicable, IPython interface, and a debugger.
The popular development environment Eclipse from the Java community can
also be used for Python programming with the PyDev extension. This IDE is
recommended for Java developers.
The commercial development environment PyCharm from JetBrains offers
everything a modern development environment needs and a little bit more:
PyCharm allows the installation of Python modules and plugins directly from
the graphical interface. In addition, there is a graphical interface to all current
version control systems like CVS, git, mercurial, and subversion or even the pos-
sibility to directly upload to Github. A helpful feature for scientific applications is
the direct display and integration of graphics and plots. Besides the commercial
version, the free community version is also available, but unfortunately, it offers
fewer features. For academic users, that is, students and lecturers, the commercial
version can be used free of charge.
As a meanwhile adequate alternative to an IDE, many scientific users use Jupy-
ter Notebook or JupyterLab. This is an open-source web application that allows
to create and share code, equations, visualization, and text modules. For example,
JupyterLab allows the direct export of a document to the typesetting system used
in the natural sciences LATEX.
The Basic Structure of a Python Program
4

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 9


C. Schäfer, Quickstart Python, essentials,
[Link]
10 4 The Basic Structure of a Python Program

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.

5.1 Numerical Data Types int and float, bool


and complex

In the Python interpreter the following statement

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

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 13


C. Schäfer, Quickstart Python, essentials,
[Link]
14 5 Data Types, Variables, Lists, Strings, …

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

Table 5.1 The operators for checking identity and equality


Operator Function
is Checks for equality of the objects,
Objects reference the same memory area
== Checks for equality of the values of the objects,
the values at the respective location of the storage area are equal

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

Table 5.2 Numerical data


Object Accuracy and value range
types and their value range
int Value range not limited
float 64 bit double-precision, according to IEEE 754
standard
bool true and false
complex Complex numbers with the real and imaginary
part

Furthermore, Python provides the class bool, whose value range is given by
False or True.

Especially for conditional statements it is important to know what is True or


False in Python. The following expressions are interpreted as false in Python:
The value False, the special value None, an empty string, an empty list, an
empty dictionary, an empty tuple, and explicit numerical null values such as 0,
0.0. In contrast, everything that is not False is True in Python, especially the
non-empty string “0” as opposed to the numerical value 0

The value ranges of the numerical data types are listed in Table 5.2.

5.2 Sequential Data Types

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

We can invert the list or just output every other planet

Since we obviously made a mistake and Pluto is no longer a planet, we create a


new list without the last element

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

because the elements of a tuple are immutable.


Apart from remove, list objects offer many useful methods that make work
easier. We will illustrate this in the following examples. To add an element to a
list, append() can be used

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

Other useful methods are insert() to insert an element at a specific position,


clear() to remove all elements from a list, count(e) to count the number of
occurrences of element value e in the list, index(e[,start[, end]]) to
find the index of the first occurrence of an element in a list, where the optional
arguments start and end limit the search indexes.
A particularly important list object function is the copy() function. Let
us consider the following program code, which creates a reference to a list of
galaxies

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.

An alternative notation for a shallow copy is also the following assignment

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

Now what happens to the contents of the old_solar_system list when we


remove Pluto from solar_system?

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.

In practice, the phenomenon of forgetting a shallow copy and only creating


references on assignments is much more common than problems for which a
deep copy is needed. However, when working with lists, it is recommended to
always consider whether you want to change values or references.
Another sequential data type besides tuple and list is the string. The string is
a special list whose elements are individual characters. Strings are enclosed in
Python using single and double quotation marks. If the string is longer than one
line, three double quotation marks must be used

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

This is especially useful with numeric values

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.

To delete entries, use the keyword del

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.

6.1 Conditional Statement with if-else

An if-else branch is used in Python to execute code only under certain


conditions that occur. An if statement looks like this

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 25


C. Schäfer, Quickstart Python, essentials,
[Link]
26 6 Conditional Statements and Loops

6.2 Repetition with while Loop

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.

6.3 Repetition with for Loop

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.

For each element the indented program code programmcode1 is executed.


If we want to execute a certain program code for each planet in our solar system,
we can do so with the for loop

The program code produces the following output

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

A function in Python is a kind of routine or procedure that performs a certain task.


A function may need function arguments, which are passed to the function when
it is called. In addition, a function can also have return values. For example, the
return value of the sin function from the math module is the sine of the function
argument with which the function is called.

7.1 Built-in Functions

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.

7.2 Declaration of Functions

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 29


C. Schäfer, Quickstart Python, essentials,
[Link]
30 7 Functions

Table 7.1 A list of the most important functions already installed


Function Argument Return value
abs() Integer, float Absolute amount of the argument
bin() Integer Converts the argument into a binary
string with prefix ‘0b’
dict() Iterable object Converts the argument to a dictionary
float() Number or character string Converts the argument into a
floating-point number object
id() Object Returns the integer value identity of the
object
int() Number or character string Converts the argument to an integer
object
input() String Prints the string of the argument on
standard output and reads a line from
standard input that is converted to a
string, removing the line feed character.
Finally, returns the string
len() Sequence object Number of elements in the object
max() Sequence object or two numbers Returns the largest of the arguments
min() Sequence object or two numbers Returns the smallest of the arguments
open() File Opens the function argument as a file
object
print() Object(s) Prints out any number of values
set() Iterable object Converts the argument to a set object
tuple() Iterable object Converts the argument to a tuple
object
type() Object Returns the object type of the function
argument
zip() Iterable object Returns an iterable object of tuples

In the following program listing, we use the function calculate_density to


calculate the density of a spherical object from its radius and its mass. We want
to use it to calculate the density of the Earth.
7.3 Global and Local Variables 31

The function calculate_density expects the two arguments radius and


mass and returns an object of the class float containing the value of the calcu-
lated density. With the print function we print the density on the terminal.
The variable volume declared within the function is only available within the
function and not in the global context. The function name is a reference to the
function and we can therefore use several function names for a single function. If
we append the following lines to the script above

we get two outputs of the density.

7.3 Global and Local Variables

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

The return value of the function calculate_density creates a new object


rho and replaces the density of the moon with the density of the Earth. Interes-
tingly, all variables of the global scope can be accessed read-only in functions.
This means that we could have written our density calculation function as follows

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.

7.4 Iterators and Generators, Functional Programming

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 generator inner_planets allows iteration using the for loop. It


remembers the current status and returns the new value when called again. The
loop automatically terminates as soon as the StopIteration signal occurs.
This somewhat academic example only serves to illustrate the basic function of a
generator. In practice, generators are used to optimize memory consumption. In
7.4 Iterators and Generators, Functional Programming 35

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

In the first case, a list comprehension in line 10 creates a new list of


densities whose values are calculated. This list is already in memory. The
implementation using the generator function in line 15 creates a generator that
calculates the density value for the respective tuple and occupies the memory only
when called in the for loop. Now imagine that the initial list radius_mass
contained not only four tuples, but forty billion. The implementation using a
generator would then require much less memory.
36 7 Functions

7.5 Anonymous Functions with the Lambda Operator

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.

7.6 Functions Can be Decorated: Decorators

A decorator is a Python object that is used to modify a function, method, or


class definition. Suppose we want to make sure that our calculate_density
function is actually called with only numbers as function arguments. Instead of
checking this in the function itself, we implement a decorator function that can
also be used for other functions.

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.

7.7 The Functional Arguments *args and **kwargs

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 39


C. Schäfer, Quickstart Python, essentials,
[Link]
40 8 Structuring with Modules

The output provides the correct value 0.7071067811865475. We have, therefore,


used the function sin and the constant value of π from the math module. It is
precisely the large number of modules available that has made Python so popular
and successful. Before you want or have to implement a new function yourself,
it is advisable to look for a module that already offers the required functionality.
Each module implements the two functions help() and dir(). The function
help() generates the help and description text of an object, in this case the
module. The dir() function lists all attributes of an object. This way we can
list all functions of a module and get help on specific elements

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

8.1 Structuring the Code with Own Modules

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

We can now access the density calculation function


42 8 Structuring with Modules

Larger programs are always structured in modules.

8.2 Some Important Modules and What to Do with Them:


Math os re sys

In this section we look at the most popular modules of the Python standard library
and explain their functionality.

Modules math and cmath


The math module, which we already got to know in the last section, provides the
most important mathematical functions. Since these functions are essentially wrap-
per functions to the respective C-function of the operating system, the speed of the
calculations is considerable and not much slower than in C or Fortran. Trigonome-
tric functions (sin, cos, tan, asin, acos, atan, atan2), hyperbolic
functions (asinh, acosh, atanh, sinh, cosh, tanh), and exponential
and logarithmic functions (exp, log, log2, log10, pow, sqrt) are availa-
ble. Furthermore, the usual mathematical constants such as Pi, the Euler number e
and Tau (pi, e, tau) are defined in the module. Some new functions have been
added to the math module since Python 3 and are listed in Table 8.1.
If the function arguments are elements of complex numbers, the functions of
the cmath module are used. As soon as the Python module NumPy is used, the
standard math module is left out because the numpy module implements its own
math functions that accept NumPy arrays as arguments.

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

Table 8.1 Newer (> Python 3) functions in the math module


Function Return value
erf(x) Returns the function value of the error
function. New since 3.2
gamma(x) Returns the function value of the gamma
function. New since 3.2
gcd(i, j) Returns the smallest common divisor of the
integer numbers i and j. New since 3.5
isclose(x, y,*, rel_tol = Returns True if the two values x and y are
1e-09, abs_tol = 0.0) close together, otherwise False. Where
rel_tol is the maximum distance between x and
y relative to the greater absolute value of x and
y. This function is very useful when comparing
floating point numbers. New since 3.5
log2(x) Returns the function value of the binary
logarithm. New since 3.3
remainder(x, y) Returns the residual value of division x/y. New
since 3.7

Table. 8.2 Objects in the os module


Function/Object Return value
chdir() Changes the current path to the function argument
getcwd() Returns the current working directory
getenv(key) Returns the value of the environment variable key
getpid() Returns the value of the process ID of the running process
mkdir(path) Creates a directory with the name path
name Contains the name of the operating system (Linux and
macOS supplies posix, Windows nt)
turkey(key, value) Sets the value of the environment variable key to value
remove(path) Deletes the file path
rmdir(path) Deletes the empty directory path
system(string) Executes the command string in a subshell. For more
information, see the module subprocess
walk(dir) Creates a generator for filenames from tuples of three
(dirpath, dirnames, filenames), traversing the
directory tree that is under dir. See the example in the text
44 8 Structuring with Modules

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.

9.1 Fast Numerical Calculations with Python: NumPy

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

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 45


C. Schäfer, Quickstart Python, essentials,
[Link]
46 9 Extensions for Scientists: NumPy, SciPy, Matplotlib, Pandas

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

• The elements of a [Link] are all of the same type.


• The size of a [Link] is fixed.
• Slicing a [Link] only creates a so-called view and still accesses
the original data, which can be accidentally changed. Slicing a list or a
tuple, however, creates a new object. See the differences between deep and
shallow copy in Sect. 5.2.
• The memory requirement of a [Link] is usually significantly smaller
than that of a list with the same number of elements.

Since the size of a [Link] is determined at build time, it is often more


efficient to first create a list of the later elements from which a [Link]
is generated than to add elements to an existing [Link]:
9.1 Fast Numerical Calculations with Python: NumPy 47

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)

Multidimensional NumPy arrays ([Link]) can be created in NumPy


in different ways. The size, the shape, the number of elements can be requested
from the object
48 9 Extensions for Scientists: NumPy, SciPy, Matplotlib, Pandas

and the shape of the [Link] can be changed

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

9.2 For Scientists: SciPy

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.

Fig. 9.1 Data points and function fitted using [Link]


9.3 Creating Diagrams and Graphics with Matplotlib 51

9.3 Creating Diagrams and Graphics with Matplotlib

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

Kepler’s 3rd law


Planets of the Solar System
Period in years

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.

9.4 Big Data Science with Pandas

The module pandas (Python Data Analysis Library) is a special extension of


Python for the management and analysis of tabular data, which we will men-
tion here for the sake of completeness. For this purpose, the module extends
the standard library by the three objects series, dataframe, and panel.
A series corresponds roughly to a 1D NumPy array, a dataframe consists
9.4 Big Data Science with Pandas 53

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 55


C. Schäfer, Quickstart Python, essentials,
[Link]
To Read More

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.

© Springer Fachmedien Wiesbaden GmbH, part of Springer Nature 2021 57


C. Schäfer, Quickstart Python, essentials,
[Link]

You might also like