Statistics
1
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-03-26

AuthorMarco Gähler

[D]esigned to help beginner developers easily learn the fundamentals and intermediate concepts of Python, [t]his book provides a structured approach to learning Python, covering essential topics from basic syntax to complex data structures and numerical computing. The book starts with an introduction to Python, explaining its advantages, drawbacks, and the importance of style guides. It then delves into key programming concepts, including variables, lists, strings, dictionaries, tuples, and rare data structures, helping readers build a strong foundation. Readers will also explore loops, conditional statements, Boolean logic, essential mathematical operations, and NumPy's use for numerical computations. Complex topics such as object-oriented programming, inheritance, file handling, unit testing, and data visualization using Matplotlib are covered in depth. Additionally, the book introduces practical applications in numerical mathematics, including Monte Carlo integration and differential equations. By the end, readers will have an understanding of Python’s core principles and practical applications, empowering them to write efficient, scalable, and well-structured code. This book is particularly helpful if you already know some other programming language and you want to kick start your Python knowledge. - Understand Python fundamentals, including variables, loops, and data structures. - What is object-oriented programming, inheritance, and file handling. - Explore numerical computing with NumPy and data visualization using Matplotlib. - Gain hands-on experience with unit testing, Monte Carlo integration, and real-world applications. This book is for beginners and aspiring programmers, software professionals transitioning to Python and students.

