A Beginners Guide to Python 3 Programming

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

This textbook is aimed at readers who have little or no knowledge of computer programming but want to learn to program in Python. It starts from the very basics including how to install your Python environment, how to write a very simple program and run it, what a variable is, what an if statement is, how iteration works using for and while loops as well as important key concepts such as functions, classes and modules. Each subject area is prefaced with an introductory chapter, before continuing with how these ideas work in Python.

The second edition has been completely updated for the latest versions of Python including Python 3.11 and Python 3.12. New chapters have been added such as those that consider where and how Python is used, the use of Frozensets, how data can be sorted, enumerated types in Python, structural pattern matching and how (and why) Python Virtual Environments are configured. A new chapter ‘The Python Bites back’ is introduced to present the fourteen most common / biggest gotchas for someone new to Python. Other sections have been updated with new features such as Exception Groups, string operations and dictionary operations.

A Beginners Guide to Python 3 Programming second Edition provides all you need to know about Python, with numerous examples provided throughout including several larger worked case studies illustrating the ideas presented in the previous chapters.


Author(s): John Hunt
Series: Undergraduate Topics in Computer Science
Edition: 2
Publisher: Springer
Year: 2023

Language: English
Commentary: Publisher PDF
Pages: 555
City: Cham
Tags: Python; Python Programming Language; Object-Oriented Programming; Functional Programming; Concurrency; Databases

