49,19 €
This book provides a thorough introduction to Python programming designed for engineers and scientists. It begins with foundational topics like development environments and program structures, then introduces key Python libraries such as NumPy, SymPy, SciPy, Matplotlib, and VPython. Clear explanations and practical exercises help readers write efficient, well-structured code while progressing through increasingly complex projects.
The content covers core programming paradigms including functions, branching, and object-oriented design, followed by numerical analysis with NumPy and symbolic math with SymPy. Detailed chapters on data visualization with Matplotlib and 3D animations with VPython enhance comprehension. Additional focus on statistical computations, Boolean algebra, and interactive GUI programming with Tkinter prepares readers for real-world applications. Each chapter ends with project tasks reinforcing hands-on learning.
Throughout the book, readers build a strong skill set combining programming expertise and scientific problem-solving. By the end, they will confidently use Python’s libraries to solve diverse engineering and scientific challenges. This practical, project-based approach ensures knowledge is both solid and immediately useful in research and professional work.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 656
Veröffentlichungsjahr: 2025
Dr. Veit Steinkamp
This e-book is a publication many contributed to, specifically:
Editor Meagan WhiteAcquisitions Editor Hareem ShafiGerman Edition Editor Christoph MeisterCopyeditor Yvette ChinTranslation Winema Language ServicesCover Design Graham GearyPhoto Credit iStockphoto: 137993550/© OGphoto, 1336159068/© cemagraphicsProduction E-Book Kelly O’CallaghanTypesetting E-Book Satz-Pro, Germany
We hope that you liked this e-book. Please share your feedback with us and read the Service Pages to find out how to contact us.
The Library of Congress Cataloging-in-Publication Control Number for the printed edition is as follows:2024930350
ISBN 978-1-4932-2559-0 (print)ISBN 978-1-4932-2560-6 (e-book)ISBN 978-1-4932-2561-3 (print and e-book)
© 2024 by Rheinwerk Publishing Inc., Boston (MA)1st edition 2024
This e-book is protected by copyright. By purchasing this e-book, you have agreed to accept and adhere to the copyrights. You are entitled to use this e-book for personal purposes. You may print and copy it, too, but also only for personal use. Sharing an electronic or printed copy with others, however, is not permitted, neither as a whole nor in parts. Of course, making them available on the internet or in a company network is illegal as well.
For detailed and legally binding usage conditions, please refer to the section Legal Notes.
This e-book copy contains a digital watermark, a signature that indicates which person may use this copy:
You are reading this e-book in a file format (EPUB or Mobi) that makes the book content adaptable to the display options of your reading device and to your personal needs. That’s a great thing; but unfortunately not every device displays the content in the same way and the rendering of features such as pictures and tables or hyphenation can lead to difficulties. This e-book was optimized for the presentation on as many common reading devices as possible.
If you want to zoom in on a figure (especially in iBooks on the iPad), tap the respective figure once. By tapping once again, you return to the previous screen. You can find more recommendations on the customization of the screen layout on the Service Pages.
This chapter provides a brief overview of the extensibility, application areas, and functionality of the Python programming language.
If you need to perform extensive calculations for your scientific work and also want to present the results in a graphically appealing way, then you should seriously consider using Python. Python is a programming language whose functionality is similar to that of MATLAB when extended with appropriate modules. In addition, Python and all its extension modules are provided free of charge. Using Python, you can, for example, solve systems of equations, create function plots, differentiate, integrate, and also solve differential equations. You can also create graphical user interfaces (GUIs). For almost every problem in engineering and natural sciences, solutions exist that not only cover a wide range of applications, but also excel in their user-friendliness and performance.
The Python programming language was developed in the early 1990s by Dutchman Guido van Rossum at Centrum voor Wiskunde & Informatica (CWI) in Amsterdam. Its name has nothing to do with the snake but refers instead to the British comedy group Monty Python.
The particular advantages and features of this programming language include the following:
Python is an easy-to-learn and powerful programming language.
It provides efficient data structures.
It also allows object-oriented programming (OOP).
It has a clear syntax and dynamic typing.
Python programs are compiled using an interpreter and are therefore suitable for the rapid development of prototypes.
Python is available for Linux, macOS, and Windows.
Python can be extended by modules.
The module concept is the cornerstone and one of Python’s outstanding strengths. A module is a component of a software system and represents a functionally self-contained unit that provides a specific service. For a definable scientific problem, a module that is tailored precisely to this problem is provided in each case. In this book, I will introduce you to the NumPy, Matplotlib, SymPy, SciPy, and VPython modules.
A development environment is a software program that consists of a text editor, a debugger, and an interpreter. The text editor of a development environment supports a programmer in writing programs, for example, with features like syntax highlighting, automatic indentation of the source code, and so on. The debugger helps programmers find errors, and the interpreter executes the program’s statements. Of the many development environments that can be used to develop Python programs, only the Integrated Development and Learning Environment (IDLE), Thonny, and Spyder development environments will be briefly presented here.
The abbreviation IDLE stands for “Integrated Development and Learning Environment.” Figure 1.1 shows the user interface for IDLE.
Figure 1.1 The IDLE Development Environment
IDLE is part of the standard Python download. During the installation of Python, IDLE is installed at the same time as the Pip package manager. You can download the latest version of Python for the Linux, macOS, and Windows operating systems at https://www.python.org/downloads/. Then, you’ll need to install the NumPy, Matplotlib, SymPy, SciPy, and VPython modules individually using the Pip package manager (Section 1.1.4). This step may cause problems if you install a new Python version: The modules can no longer be imported with the new IDLE version, and the programs will no longer run. I will show you a way to fix this problem in Section 1.1.4. If the installation of the Python modules fails, I recommend you use the Thonny development environment.
When you click Run • Python Shell, the Python shell will open. Next to the >>> input prompt, you can directly enter Python commands or mathematical expressions, such as 2+3, 3*5, or 7/5. Note that you must complete each entry by pressing the (Return) key.
Compared to the professional solutions, Thonny is a rather simply designed development environment with a comparatively small range of functions. However, it is particularly suitable for programming beginners due to its ease of use. Using Thonny, you can run and test all the sample programs discussed in this book. Figure 1.2 shows the user interface.
Figure 1.2 The Thonny Development Environment
Thonny is available for Linux, macOS, and Windows and can be downloaded at https://thonny.org.
The source code of the program must be entered into the text editor (upper left area). Once the program has been started via the (F5) function key or by clicking the Start button, a window opens where you’ll need to enter the file name of the program. The result of numerical calculations is then output in the Command Line window at the bottom left of the Python shell. Each function plot of Matplotlib programs will be output in a separate window. In the shell, also referred to as the Python console, you can also enter Python commands directly. The Assistant in the main window, on the right, supports you in terms of troubleshooting, although you should temper your expectations about its capabilities.
A particularly important feature of Thonny is that you can easily install and update the NumPy, Matplotlib, SymPy, SciPy, and VPython modules. For these tasks, all you need to do is open the Tools • Manage Packages dialog box, as shown in Figure 1.3. Then, in the text box in the top-left corner, enter the name of the module you want to install and click Install or Update.
Figure 1.3 Installing Modules
To remove a module, you must select the corresponding module in the pane on the left. Then, the Uninstall command button appears to the right of the Install command button. One notable advantage of the package manager in Thonny is that you can also test older versions of all available modules. For this task, simply click the ... command icon to the right of the Install button, which will open a window where you can select the desired version of the module.
Spyder is the development environment of the Anaconda distribution of Python. Except for VPython, the modules covered in this book—NumPy, Matplotlib, SymPy, and SciPy—are already built in.
Figure 1.4The Spyder Development Environment
Spyder is available as a free download for Linux, macOS, and Windows at https://www.spyder-ide.org.
To run an animation using a Matplotlib program, you must select Automatic as the backend in the settings under IPython Console • Graphics. After starting the program, a separate window will open where the animation will run. Matplotlib programs containing slider controls can also be executed interactively only with this option.
Spyder is an immensely powerful development environment. However, one disadvantage is that the subsequent installation of modules that are not installed by default, such as VPython, can be difficult for beginners. For more information on installing Python modules, see the documentation for Spyder at https://www.spyder-ide.org.
To use development environments other than Thonny or Spyder, you can install Python modules using Pip. Pip is not a development environment but the package manager for Python that installs modules from the Python Package Index (PyPI) (https://pypi.org/). Pip allows you to download and update modules easily—when you use Python, Pip is a particularly important tool.
If you have installed Python and want to add only the NumPy module, for example, you can enter the following command in a terminal on Windows, Linux, or macOS:
The following command enables you to update an existing NumPy installation:
If you use IDLE (e.g., version 3.9) and install a new version of Python (e.g., 3.11), then the previously installed Python modules will no longer be imported into the updated version. In this case, you should try installing via pip3.11 install numpy.
For more information about using Pip, see https://pypi.org/project/pip. If the installation or update of the Python modules fails, I recommend using the Thonny development environment instead.
Whenever you learn a new programming language, the first thing you must know are the keywords defined in that language. Keywords are the reserved words of a programming language. They have a specific meaning in the programming language definition and must therefore not be used as variable names in a program. Python has 35 keywords, which you can view by entering the following commands in the Python console:
>>> import keyword
>>> a=keyword.kwlist
>>> print(a)
You’ll receive the following output:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Similarly, the following statement will output the number of keywords:
>>> print(len(a))
35
You don’t need to memorize all the keywords at first. For better overview of Python’s keywords, a useful exercise is to first arrange them according to their functionalities. Table 1.1 provides an overview of the most important keywords arranged according to functional criteria.
ConditionalStatements
Loops
Classes, Modules, and Functions
ErrorHandling
if
for
class
try
else
in
def
except
elif
while
global
finally
not
break
lambda
raise
or
as
nonlocal
assert
and
continue
yield
with
is
import
True
return
False
from
None
Table 1.1 The Most Important Keywords in Python
With a few keywords like if, else, for, and while, along with the built-in Python function print(), you can already write simple Python programs.
How should you read this book? Basically, you can read the individual chapters independently of each other. If you already know the basic structures of Python, then you can skip Chapter 2. If just starting to learn, you must read that chapter first as a prerequisite for understanding subsequent chapters.
Our approach to presentation and knowledge transfer is based on a uniform principle: One to three examples from electrical engineering, mechanical engineering, or physics are presented for each topic. After a brief description of the task, the complete source code is printed. Directly after the source code, the output (the results of the calculations) takes place. The source code is then discussed and analyzed.
Our analysis of source code also includes an analysis of the results (output). Are the results in line with expectations? Does the program solve the task set for it at all? Often, you won’t fully understand the source code of a program until you’ve taken a closer look at the output. After viewing the output, you can then analyze the source code again.
At the end of each chapter, one or more project assignments are provided, discussed, and fully solved to reinforce and expand on what was learned in that chapter.