Inhaltsverzeichnis

Alle Kapitel aufklappen
Alle Kapitel zuklappen
Materials for This Book
19
1 Introduction
21
1.1 Advantages of Python
21
1.2 Prevalence of Python
22
1.3 Structure of This Book
22
1.4 Exercises
23
1.5 Installation on Windows
23
1.6 Installation on Ubuntu Linux
24
1.7 Installation on macOS
25
2 Getting Started
27
2.1 Python as a Calculator
27
2.1.1 Entering Calculations
27
2.1.2 Addition, Subtraction, and Multiplication
28
2.1.3 Division, Integer Division, and Modulo
28
2.1.4 Ranking and Parentheses
29
2.1.5 Exercise
29
2.1.6 Variables and Assignment
30
2.1.7 Exercise
31
2.2 Our First Program
31
2.2.1 Hello World
31
2.2.2 Entering a Program
32
2.3 Saving and Running
32
2.3.1 Saving
32
2.3.2 Running on Windows
33
2.3.3 Running on Ubuntu Linux and on macOS
34
2.3.4 Comments
35
2.3.5 Concatenating Outputs
35
2.3.6 Long Outputs
36
3 Programming Course
37
3.1 Programming a Game
37
3.2 Variables and Operators
38
3.2.1 Assignment, Calculation, and Output
38
3.2.2 String Literals
38
3.2.3 Entering a String
39
3.2.4 Entering a Number
40
3.2.5 Our Game: Version with Input
41
3.2.6 Exercises
42
3.2.7 Random Numbers
42
3.2.8 Determining a Type
43
3.3 Branches
44
3.3.1 Comparison Operators
44
3.3.2 Branch Using if
46
3.3.3 Our Game: Version with Input Evaluation
47
3.3.4 Exercise
47
3.3.5 Multiple Branches
48
3.3.6 Exercise
49
3.3.7 Conditional Expression
49
3.3.8 Logical Operators
50
3.3.9 Exercises
51
3.3.10 Multiple Comparison Operators
52
3.3.11 Our Game: Version with Exact Evaluation of the Input
53
3.3.12 Branch with match
54
3.3.13 Order of Operations
56
3.4 Loops
57
3.4.1 Loops with for
57
3.4.2 Terminating a Loop Using break
59
3.4.3 Continuing a Loop Using continue
59
3.4.4 Nested Control Structures
60
3.4.5 Our Game: Version with for Loop and Abort
61
3.4.6 Loops with for and range()
62
3.4.7 Exercises
64
3.4.8 Our Game: Version with range()
65
3.4.9 Loop with while
66
3.4.10 Our Game: Version with while Loop and Counter
67
3.4.11 Exercise
68
3.4.12 Combined Assignment Expressions
68
3.5 Developing a Program
69
3.6 Errors and Exceptions
69
3.6.1 Base Program
70
3.6.2 Intercepting Errors
70
3.6.3 Outputting an Error Message
71
3.6.4 Repeating an Entry
72
3.6.5 Exercise
73
3.6.6 Our Game: Version with Exception Handling
73
3.7 Functions and Modules
74
3.7.1 Simple Functions
75
3.7.2 Functions with One Parameter
76
3.7.3 Exercise
77
3.7.4 Functions with Multiple Parameters
77
3.7.5 Functions with Return Value
78
3.7.6 Our Game: Version with Functions
79
3.7.7 Exercises
80
3.8 Type Hints
80
3.8.1 Variables and Type Checks
81
3.8.2 Functions and Type Checks
82
3.9 The Finished Game
84
4 Data Types
89
4.1 Numbers
89
4.1.1 Integers
89
4.1.2 Numbers with Decimal Places
91
4.1.3 Exponential Operator **
91
4.1.4 Rounding and Conversion
92
4.1.5 Trigonometric Functions
93
4.1.6 Other Mathematical Functions
94
4.1.7 More Precise Figures with Decimal Places
97
4.1.8 Complex Numbers
99
4.1.9 Bit Operators
101
4.1.10 Fractions
103
4.2 Character Strings
106
4.2.1 Properties
106
4.2.2 Operators
107
4.2.3 Slices
108
4.2.4 Mutability
110
4.2.5 Searching and Replacing
110
4.2.6 Removing Blank Spaces
112
4.2.7 Decomposing Texts
113
4.2.8 Constants
114
4.2.9 The bytes Data Type
114
4.3 Lists
115
4.3.1 Properties and Operators
115
4.3.2 Multidimensional Lists
117
4.3.3 Mutability
118
4.3.4 Methods
119
4.3.5 List Comprehension
121
4.4 Tuples
122
4.5 Dictionaries
124
4.5.1 Properties, Operators, and Methods
124
4.5.2 Dynamic Views
126
4.6 Sets
127
4.6.1 Properties, Operators, and Methods
127
4.6.2 Set Theory
129
4.7 Truth Values and Nothing
130
4.7.1 Truth Values “True” and “False”
130
4.7.2 Nothing and the None Keyword
132
4.8 Reference, Identity, and Copy
133
4.8.1 Reference and Identity
133
4.8.2 Saving Resources
135
4.8.3 Copying Objects
136
4.9 Type Hints
136
4.9.1 A Sequence as a Parameter
137
4.9.2 Restriction to a List
137
5 Advanced Programming
139
5.1 General Remarks
139
5.1.1 Combined Assignment Operators
139
5.1.2 Statement in Multiple Lines
140
5.1.3 Input with Assistance
141
5.1.4 The pass Statement
142
5.2 Output and Formatting
143
5.2.1 The print() Function
143
5.2.2 Formatting Numbers with Decimal Places
144
5.2.3 Formatting Integers
146
5.2.4 Formatting Strings
146
5.2.5 Exercise
147
5.3 Functions for Iterables
147
5.3.1 The zip() Function
148
5.3.2 The map() Function
148
5.3.3 The filter() Function
150
5.4 Encryption
151
5.5 Errors and Exceptions
153
5.5.1 General Remarks
153
5.5.2 Syntax Errors
153
5.5.3 Runtime Errors
154
5.5.4 Logical Errors and Debugging
155
5.5.5 Generating Errors
158
5.5.6 Distinguishing Exceptions
159
5.6 Functions
160
5.6.1 A Variable Number of Parameters
161
5.6.2 Named Parameters
161
5.6.3 Optional Parameters
162
5.6.4 Multiple Return Values
163
5.6.5 Transferring Copies and References
164
5.6.6 Namespaces
166
5.6.7 Recursive Functions
167
5.6.8 Lambda Functions
168
5.6.9 Functions as Parameters
169
5.7 Built-In Functions
169
5.7.1 The max(), min(), and sum() Functions
171
5.7.2 The chr() and ord() Functions
171
5.7.3 The reversed() and sorted() Functions
172
5.8 Other Mathematical Modules
173
5.8.1 Drawing Function Graphs
173
5.8.2 Multiple Partial Drawings
175
5.8.3 One-Dimensional Arrays and Vectors
177
5.8.4 Multidimensional Arrays and Matrices
179
5.8.5 Signal Processing
182
5.8.6 Statistics Functions
183
5.9 Custom Modules
186
5.9.1 Creating Custom Modules
186
5.9.2 Standard Import of a Module
186
5.9.3 Importing and Renaming a Module
187
5.9.4 Importing Functions
187
5.9.5 Exercise
187
5.10 Command Line Parameters
188
5.11 “Fractions Training” Program
189
5.11.1 The Program Flow
189
5.11.2 Main Program
190
5.11.3 An Easy Task
191
5.11.4 A Moderately Difficult Task
191
5.11.5 A Difficult Task
193
6 Object-Oriented Programming
195
6.1 Basic Principles
195
6.1.1 What Is Object-Oriented Programming?
195
6.1.2 Classes, Objects, and Custom Methods
196
6.1.3 Special Members
198
6.1.4 Exercise
199
6.1.5 Operator Methods
200
6.1.6 Reference, Identity, and Copy
203
6.1.7 Concatenation
205
6.1.8 Nesting
206
6.1.9 Exercises
207
6.2 Advanced Topics
209
6.2.1 Inheritance
209
6.2.2 Exercise
211
6.2.3 Multiple Inheritance
211
6.2.4 Data Classes
213
6.2.5 Enumerations
214
6.2.6 Our Game: Object-Oriented Version
217
7 Various Modules
219
7.1 Date and Time
219
7.1.1 The time Module
219
7.1.2 Stopping a Program
221
7.1.3 The timedelta Class
222
7.1.4 The datetime Class
224
7.1.5 The calendar Class
226
7.1.6 Our Game: Version with Time Measurement
228
7.1.7 Our Game: Object-Oriented Version with Time Measurement
230
7.2 Queues
231
7.2.1 The SimpleQueue Class
231
7.2.2 The LifoQueue Class
232
7.2.3 The PriorityQueue Class
232
7.2.4 The deque Class
233
7.3 Multithreading
236
7.3.1 What Is Multithreading Used For?
236
7.3.2 Generating a Thread
236
7.3.3 Identifying a Thread
238
7.3.4 Shared Data and Objects
239
7.3.5 Threads and Exceptions
240
7.4 Regular Expressions
241
7.4.1 Finding Text Parts
242
7.4.2 Replacing Text Parts
244
7.5 Audio Output
246
8 Files
247
8.1 Opening and Closing a File
247
8.2 Text Files
248
8.2.1 Writing a Text File
248
8.2.2 Reading a Text File
250
8.2.3 Writing a CSV File
252
8.2.4 Reading a CSV File
254
8.3 Files with a Defined Structure
255
8.3.1 Formatted Writing
255
8.3.2 Reading in Any Position
256
8.3.3 Writing in Any Position
257
8.4 Serialization Using pickle
258
8.4.1 Writing Objects to Files
259
8.4.2 Reading Objects from Files
260
8.5 Data Exchange Using JSON
261
8.5.1 Writing Objects to Files
262
8.5.2 Reading Objects from Files
263
8.6 Editing Multiple Files
264
8.6.1 The glob.glob() Function
264
8.6.2 The os.scandir() Function
265
8.7 Information about Files
266
8.8 Managing Files and Directories
266
8.9 The Morse Code Sample Project
268
8.9.1 Reading Morse Code from a File
268
8.9.2 Output on the Screen
269
8.9.3 Output with Sound Signals
270
8.10 Our Game: Version with Highscore File
271
8.10.1 Sample Entries
271
8.10.2 Structure of the Program
272
8.10.3 The Code of the Program
272
8.11 Our Game: Object-Oriented Version with Highscore File
276
9 Databases
281
9.1 Structure of Databases
281
9.2 SQLite
282
9.2.1 Database, Table, and Data Records
282
9.2.2 Displaying Data
284
9.2.3 Selecting Data
285
9.2.4 The LIKE Operator
287
9.2.5 Sorting the Output
288
9.2.6 Selection after Entry
289
9.2.7 Changing Data Records
291
9.2.8 Deleting Data Records
292
9.2.9 Exercise
293
9.3 MySQL
295
9.3.1 XAMPP and Connector/Python
295
9.3.2 Database, Table, and Data Records
296
9.3.3 Displaying Data
298
9.4 Our Game: Version with Highscore Database
299
9.5 Our Game: Object-Oriented Version with Highscore Database
301
10 User Interfaces
303
10.1 Introduction
303
10.1.1 Our First GUI Program
303
10.1.2 Setup and Arrangement
305
10.1.3 Event Methods for Multiple Widgets
307
10.2 Widget Types
308
10.2.1 Single-Line Input Field
309
10.2.2 Hidden Input and Deactivating a Widget
310
10.2.3 Multiline Input Field
312
10.2.4 List Box with Simple Selection
314
10.2.5 List Box with Multiple Selection Option
317
10.2.6 Spin Boxes
318
10.2.7 Radio Buttons and the Widget Variable
320
10.2.8 Check Buttons
323
10.2.9 Sliders and Scales
324
10.3 Images and Mouse Events
326
10.3.1 Embedding and Changing an Image
326
10.3.2 Mouse Events
329
10.4 The place Geometry Manager
332
10.4.1 Window Size and Absolute Position
332
10.4.2 Relative Position
333
10.4.3 Changing the Position
335
10.5 Menus and Dialog Boxes
337
10.5.1 Menu Bars
337
10.5.2 Context Menus
340
10.5.3 Standard Dialog Boxes
342
10.5.4 Custom Dialog Boxes
345
10.6 Drawings and Animations
346
10.6.1 Various Drawing Objects
347
10.6.2 Controlling Drawing Objects
349
10.6.3 Animating Drawing Objects
350
10.6.4 Collision of Drawing Objects
352
10.7 Our Game: GUI Version
353
10.8 Exercise
358
11 User Interfaces with PyQt
361
11.1 Introduction
361
11.1.1 Our First GUI Program
361
11.1.2 Setup and Arrangement
363
11.1.3 Size of the Application Window
365
11.1.4 Event Methods for Multiple Widgets
365
11.2 Widget Types
366
11.2.1 Single-Line Input Field
366
11.2.2 Hidden Inputs and Disabling Widgets
368
11.2.3 Multiline Input Field
370
11.2.4 List with a Single Selection Option
373
11.2.5 List with Multiple Selection Options
375
11.2.6 Combo Boxes
377
11.2.7 Spin Boxes
379
11.2.8 Radio Buttons
381
11.2.9 Multiple Groups of Radio Buttons
383
11.2.10 Checkboxes
386
11.2.11 Sliders
388
11.2.12 Images, Formats, and Hyperlinks
390
11.2.13 Standard Dialog Boxes
392
11.3 PyQt and SQLite
397
11.3.1 The User Interface of the Application
397
11.3.2 Widgets on the Left
398
11.3.3 Widgets in the Middle
399
11.3.4 Widgets in the Right
399
11.3.5 List Widget and Python List
400
11.3.6 Database File and Window
401
11.3.7 Three Auxiliary Methods
402
11.3.8 Display in the List Widget
403
11.3.9 Selection in a List Widget
404
11.3.10 Inserting a New Data Record
405
11.3.11 Changing a Data Record
406
11.3.12 Deleting a Data Record
407
11.3.13 Searching for a Name
408
11.3.14 Main Program
409
11.4 Exercise
409
Appendices
411
A Miscellaneous Topics
411
A.1 Installing Additional Modules
411
A.2 Creating Executable Files
412
A.3 Installing XAMPP
413
A.4 UNIX Commands
414
A.5 Tips for Development
416
B The Author
419
Index
421