Getting Started with Python
If you want to program with Python, you’ve come to the right place! Take your first steps with this Python crash course that teaches you to use core language elements, from variables to branches to loops. Follow expert guidance to work with data types, functions, and modules—and learn how to manage errors and exceptions along the way. Apply Python programming to develop databases, graphical user interfaces, widgets, and more. Practice your skills with example exercises, and start developing your own applications with Python today!
- Develop your own Python programs, step by step
- Understand how to work with variables, operators, loops, data types, functions, and modules
- Follow detailed exercises to build Python applications, GUIs, and more
You'll learn about:
- Language Basics:
Learn the Python fundamentals, from initial installation to running your first program. Master core concepts including variables, branching, loops, functions, and modules; then work with data types such as integers, strings, and tuples.
- Python in Practice:
Apply Python programming in key development scenarios. Use Python for object-oriented programming, create and identify threads, manage files and directories, develop databases with SQLite and MySQL, and more.
- Example Exercises:
Get hands-on experience with practical programming exercises. Follow along with a game development example as you move from chapter to chapter. You’ll be ready to create your own applications with Python in no time!
Key Highlights:
- Installation
- Python language basics
- Object-oriented programming
- Error handling
- Data types
- Multithreading
- File processing
- Databases
- GUI programming
- PyQt
- Example exercises
View Full Table of Contents
- Materials for This Book
- 1 Introduction
- 1.1 Advantages of Python
- 1.2 Prevalence of Python
- 1.3 Structure of This Book
- 1.4 Exercises
- 1.5 Installation on Windows
- 1.6 Installation on Ubuntu Linux
- 1.7 Installation on macOS
- 2 Getting Started
- 2.1 Python as a Calculator
- 2.1.1 Entering Calculations
- 2.1.2 Addition, Subtraction, and Multiplication
- 2.1.3 Division, Integer Division, and Modulo
- 2.1.4 Ranking and Parentheses
- 2.1.5 Exercise
- 2.1.6 Variables and Assignment
- 2.1.7 Exercise
- 2.2 Our First Program
- 2.2.1 Hello World
- 2.2.2 Entering a Program
- 2.3 Saving and Running
- 2.3.1 Saving
- 2.3.2 Running on Windows
- 2.3.3 Running on Ubuntu Linux and on macOS
- 2.3.4 Comments
- 2.3.5 Concatenating Outputs
- 2.3.6 Long Outputs
- 3 Programming Course
- 3.1 Programming a Game
- 3.2 Variables and Operators
- 3.2.1 Assignment, Calculation, and Output
- 3.2.2 String Literals
- 3.2.3 Entering a String
- 3.2.4 Entering a Number
- 3.2.5 Our Game: Version with Input
- 3.2.6 Exercises
- 3.2.7 Random Numbers
- 3.2.8 Determining a Type
- 3.3 Branches
- 3.3.1 Comparison Operators
- 3.3.2 Branch Using if
- 3.3.3 Our Game: Version with Input Evaluation
- 3.3.4 Exercise
- 3.3.5 Multiple Branches
- 3.3.6 Exercise
- 3.3.7 Conditional Expression
- 3.3.8 Logical Operators
- 3.3.9 Exercises
- 3.3.10 Multiple Comparison Operators
- 3.3.11 Our Game: Version with Exact Evaluation of the Input
- 3.3.12 Branch with match
- 3.3.13 Order of Operations
- 3.4 Loops
- 3.4.1 Loops with for
- 3.4.2 Terminating a Loop Using break
- 3.4.3 Continuing a Loop Using continue
- 3.4.4 Nested Control Structures
- 3.4.5 Our Game: Version with for Loop and Abort
- 3.4.6 Loops with for and range()
- 3.4.7 Exercises
- 3.4.8 Our Game: Version with range()
- 3.4.9 Loop with while
- 3.4.10 Our Game: Version with while Loop and Counter
- 3.4.11 Exercise
- 3.4.12 Combined Assignment Expressions
- 3.5 Developing a Program
- 3.6 Errors and Exceptions
- 3.6.1 Base Program
- 3.6.2 Intercepting Errors
- 3.6.3 Outputting an Error Message
- 3.6.4 Repeating an Entry
- 3.6.5 Exercise
- 3.6.6 Our Game: Version with Exception Handling
- 3.7 Functions and Modules
- 3.7.1 Simple Functions
- 3.7.2 Functions with One Parameter
- 3.7.3 Exercise
- 3.7.4 Functions with Multiple Parameters
- 3.7.5 Functions with Return Value
- 3.7.6 Our Game: Version with Functions
- 3.7.7 Exercises
- 3.8 Type Hints
- 3.8.1 Variables and Type Checks
- 3.8.2 Functions and Type Checks
- 3.9 The Finished Game
- 4 Data Types
- 4.1 Numbers
- 4.1.1 Integers
- 4.1.2 Numbers with Decimal Places
- 4.1.3 Exponential Operator **
- 4.1.4 Rounding and Conversion
- 4.1.5 Trigonometric Functions
- 4.1.6 Other Mathematical Functions
- 4.1.7 More Precise Figures with Decimal Places
- 4.1.8 Complex Numbers
- 4.1.9 Bit Operators
- 4.1.10 Fractions
- 4.2 Character Strings
- 4.2.1 Properties
- 4.2.2 Operators
- 4.2.3 Slices
- 4.2.4 Mutability
- 4.2.5 Searching and Replacing
- 4.2.6 Removing Blank Spaces
- 4.2.7 Decomposing Texts
- 4.2.8 Constants
- 4.2.9 The bytes Data Type
- 4.3 Lists
- 4.3.1 Properties and Operators
- 4.3.2 Multidimensional Lists
- 4.3.3 Mutability
- 4.3.4 Methods
- 4.3.5 List Comprehension
- 4.4 Tuples
- 4.5 Dictionaries
- 4.5.1 Properties, Operators, and Methods
- 4.5.2 Dynamic Views
- 4.6 Sets
- 4.6.1 Properties, Operators, and Methods
- 4.6.2 Set Theory
- 4.7 Truth Values and Nothing
- 4.7.1 Truth Values “True” and “False”
- 4.7.2 Nothing and the None Keyword
- 4.8 Reference, Identity, and Copy
- 4.8.1 Reference and Identity
- 4.8.2 Saving Resources
- 4.8.3 Copying Objects
- 4.9 Type Hints
- 4.9.1 A Sequence as a Parameter
- 4.9.2 Restriction to a List
- 5 Advanced Programming
- 5.1 General Remarks
- 5.1.1 Combined Assignment Operators
- 5.1.2 Statement in Multiple Lines
- 5.1.3 Input with Assistance
- 5.1.4 The pass Statement
- 5.2 Output and Formatting
- 5.2.1 The print() Function
- 5.2.2 Formatting Numbers with Decimal Places
- 5.2.3 Formatting Integers
- 5.2.4 Formatting Strings
- 5.2.5 Exercise
- 5.3 Functions for Iterables
- 5.3.1 The zip() Function
- 5.3.2 The map() Function
- 5.3.3 The filter() Function
- 5.4 Encryption
- 5.5 Errors and Exceptions
- 5.5.1 General Remarks
- 5.5.2 Syntax Errors
- 5.5.3 Runtime Errors
- 5.5.4 Logical Errors and Debugging
- 5.5.5 Generating Errors
- 5.5.6 Distinguishing Exceptions
- 5.6 Functions
- 5.6.1 A Variable Number of Parameters
- 5.6.2 Named Parameters
- 5.6.3 Optional Parameters
- 5.6.4 Multiple Return Values
- 5.6.5 Transferring Copies and References
- 5.6.6 Namespaces
- 5.6.7 Recursive Functions
- 5.6.8 Lambda Functions
- 5.6.9 Functions as Parameters
- 5.7 Built-In Functions
- 5.7.1 The max(), min(), and sum() Functions
- 5.7.2 The chr() and ord() Functions
- 5.7.3 The reversed() and sorted() Functions
- 5.8 Other Mathematical Modules
- 5.8.1 Drawing Function Graphs
- 5.8.2 Multiple Partial Drawings
- 5.8.3 One-Dimensional Arrays and Vectors
- 5.8.4 Multidimensional Arrays and Matrices
- 5.8.5 Signal Processing
- 5.8.6 Statistics Functions
- 5.9 Custom Modules
- 5.9.1 Creating Custom Modules
- 5.9.2 Standard Import of a Module
- 5.9.3 Importing and Renaming a Module
- 5.9.4 Importing Functions
- 5.9.5 Exercise
- 5.10 Command Line Parameters
- 5.11 “Fractions Training” Program
- 5.11.1 The Program Flow
- 5.11.2 Main Program
- 5.11.3 An Easy Task
- 5.11.4 A Moderately Difficult Task
- 5.11.5 A Difficult Task
- 6 Object-Oriented Programming
- 6.1 Basic Principles
- 6.1.1 What Is Object-Oriented Programming?
- 6.1.2 Classes, Objects, and Custom Methods
- 6.1.3 Special Members
- 6.1.4 Exercise
- 6.1.5 Operator Methods
- 6.1.6 Reference, Identity, and Copy
- 6.1.7 Concatenation
- 6.1.8 Nesting
- 6.1.9 Exercises
- 6.2 Advanced Topics
- 6.2.1 Inheritance
- 6.2.2 Exercise
- 6.2.3 Multiple Inheritance
- 6.2.4 Data Classes
- 6.2.5 Enumerations
- 6.2.6 Our Game: Object-Oriented Version
- 7 Various Modules
- 7.1 Date and Time
- 7.1.1 The time Module
- 7.1.2 Stopping a Program
- 7.1.3 The timedelta Class
- 7.1.4 The datetime Class
- 7.1.5 The calendar Class
- 7.1.6 Our Game: Version with Time Measurement
- 7.1.7 Our Game: Object-Oriented Version with Time Measurement
- 7.2 Queues
- 7.2.1 The SimpleQueue Class
- 7.2.2 The LifoQueue Class
- 7.2.3 The PriorityQueue Class
- 7.2.4 The deque Class
- 7.3 Multithreading
- 7.3.1 What Is Multithreading Used For?
- 7.3.2 Generating a Thread
- 7.3.3 Identifying a Thread
- 7.3.4 Shared Data and Objects
- 7.3.5 Threads and Exceptions
- 7.4 Regular Expressions
- 7.4.1 Finding Text Parts
- 7.4.2 Replacing Text Parts
- 7.5 Audio Output
- 8 Files
- 8.1 Opening and Closing a File
- 8.2 Text Files
- 8.2.1 Writing a Text File
- 8.2.2 Reading a Text File
- 8.2.3 Writing a CSV File
- 8.2.4 Reading a CSV File
- 8.3 Files with a Defined Structure
- 8.3.1 Formatted Writing
- 8.3.2 Reading in Any Position
- 8.3.3 Writing in Any Position
- 8.4 Serialization Using pickle
- 8.4.1 Writing Objects to Files
- 8.4.2 Reading Objects from Files
- 8.5 Data Exchange Using JSON
- 8.5.1 Writing Objects to Files
- 8.5.2 Reading Objects from Files
- 8.6 Editing Multiple Files
- 8.6.1 The glob.glob() Function
- 8.6.2 The os.scandir() Function
- 8.7 Information about Files
- 8.8 Managing Files and Directories
- 8.9 The Morse Code Sample Project
- 8.9.1 Reading Morse Code from a File
- 8.9.2 Output on the Screen
- 8.9.3 Output with Sound Signals
- 8.10 Our Game: Version with Highscore File
- 8.10.1 Sample Entries
- 8.10.2 Structure of the Program
- 8.10.3 The Code of the Program
- 8.11 Our Game: Object-Oriented Version with Highscore File
- 9 Databases
- 9.1 Structure of Databases
- 9.2 SQLite
- 9.2.1 Database, Table, and Data Records
- 9.2.2 Displaying Data
- 9.2.3 Selecting Data
- 9.2.4 The LIKE Operator
- 9.2.5 Sorting the Output
- 9.2.6 Selection after Entry
- 9.2.7 Changing Data Records
- 9.2.8 Deleting Data Records
- 9.2.9 Exercise
- 9.3 MySQL
- 9.3.1 XAMPP and Connector/Python
- 9.3.2 Database, Table, and Data Records
- 9.3.3 Displaying Data
- 9.4 Our Game: Version with Highscore Database
- 9.5 Our Game: Object-Oriented Version with Highscore Database
- 10 User Interfaces
- 10.1 Introduction
- 10.1.1 Our First GUI Program
- 10.1.2 Setup and Arrangement
- 10.1.3 Event Methods for Multiple Widgets
- 10.2 Widget Types
- 10.2.1 Single-Line Input Field
- 10.2.2 Hidden Input and Deactivating a Widget
- 10.2.3 Multiline Input Field
- 10.2.4 List Box with Simple Selection
- 10.2.5 List Box with Multiple Selection Option
- 10.2.6 Spin Boxes
- 10.2.7 Radio Buttons and the Widget Variable
- 10.2.8 Check Buttons
- 10.2.9 Sliders and Scales
- 10.3 Images and Mouse Events
- 10.3.1 Embedding and Changing an Image
- 10.3.2 Mouse Events
- 10.4 The place Geometry Manager
- 10.4.1 Window Size and Absolute Position
- 10.4.2 Relative Position
- 10.4.3 Changing the Position
- 10.5 Menus and Dialog Boxes
- 10.5.1 Menu Bars
- 10.5.2 Context Menus
- 10.5.3 Standard Dialog Boxes
- 10.5.4 Custom Dialog Boxes
- 10.6 Drawings and Animations
- 10.6.1 Various Drawing Objects
- 10.6.2 Controlling Drawing Objects
- 10.6.3 Animating Drawing Objects
- 10.6.4 Collision of Drawing Objects
- 10.7 Our Game: GUI Version
- 10.8 Exercise
- 11 User Interfaces with PyQt
- 11.1 Introduction
- 11.1.1 Our First GUI Program
- 11.1.2 Setup and Arrangement
- 11.1.3 Size of the Application Window
- 11.1.4 Event Methods for Multiple Widgets
- 11.2 Widget Types
- 11.2.1 Single-Line Input Field
- 11.2.2 Hidden Inputs and Disabling Widgets
- 11.2.3 Multiline Input Field
- 11.2.4 List with a Single Selection Option
- 11.2.5 List with Multiple Selection Options
- 11.2.6 Combo Boxes
- 11.2.7 Spin Boxes
- 11.2.8 Radio Buttons
- 11.2.9 Multiple Groups of Radio Buttons
- 11.2.10 Checkboxes
- 11.2.11 Sliders
- 11.2.12 Images, Formats, and Hyperlinks
- 11.2.13 Standard Dialog Boxes
- 11.3 PyQt and SQLite
- 11.3.1 The User Interface of the Application
- 11.3.2 Widgets on the Left
- 11.3.3 Widgets in the Middle
- 11.3.4 Widgets in the Right
- 11.3.5 List Widget and Python List
- 11.3.6 Database File and Window
- 11.3.7 Three Auxiliary Methods
- 11.3.8 Display in the List Widget
- 11.3.9 Selection in a List Widget
- 11.3.10 Inserting a New Data Record
- 11.3.11 Changing a Data Record
- 11.3.12 Deleting a Data Record
- 11.3.13 Searching for a Name
- 11.3.14 Main Program
- 11.4 Exercise
- Appendices
- A Miscellaneous Topics
- A.1 Installing Additional Modules
- A.2 Creating Executable Files
- A.3 Installing XAMPP
- A.4 UNIX Commands
- A.5 Tips for Development
- B The Author
- Index