Coding in Python
Writing programs in the programming language Python [236] is not that difficult as you might expect. Even if you are
not an experienced program-mer, there is still support to learn the most important Python constructs to finally
implement the dashboard that you designed for visually explor-ing your or other people’s data [255]. In this
chapter we are trying to give a step-by-step tutorial on how to create Python programs, starting with simple
instructions and more and more walking into the directions of much more complex programs including both functional
and object-oriented programming paradigms. Since writing about Python programming can fill several books, we will
only focus on the most relevant Python constructs to implement dashboards with some but not all possible
functionalities. For the interested reader we suggest to read a more Python-specific book that provides many more
insights into Python code constructs that are not used on an everyday basis, for example, to build dashboards for
visually and algorithmically exploring data, or to analyze data based on machine learning and further data science concepts
[28].
In the first few sections, we more or less use Python as some kind of calculator to mathematically evaluate
arithmetic expressions, even Boolean expressions consisting of Boolean and relational operators as well as further
arithmetic expressions. Those expressions build some kind of basic structure when writing computer programs since they
describe how to put information together, how to aggregate and evaluate that to guide the control flow of a program.
Unfortunately, programming is not as easy as writing expressions but more complex constructs are required like loops or
conditionals, as well as data structures with one-dimensional lists being the basic ones from a longer list of possibilities
including dictionaries, for example. For each subsection, we will provide some exercises that are worth solving by either
using Jupyter Notebook for the simple ones or later, for the more complex ones, using an IDE like PyCharm or
Spyder for example. This chapter can also be
110 Coding in Python
studied as an introduction into Python programming without drifting away into dashboard design and implementation,
however, we have written it as a tool to understand, develop, and extend dashboard code, focusing on solving certain user-
defined data exploration tasks.
We start the chapter with expressions (Section 4.1) that are impor-tant ingredients to evaluate complex
combined mathematical, relational, or Boolean problems, even supporting bitwise operations. In Section 4.2, we
describe the most common basic and composite data types and explain the concept of variables, constants, and
conversions. In Section 4.3, we have a look into strings and characters as well as into typical functions and methods to
allow meaningful operations on them. To allow branching in the control flow, we describe the use of conditionals as well
as exceptions that must be handled sometimes (Section 4.4). A certain number of similar executions with varying values
can be modeled by loops that can occur as definite or indefinite ones (Section 4.5). To encapsulate functionality like
subroutines that are used at several locations in the code, we introduce the concept of functions in Section 4.6. A
mighty concept in programming is recursion that can be used in its standard form as well as tail recursion. Also, high-order
functions and lambda expressions serving as anonymous functions are worth discussing (Section 4.7). To allow a
communication with the users and to support them at tasks like data reading and writing, we explain the most important
operations to work with data files (Section 4.8). Apart from functional or procedural programming paradigms, we can
also create classes and objects, which is typically falling into the object-oriented programming paradigm which is
discussed in Section 4.9. Again, we do not focus on completeness in this chapter, and we only want to describe the
most important ingredients to get started in Python programming.
4.1 Expressions
Evaluating mathematical constructs composed of arithmetic expressions by adding, subtracting, multiplying, or dividing
is one of the major ingredients in nearly any computer program. Such expressions can get quite long with a multitude of
operators connecting the individual parts. It is important to understand in which order such expressions are
computed, for example, prioritizing some expressions by using parentheses. Understanding the laws of execution and
evaluation is an important ingredient to avoid errors that might be hard to locate later on in a computer program. Each
operator has some kind of precedence in Python, and in any other programming language,