Preface the Second Edition
Preface to the First Edition
Chapter Organization
What You Need
Using an IDE
Conventions
Example Code and Sample Solutions
Contents
1 Introduction
1.1 What is Python?
1.2 Python Versions
1.3 Python History
1.4 Python Programming
1.5 Python Libraries
1.6 Python Execution Model
1.7 Running Python Programs
1.7.1 Interactively Using the Python Interpreter
1.7.2 Running a Python File
1.7.3 Executing a Python Script
1.7.4 Using Python in an IDE
1.8 Jupyter Notebooks
1.9 Useful Resources
2 Where is Python Used
2.1 Introduction
2.1.1 Data Analytics
2.1.2 Machine Learning and AI
2.1.3 Database Work
2.1.4 Python for Animation
2.1.5 Python for Film Making
2.1.6 Cross Platform UIs
2.1.7 Games Programming
2.1.8 Integration Testing Framework
2.1.9 Academic Research
2.1.10 Web Services
2.2 Useful Resources
3 Setting up the Python Environment
3.1 Introduction
3.2 Check to See if Python is Installed
3.3 Installing Python on a Windows PC
3.3.1 Step 1: Downloading Python
3.3.2 Step 2: Running the Installer
3.3.3 Step 3: Set up the PATH (Optional)
3.3.4 Step 4: Verify the Installation
3.3.5 Step 5: Run Some Python
3.3.6 Step 6: Exit the Python Interpreter
3.4 Setting up on a Mac
3.5 Setting up an IDE
3.5.1 Downloading the PyCharm IDE
3.5.2 Setting up the IDE
3.6 Writing Python Programs
3.7 A Note on Filenames
3.8 Online Resources
4 A First Python Program
4.1 Introduction
4.2 Hello World
4.3 Interactive Hello World
4.4 Variables
4.5 Naming Conventions
4.6 Assignment Operator
4.7 Python Statements
4.8 Comments in Code
4.9 Scripts Versus Programs
4.10 Online Resources
4.11 Exercises
5 Python Strings
5.1 Introduction
5.2 What Are Strings?
5.3 Representing Strings
5.4 What Type is String?
5.5 What Can You Do with Strings?
5.5.1 String Concatenation
5.5.2 Length of a String
5.5.3 Accessing a Character
5.5.4 Accessing a Subset of Characters
5.5.5 Repeating Strings
5.5.6 Splitting Strings
5.5.7 Counting Strings
5.5.8 Replacing Strings
5.5.9 Finding Sub Strings
5.5.10 Converting Other Types into Strings
5.5.11 Remove Prefix and Suffix
5.5.12 Comparing Strings
5.5.13 Other String Operations
5.6 Hints on Strings
5.6.1 Python Strings are Case Sensitive
5.6.2 Function/Method Names
5.6.3 Function/Method Invocations
5.7 String Formatting
5.8 String Templates
5.9 Formatting Using f-strings
5.10 Online Resources
5.11 Exercises
6 Numbers, Booleans and None
6.1 Introduction
6.2 Types of Numbers
6.3 Integers
6.3.1 Converting to Ints
6.4 Floating Point Numbers
6.4.1 Converting to Floats
6.4.2 Converting an Input String into a Floating Point Number
6.5 Complex Numbers
6.6 Boolean Values
6.7 Arithmetic Operators
6.7.1 Integer Operations
6.7.2 Negative Number Integer Division
6.7.3 Floating Point Number Operators
6.7.4 Integers and Floating Point Operations
6.7.5 Complex Number Operators
6.8 Assignment Operators
6.9 None Value
6.10 Online Resources
6.11 Exercises
6.11.1 General Exercise
6.11.2 Convert Kilometers to Miles
7 Flow of Control Using if Statements
7.1 Introduction
7.2 Comparison Operators
7.3 Logical Operators
7.4 The If Statement
7.4.1 Working with an If Statement
7.4.2 Else in an If Statement
7.4.3 The Use of elif
7.5 Nesting if Statements
7.6 If Expressions
7.7 A Note on True and False
7.8 Hints
7.9 Online Resources
7.10 Exercises
7.10.1 Check Input is Positive or Negative
7.10.2 Test if a Number is Odd or Even
7.10.3 Kilometers to Miles Converter
8 Iteration/Looping
8.1 Introduction
8.2 While Loop
8.3 For Loop
8.3.1 Python for Loops
8.3.2 Looping by Non-default Increments
8.3.3 Anonymous Loop Variables
8.4 Negative Loops
8.5 Break Loop Statement
8.6 Continue Loop Statement
8.7 For Loop with Else
8.8 A Note on Loop Variable Naming
8.9 Dice Roll Game
8.10 Online Resources
8.11 Exercises
8.11.1 Calculate the Factorial of a Number
8.11.2 Print All the Prime Numbers in a Range
9 Number Guessing Game
9.1 Introduction
9.2 Setting up the Program
9.2.1 Create a New Python File
9.2.2 Add a Welcome Message
9.2.3 Running the Program
9.3 What Will the Program Do?
9.4 Creating the Game
9.4.1 Generate the Random Number
9.4.2 Obtain an Input From the User
9.4.3 Check to See if the Player Has Guessed the Number
9.4.4 Check They Haven’t Exceeded Their Maximum Number of Guess
9.4.5 Notify the Player Whether Higher or Lower
9.4.6 End of Game Status
9.5 The Complete Listing
9.6 Hints
9.6.1 Initializing Variables
9.6.2 Blank Lines Within a Block of Code
9.7 Exercises
10 Recursion
10.1 Introduction
10.2 Recursive Behaviour
10.3 Benefits of Recursion
10.4 Recursively Searching a Tree
10.5 Recursion in Python
10.6 Calculating Factorial Recursively
10.7 Disadvantages of Recursion
10.8 Online Resources
10.9 Exercises
11 Introduction to Structured Analysis
11.1 Introduction
11.2 Structured Analysis and Function Identification
11.3 Functional Decomposition
11.3.1 Functional Decomposition Terminology
11.3.2 Functional Decomposition Process
11.3.3 Calculator Functional Decomposition Example
11.4 Functional Flow
11.5 Data Flow Diagrams
11.6 Flowcharts
11.7 Data Dictionary
11.8 Online Resources
12 Functions in Python
12.1 Introduction
12.2 What are Functions?
12.3 How Functions Work
12.4 Types of Functions
12.5 Defining Functions
12.5.1 An Example Function
12.6 Returning Values from Functions
12.7 Docstring
12.8 ReStructured Text
12.9 Function Parameters
12.9.1 Multiple Parameter Functions
12.9.2 Default Parameter Values
12.9.3 Named Arguments
12.9.4 Arbitrary Arguments
12.9.5 Positional and Keyword Arguments
12.10 Anonymous Functions
12.11 Online Resources
12.12 Exercises
13 Scope and Lifetime of Variables
13.1 Introduction
13.2 Local Variables
13.3 The Global Keyword
13.4 Nonlocal Variables
13.5 Hints
13.6 Online Resources
13.7 Exercise
14 Implementing a Calculator Using Functions
14.1 Introduction
14.2 What the Calculator Will Do
14.3 Getting Started
14.4 The Calculator Operations
14.5 Behaviour of the Calculator
14.6 Identifying Whether the User has Finished
14.7 Selecting the Operation
14.8 Obtaining the Input Numbers
14.9 Determining the Operation to Execute
14.10 Running the Calculator
14.11 Exercises
15 Introduction to Functional Programming
15.1 Introduction
15.2 What is Functional Programming?
15.3 Advantages to Functional Programming
15.4 Disadvantages of Functional Programming
15.5 Referential Transparency
15.6 Further Reading
16 Higher-Order Functions
16.1 Introduction
16.2 Recap on Functions in Python
16.3 Functions as Objects
16.4 Higher-Order Function Concepts
16.4.1 Higher-Order Function Example
16.5 Python Higher-Order Functions
16.5.1 Using Higher-Order Functions
16.5.2 Functions Returning Functions
16.6 Online Resources
16.7 Exercises
17 Curried Functions
17.1 Introduction
17.2 Currying Concepts
17.3 Python and Curried Functions
17.4 Closures
17.5 Online Resources
17.6 Exercises
18 Introduction to Object Orientation
18.1 Introduction
18.2 Classes
18.3 What Are Classes for?
18.3.1 What Should a Class Do?
18.3.2 Class Terminology
18.4 How is an OO System Constructed?
18.4.1 Where Do We Start?
18.4.2 Identifying the Objects
18.4.3 Identifying the Services or Methods
18.4.4 Refining the Objects
18.4.5 Bringing It All Together
18.5 Where Is the Structure in an OO Program?
18.6 Further Readings
19 Python Classes
19.1 Introduction
19.2 Python and Classes
19.3 Class Definitions
19.4 Creating Examples of the Class Person
19.5 Be Careful with Assignment
19.6 Printing Out Objects
19.6.1 Accessing Object Attributes
19.6.2 Defining a Default String Representation
19.6.3 Defining a Default Storage Representation
19.7 Providing a Class Comment
19.8 Adding a Birthday Method
19.9 Defining Instance Methods
19.10 Person Class Recap
19.11 The Del Keyword
19.12 Automatic Memory Management
19.13 Intrinsic Attributes
19.14 Online Resources
19.15 Exercises
20 Class Side and Static Behaviour
20.1 Introduction
20.2 Class-Side Data
20.3 Class-Side Methods
20.3.1 Why Class-Side Methods?
20.4 Static Methods
20.5 Hints
20.6 Online Resources
20.7 Exercises
21 Class Inheritance
21.1 Introduction
21.2 What is Inheritance?
21.3 Defining Inheritance Between Classes
21.4 Terminology Around Inheritance
21.4.1 Types of Hierarchy
21.5 The Class Object and Inheritance
21.6 The Built-in Object Class
21.7 Purpose of Subclasses
21.8 Overriding Methods
21.9 Extending Superclass Methods
21.10 Inheritance-Oriented Naming Conventions
21.11 Python and Multiple Inheritance
21.12 Multiple Inheritance Considered Harmful
21.13 Summary
21.14 Online Resources
21.15 Exercises
22 Why Bother with Object Orientation?
22.1 Introduction
22.2 The Procedural Approach
22.2.1 Procedures for the Data Structure
22.2.2 Packages
22.3 Does Object Orientation Do Any Better?
22.3.1 Packages Versus Classes
22.3.2 Inheritance
22.4 Summary
23 Operator Overloading
23.1 Introduction
23.2 Operator Overloading
23.2.1 Why Have Operator Overloading?
23.2.2 Why not Have Operator Overloading?
23.2.3 Implementing Operator Overloading
23.3 Numerical Operators
23.4 Comparison Operators
23.5 Logical Operators
23.6 Summary
23.7 Online Resources
23.8 Exercises
24 Python Properties
24.1 Introduction
24.2 Python Attributes
24.3 Setter and Getter Style Methods
24.4 Public Interface to Properties
24.5 More Concise Property Definitions
24.6 Online Resources
24.7 Exercises
25 Abstract Base Classes
25.1 Introduction
25.2 Abstract Classes as a Concept
25.3 Abstract Base Classes in Python
25.3.1 Subclassing an ABC
25.3.2 Defining an Abstract Base Class
25.4 Defining an Interface
25.5 Virtual Subclasses
25.6 Mixins
25.7 Online Resources
26 Error and Exception Handling
26.1 Introduction
26.2 Errors and Exceptions
26.3 What is an Exception?
26.4 What is Exception Handling?
26.5 Handling an Exception
26.5.1 Accessing the Exception Object
26.5.2 Jumping to Exception Handlers
26.5.3 Catch Any Exception
26.5.4 The Else Clause
26.5.5 The Finally Clause
26.6 Raising an Exception
26.7 Defining an Custom Exception
26.8 Chaining Exceptions
26.9 Exception Groups
26.10 Exception Group Handling
26.11 Exception Notes
26.12 Generate a Traceback Programmatically
26.13 Online Resources
26.14 Exercises
27 Python Modules and Packages
27.1 Introduction
27.2 Modules
27.3 Python Modules
27.4 Importing Python Modules
27.4.1 Importing a Module
27.4.2 Importing from a Module
27.4.3 Importing Aliases
27.4.4 Hiding Some Elements of a Module
27.4.5 Importing Within a Function
27.5 Module Properties
27.6 Standard Modules
27.7 Python Module Search Path
27.8 Modules as Scripts
27.9 Python Packages
27.9.1 Package Organization
27.9.2 Sub Packages
27.10 Online Resources
27.11 Exercise
28 Protocols, Polymorphism and Descriptors
28.1 Introduction
28.2 Implicit Contracts
28.3 Duck Typing
28.4 Polymorphism
28.5 Protocols
28.6 The Length Protocol
28.7 The Sequence Protocol
28.8 The Context Manager Protocol
28.9 The Descriptor Protocol
28.10 Online Resources
28.11 Exercises
29 Decorators
29.1 Introduction
29.2 What Are Decorators?
29.3 Defining a Decorator
29.4 Using Decorators
29.5 Functions with Parameters
29.6 Stacked Decorators
29.7 Parameterized Decorators
29.8 Method Decorators
29.8.1 Methods Without Parameters
29.8.2 Methods with Parameters
29.9 Class Decorators
29.10 When is a Decorator Executed?
29.11 Built-In Decorators
29.12 FuncTools Wrap
29.13 Online Resources
29.14 Book Reference
29.15 Exercises
30 Iterables and Iterators
30.1 Introduction
30.2 Iteration
30.2.1 Iterables
30.2.2 Iterators
30.2.3 The Iteration Related Methods
30.2.4 The Iterable Evens Class
30.2.5 Using the Evens Class with a For Loop
30.3 The Itertools Module
30.4 Online Resources
31 Generators and Coroutines
31.1 Introduction
31.2 Generators
31.2.1 Defining a Generator Function
31.2.2 Using a Generator Function in a for Loop
31.2.3 When Do the Yield Statements Execute?
31.2.4 An Even Number Generator
31.2.5 Nesting Generator Functions
31.2.6 Using Generators Outside a for Loop
31.3 Coroutines
31.4 Exercises
32 Collections, Tuples and Lists
32.1 Introduction
32.2 Python Collection Types
32.3 Characterising Collections
32.4 Tuples
32.4.1 Creating Tuples
32.4.2 The Tuple() Constructor Function
32.4.3 Accessing Elements of a Tuple
32.4.4 Creating New Tuples from Existing Tuples
32.4.5 Tuples Can Hold Different Types
32.4.6 Iterating Over Tuples
32.4.7 Tuple Related Functions
32.4.8 Checking if an Element Exists
32.4.9 Nested Tuples
32.4.10 Things You Can’t Do with Tuples
32.5 Lists
32.5.1 Creating Lists
32.5.2 List Constructor Function
32.5.3 Accessing Elements from a List
32.5.4 Adding to a List
32.5.5 Inserting into a List
32.5.6 List Concatenation
32.5.7 Removing from a List
32.5.8 The Pop() Method
32.5.9 Deleting from a List
32.5.10 List Methods
32.6 Online Resources
32.7 Exercises
33 Sets
33.1 Introduction
33.2 Creating a Set
33.3 The Set() Constructor Function
33.4 Accessing Elements in a Set
33.5 Working with Sets
33.5.1 Checking for Presence of an Element
33.5.2 Adding Items to a Set
33.5.3 Changing Items in a Set
33.5.4 Obtaining the Length of a Set
33.5.5 Obtaining the Max and Min Values in a Set
33.5.6 Removing an Item
33.6 Nesting Lists Within Sets
33.7 Set Operations
33.8 Set Methods
33.9 Online Resources
33.10 Exercises
34 Dictionaries
34.1 Introduction
34.2 What is a Dictionary
34.3 Creating a Dictionary
34.3.1 The dict() Constructor Function
34.4 Working with Dictionaries
34.4.1 Accessing Items via Keys
34.4.2 Adding a New Entry
34.4.3 Changing a Keys Value
34.4.4 Removing an Entry
34.4.5 Iterating Over Keys
34.4.6 Values, Keys and Items
34.4.7 Checking Key Membership
34.4.8 Obtaining the Length of a Dictionary
34.4.9 Nesting Dictionaries
34.4.10 Checking to See if a Key is Present
34.5 Dictionary Operators Added in 3.9
34.6 A Note on Dictionary Key Objects
34.7 Dictionary Methods
34.8 Online Resources
34.9 Exercises
35 Frozensets
35.1 Introduction
35.2 Frozenset Characteristics
35.3 Creating a Frozenset
35.4 Frozenset Operations
35.5 Using Frozensets with Sets
36 Collection Related Modules
36.1 Introduction
36.2 List Comprehension
36.3 The Collections Module
36.3.1 The Counter Container
36.3.2 The Defaultdict Container
36.3.3 The Namedtuple Container
36.4 The Itertools Module
36.5 Online Resources
36.6 Exercises
37 ADTs, Queues and Stacks
37.1 Introduction
37.2 Abstract Data Types
37.3 Data Structures
37.4 Queues
37.4.1 Python List as a Queue
37.4.2 Defining a Queue Class
37.5 Stacks
37.5.1 Python List as a Stack
37.6 Online Resources
37.7 Exercises
38 Map, Filter and Reduce
38.1 Introduction
38.2 Filter
38.3 Map
38.4 Reduce
38.5 Online Resources
38.6 Exercises
39 Sorting and Higher-Order Functions
39.1 Introduction
39.2 The sorted() Function
39.3 Sorting in Reverse Order
39.4 The sort() Method
39.5 Reversing the sort() Method
39.6 Changing the Sort Key
39.7 Sorting Containers of Containers
39.8 Exercises
40 Python Enumerated Values
40.1 Introduction
40.2 What is an Enumeration?
40.3 Creating an Enumeration Using Class Syntax
40.4 Enum Values
40.5 Using the Name and Value Attributes
40.6 Creating an Enumeration Using the Function Call Syntax
40.7 Creating Enumerations from Automatic Values
40.8 Working with Enum Aliases
40.9 Enumerations Unique Values
40.10 Iterating Through Enumeration Values
40.11 Comparing Enumerations
40.12 Integer Enumerations with IntEnum
40.13 Exercises
41 Structural Pattern Matching
41.1 Introduction
41.2 Basic Pattern Matching
41.3 Default Case
41.4 Capture Default Case
41.5 Alternative Values in Case Condition
41.6 Matching Sequences/Containers
41.7 Structural Matching and Lists
41.8 Matching Alternative Sequences/Containers
41.9 Matching Multiple Options Within a Container
41.10 Guards on a Case Condition
41.11 Exercises
42 Python Virtual Environments
42.1 Introduction
42.2 Third-Party Libraries
42.3 The Pip Tool
42.3.1 Finding the Pip Version
42.3.2 Installing Pip
42.3.3 Updating Pip
42.4 Default Installation
42.5 Pip Virtual Environments
42.6 Installing Modules
42.7 Listing and Freezing on Modules
42.7.1 The pip list Command
42.7.2 The pip inspect Command
42.7.3 The pip freeze Command
42.8 Uninstalling Modules
42.9 Module Path Search Order
42.10 Using PyCharm with Virtual Environments
42.11 Online Resources
43 Monkey Patching
43.1 Introduction
43.2 What is Monkey Patching?
43.2.1 How Does Monkey Patching Work?
43.2.2 Monkey Patching Example
43.2.3 The Self Parameter
43.2.4 Adding New Data to a Class
43.3 Online Resources
44 Attribute Lookup
44.1 Introduction
44.2 Attributes in Python
44.3 Handling Unknown Attribute Access
44.4 Handling Unknown Method Invocations
44.5 Intercepting Attribute Lookup
44.6 Intercepting Setting an Attribute
44.7 Exercises
45 The Python Bites Back
45.1 Fourteen Beginner Mistakes to Avoid in Python
45.2 Mistake #1: Indentation
45.3 Mistake #2: Case Sensitivity
45.4 Mistake #3: Default Initialization of Function/Method Parameters with Mutable Types
45.5 Mistake #4: Default Initialization of Function/Method Parameters Using a Function
45.6 Mistake #5: Out by 1 Ranges
45.7 Mistake #5: Assignment Operator Versus Equality Operator
45.8 Mistake #6: Falsey and Truthy
45.9 Mistake #7: Hiding Built-In Functions and Classes
45.10 Mistake #8: Hiding Built-In Modules
45.11 Mistake #9: Function Reference Versus Function Call
45.12 Mistake #10: Modifying a List While Iterating Over It
45.13 Mistake #11: Sets Can Only Contain Immutable Objects
45.14 Mistake #12: Local Versus Global Variables
45.15 Mistake #13: Referencing Nonlocal Variable
45.16 Mistake #14: Copying a Mutable Type
46 TicTacToe Game
46.1 Introduction
46.2 Classes in the Game
46.3 Counter Class
46.4 Move Class
46.5 The Player Class
46.6 The HumanPlayer Class
46.7 The ComputerPlayer Class
46.8 The Board Class
46.9 The Game Class
46.10 Running the Game