Tags
No tags
ISBN: 886882132X
Publisher: Apress
Publish Year: 2026
Language: 英文
Pages: 135
File Format: PDF
File Size: 5.2 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Python Made Easy
Marco Gähler Python Made Easy A Beginner’s Guide to Coding, Data Structures, and Practical Applications
Marco Gähler Zürich, Zürich, Switzerland ISBN 979-8-8688-2132-5 ISBN 979-8-8688-2133-2 (eBook) https://doi.org/10.1007/979-8-8688-2133-2 © Marco Gähler 2026 This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, 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. This Apress imprint is published by the registered company APress Media, LLC, part of Springer Nature. The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A. If disposing of this product, please recycle the paper.
Declarations Competing Interests The author has no competing interests to declare that are relevant to the content of this manuscript. v
Contents 1 About Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Advantages and Drawbacks of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Importing Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Style Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Software Engineering Made Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Naming Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Writing Readable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3 Components of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Mutable and Immutable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 None . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Creating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Accessing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Accessing Sublists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Multidimensional Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Sorting lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Merging Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 How to Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Various Types of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Dicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 vii
viii Contents Tuples vs. Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Rarely Used Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Try, Catch, Else, Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 When to Use Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Coupling and Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4 Loops and If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Nested If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Boolean Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Return, Break, and Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5 Basic Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Basic Mathematics with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Math Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Calculating Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6 Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 args and kwargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Function Definition Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Cyclic Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 7 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Implementation Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Contents ix Interface Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 8 Python Modules and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Modules and Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Regular Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Namespace Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Installing Packages with Pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The Virtual Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 9 Util . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 JSON and Co . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 HDF5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Timing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 10 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Why Tests? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 How to Write Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 What to Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Example of TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Mocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Faking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 11 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Simple Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Labeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Histograms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Local Minima of Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 12 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Pandas Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13 Numerical Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Mathematical Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Geometric Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Higher Order Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Estimating the Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
x Contents Numerical Solution of Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Coupled Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 14 Some Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Percolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
About the Author Marco Gähler began his career studying physics at ETH Zurich before transitioning to software engineering. In 2018, he joined Zurich Instruments, where he developed electronic devices used in quantum computing. At the time of writing, he is founding a company EasyCare, which organizes home treatment of elderly people. Throughout his career, Marco has observed the pitfalls in code written by self-taught developers, for example, PhD students, and recognized the need for clear, practical guidance on simple programming practices. xi
About the Technical Reviewer Shibsankar Das is currently working as a senior data scientist at Microsoft. He has 10+ years of experience working in IT where he has led several data science initiatives, and in 2019, he was recognized as one of the top 40 data scientists in India. His core strength is in GenAI, deep learning, NLP, and graph neural networks. Currently, he is focusing on his research on AI agents and knowledge graphs. He has experience working in the domain of foundational research, FinTech, and ecommerce. Before Microsoft, he worked at Optum, Walmart, Envestnet, Microsoft Research, and Capgemini. He pursued a master’s from the Indian Institute of Technology, Bangalore. xiii
Introduction Is there a reason to write a book about programming in times of artificial intelligence (AI)? I think, yes, there is. You still have to learn the fundamentals of programming, and that’s what this book is about. AI can do many things by now, but it cannot replace yet every part of software engineering. And this is where this book comes into place. I try to give you an idea of what programming is about, about the pitfalls of Python, and what Python has to offer. I didn’t write exercises in this book because I think the best way of learning a programming language is by doing a project. I never really solved the exercises anyway; learning by doing is, in my opinion, the much more effective way of learning. This book is not going to teach you everything there is to know about Python. It’s simply impossible to learn too many things. Though there are books on Python that tried, these are 1000 pages long and feel more like encyclopedias. I didn’t want to write such a book because who reads 1000 pages about Python? I think it’s much more efficient to write a concise book that you can read back to back and later on you will anyway use Google or an AI assistant to find the information you need. I think a book on a programming language should give you an idea of how the programming language works and what it can do. It should only kick start your journey into this new language. The times of printed encyclopedias are over. I wrote this book with scientists and engineers in mind. Of course, you can also do web development, IT security, and many other things with Python. But I didn’t write about these things. Explaining the Django web framework or some IT security is too specific for this book and out of scope. Science and engineering, on the other hand, need very little knowledge in order to get started. Furthermore, scientists and engineers have the potential to become good programmers. But first, they have to learn it properly, which, unfortunately, is something many experienced programmers didn’t do. And for this reason, there is a lot of bad code out there. When programming, you often have to find out why something doesn’t work as it should, so you have to follow the traces of the code to find the error. You also have to structure the knowledge you have about the code as well as possible and thus make it available to your coworkers. Knowing how to work scientifically is a great advantage in programming. This book is only the beginning. It takes some intrinsic motivation to become a good programmer. For this reason, there are no exercises in this book, nor is it hundreds of pages long explaining all kinds of things you won’t remember. Instead, I tried to write a concise book that you can really read back to back. Though I have to admit that I also added some examples of mathematical problems for scientists and engineers toward the end of the book for the interested reader. But you don’t have to read them if you’re not interested in them. Usually by far the best way of learning programming is having a personal project to work on. This gives you a goal and practical experience. You also learn to work independently. This book won’t teach you everything that you need to know for your project; it is only able to give you a jump start. You can take some of the things that you learned and combine them into something new. But you also xv
xvi Introduction have to find out a lot of things yourself. Here, your best friend is good old Google, or also some of the many AI tools that can answer your questions as well. Once you get started, it is important to keep learning. Ask some friends for advice to improve your code, do an internship, watch YouTube videos, or read books that help you write better code. For example, my other book, Software Engineering Made Easy, further explains many of the gray boxes you will find in this book. Learn from different sources and compare their advice. Programming is not an exact science, and you will have to find out what works best for you. There’s an infinite number of ways to solve a problem, and you have to find the one that works best for you.
1About Python Python is a universal, interpreted, high-level programming language. It was developed in 1991 by Guido van Rossum and is named after the comedy group Monty Python. The current version, 3, was released in 2008, with new features being added all the time. Python is so widely used that it is preinstalled on most operating systems. Simply search your operating system for “Python,” or open the command line and type. If, contrary to expectations, it is not installed, you can download and install Python from python.org. Python is an interpreted language; it executes code line by line, which enhances flexibility and speeds up development. This feature makes it ideal for beginners and professionals alike, allowing real-time feedback and simplifying the debugging process. Its interpreted nature also supports dynamic typing and interactive execution, making Python a preferred choice for rapid prototyping, scripting, and automation across platforms. The REPL Since Python is an interpreted language (the alternative would be a compiled language), there is also a special command line that allows you to run Python code. This is also called a “REPL” (Read-Eval- Print Loop). You can also tell whether some code examples are executed in the REPL by the fact that every line with user input begins with > > > or .... In Figure 1.1 you can see a screenshot of the REPL, which I opened in PowerShell by typing Python. The alternative is to write code in a text editor (without formatting, not Word!) and then save it as a .py file, for example, PrintAddress.py. You can then run this code in the normal command line by calling Python with python PrintAddress.py and the entire code will be executed. If you write code that is longer than just a few lines, I recommend that you save it in a special text file and not use the REPL. At the time of writing, I use the VS Code integrated development environment (IDE) for writing code. I recommend you use it as well, unless you have a friend helping you with another IDE. Using the REPL The REPL is great for playing around with a few lines of code, but for longer code, it becomes tedious to use. I use it only for short code snippets. © The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2026 M. Gähler, Python Made Easy, https://doi.org/10.1007/979-8-8688-2133-2_1 1
2 1 About Python Fig. 1.1 The REPL of Python started in PowerShell. I have already executed the “hello world” command on the last three lines As far as I know, there is only one somewhat significant difference between the REPL and executing code as a text file. This is the way you can inspect an object. In “normal” code, you have to call the print function to do this. Meanwhile, in the REPL, you can omit the print command and just enter the variable itself. The REPL then returns the value of the variable. >>> "echo" ’echo’ In this short example, we also saw the syntactical difference between the REPL and the code in text form. In the REPL, we write a > > > before each new line. You don’t have to enter this yourself; that would be very tedious and pointless. Instead, this is simply the way to show that you are in the REPL and not, for example, in the normal command line or in the text editor. Whether you use the REPL or prefer to write code as normal text is largely up to you. As I wrote before, I recommend that you write longer pieces of code as text and only use the REPL when you want to inspect the effect of one or two lines of code. Writing long pieces of code in the REPL is possible but tedious. An alternative to the REPL is the Jupyter Notebook. Jupyter allows you to execute code in your browser and display the results directly. Jupyter is useful for small- to intermediate-sized projects, such as some data analysis. For larger projects, I recommend using pure code files, as this allows you to use version control systems like Git. Git Git is the version control system. Most professional software companies use it. With Git, you can take snapshots of your code and jump back and forth between them. Furthermore, there are websites like GitHub that host Git projects, making it easy to back up your code. Git is also great for collaborating with others. You should learn Git as soon as you start your first bigger project.
Importing Libraries 3 Advantages and Drawbacks of Python While Python is great for faster development due to the fact that it is an interpreted language without the need for defining types, it has its drawbacks when writing large-scale software. The types of variables and arguments are not just boilerplate code; they are an important piece of information. For this reason, Python also introduced type hints, even though they are neither mandatory nor enforced. You could take the following function add and pass it two strings instead of ints. def add(a: int, b: int) -> int: return a + b add("hello ", "world") # returns "hello world" A compiler can give you a lot of important information when writing code. It’s an advantage that you save some time as you don’t have to wait for the compiler after every change you make, but on the other hand, running the unit tests in Python may take some time, as executing code in Python is slow. In bigger projects, this may outweigh the time you save by not having to compile the code. These are drawbacks to consider when starting a new project and considering using Python as your programming language. Though there are tools like pylint that can give you some warnings if your code is off, they do not replace the capabilities of a full-fledged compiler. The only other thing that can check your code for errors is unit tests (p. 91). For this reason, it is even more important to write unit tests in Python than in compiled languages. Generally, I don’t recommend using Python for larger projects. It is a great language for small- to medium-sized projects, but if you are working on a large project, I recommend using a compiled language like Rust, Java, or C++. These languages are much more efficient with memory usage and much faster, and the compiler can give you a lot of useful information; meanwhile, in Python, you’ll only find errors at runtime. Importing Libraries As in any commonly used programming language, there is a plethora of libraries available for Python. Libraries are collections of prewritten code that provide functions and tools for specific tasks, such as data analysis, web development, or machine learning. They save time and effort by eliminating the need to write code from scratch, allowing developers to build applications more efficiently and with fewer errors. In Python, there are libraries for pretty much any programming topic that you could think of. In order to use a third-party library, you first have to install it, unless it is one of the few standard libraries that are already preinstalled. Installing a library can be done in many different ways. The easiest one is using the pip command. Pip is the package manager for Python and is installed by default when you install Python. You have to open the command line and run pip install numpy to install the NumPy library, for example.
4 1 About Python Virtual Environments While it is possible to call pip install numpy in the command line, it is generally recom- mended to use a virtual environment because different projects that you work on may have different dependencies. Furthermore, there are plenty of tools that simplify the management of dependencies using virtual environments. Here is a short example of how to create a virtual environment and install NumPy in it. Note that there is a minor difference between Linux and Windows. On Linux, you have to use source venv/bin/activate to activate the virtual environment, while on Windows, you have to use venv\Scripts\activate. python -m venv venv source venv/bin/activate pip install numpy If you are working on a serious project, you will be collaborating with other developers. Then it’s worth creating a requirements.txt file that lists all the dependencies of your project. This file can be used to install all dependencies with a single command. You can install libraries with the following command: pip install -r requirements.txt In order to use a library in your project, you have to import it. In Python, this is fairly simple. You just write import numpy at the beginning of your code. Style Guides Software development is not an exact science where there is only right or wrong. Instead, there are endless ways to implement a certain functionality. Some of them are better than others. There are websites, YouTube videos, and a plethora of books, including my own,1 with tips on how to write good code. A few examples are the Zen of Python2 or the Google Style Guide.3 I can highly recommend reading through these resources as they help you get an idea of how to write good code. Learning Programming This book won’t be sufficient to learn programming. It only serves as a short overview of what you can learn. The best way to learn programming is to have a project to work on or at least to attend a course where you get exercises to solve. This gives you a goal and practical experience. If you encounter a problem you haven’t faced before, you have to search the internet or ask an AI tool for advice. This is what programming is really about. The simplest way to get started solving a problem is to take some example code that does something similar to what you want to do and modify it until it does exactly what you want. In fact, this is generally a very efficient way to learn something new, not only in programming. 1 Software Engineering Made Easy, Marco Gähler. 2 https://peps.python.org/pep-0020/ 3 https://google.github.io/styleguide/pyguide.html
Style Guides 5 Software Engineering Made Easy In my book Software Engineering Made Easy, I came up with four fundamental principles that I think are very important to adhere to. • Write code that is easy to understand. As we’ll see on p. 8, you’ll spend much more time reading code than writing it. For this reason, it is of utmost importance that you write code that is easy to understand. • Write code that can have as few bugs as possible. Humans are fallible; we make mistakes. For this reason, one should make code as fail-safe as possible. Of course, you cannot prevent all bugs, but you should try to make it as hard as possible for bugs to occur. • Constantly clean up your code. Over time, the entropy of your code increases and it will become a mess. Just like anything else in the universe, entropy always increases and you have to fight it. At home, it is called cleaning up; in software, it is called refactoring. • Write code to create value for your customers. There are many software engineers who love to discuss about meaningless details for hours. Please don’t do this. You will be paid by your customers, and they want to have new features, not to pay for meaningless details. The Zen of Python • Explicit is better than implicit. • Simple is better than complex. • Readability counts. Smart people often tend to write implicit code because it is more efficient and I have to admit that implicit code can be beautiful. However, implicit code is often hard to read and understand, and therefore, it is more error-prone. For the same reason, you should keep the code also as simple as possible. • Flat is better than nested. This is a very important principle. Nested code is often hard to read and understand, and therefore, it is more error-prone. And if you don’t believe me, just write some nested loops with if/else statements and try to understand what the code does. You will have a hard time. • Special cases aren’t special enough to break the rules. • Although practicality beats purity.
6 1 About Python Write the code such that there are hardly any special cases, such that there are hardly any if statements. Though you should not overdo it. It is not worth to write a hundred lines of code only to replace one if statement. • In the face of ambiguity, refuse the temptation to guess. Guessing is done way too often in software engineering. It is better to write a few lines of code that are not needed than to guess what the code does. And if you are guessing, you should at least write some unit tests. • There should be one—and preferably only one—obvious way to do it. It is better to write the code in a way that there is only one way to do it than to write the code in a way that there are multiple ways to do it. This is because there is always the risk that you will forget to update one of the ways, and then you will have a bug in your code. • If the implementation is hard to explain, it’s a bad idea. • If the implementation is easy to explain, it may be a good idea. Code should be easy to understand. In most cases, others will have to understand the code without any help. If you can’t explain the code to someone else, it is a sign that the code is not good enough. And if you ever happen to read code that you don’t understand, it is not your fault. It’s the fault of the author of the code. They didn’t manage to write the code in a way that is easy to understand.
2Naming Now it might be a little surprising to you that I start this book with the chapter on naming. You were probably expecting some code, but there is no denying that naming is one of the most important and difficult things in software development. Every variable (p. 15), function (p. 57), or class (p. 67) has a name. This name is generally the only description of an object that you have when reading code. You read the name, and you are supposed to know what the object does. It’s just the same when I hear my name being called, and I know that somebody wants to talk to me. If there is someone else with the same name around, it will cause a lot of confusion. For these reasons, naming is a very important and also a very difficult part of software development. A good name can make the code more understandable, while a bad name can make the code incomprehensible. However, finding good names is one of the hardest things in software development because there is no algorithm that tells you how to name things. You have to rely on your intuition and experience. Let’s take a quick look at an example with bad names. def a(b, c): pass As we can see, poorly chosen names cause the code to become incomprehensible. What is this code all about? We have no idea. We’d have to read through the implementation of the function to find out what it does. But this is extremely time-consuming and error-prone. It is much better to choose a good name for the function to begin with. The Importance of Naming Naming is one of the hardest, yet also one of the most important things in software development. Only with good names can your code be understood. If you choose the names poorly, it’s like reading a foreign language. You won’t understand a thing. © The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2026 M. Gähler, Python Made Easy, https://doi.org/10.1007/979-8-8688-2133-2_2 7