# Intermediate Python 9781501521744

This book provides you with relevant information about using intermediate Python 3.x for a variety of topics, such as co

218 9 7MB

English Pages 196 Year 2023

Cover
Title Page
Dedication
Contents
Preface
Chapter 1: Data Structures in Python
Working with Lists
Lists and Basic Operations
Reversing and Sorting a List
Lists and Arithmetic Operations
Lists and Filter-Related Operations
Calculating Squares and Cubes in Lists
Sorting Lists of Numbers and Strings
Concatenating a List of Words
The Python range() Function
Counting Digits and Uppercase and Lowercase Letters
Lists and the append() Function
Working with Lists and the split() Function
Counting Words in a List
Iterating Through Pairs of Lists
List Slices
Other List-Related Functions
Working with Vectors
Working with Matrices
Queues
Tuples (Immutable Lists)
Sets
Dictionaries
Creating a Dictionary
Displaying the Contents of a Dictionary
Checking for Keys in a Dictionary
Deleting Keys from a Dictionary
Iterating Through a Dictionary
Interpolating Data from a Dictionary
Dictionary Functions and Methods
Ordered Dictionaries
Sorting Dictionaries
Dictionary Formatting
Multiple Dictionaries
Other Sequence Types
Mutable and Immutable Types
Packing/Unpacking Sequences
Automatic Packing (Direct Assignment)
Unpacking Return Values of Functions
Swapping Pairs of Values
Iterating Sequences in Loops
Serialize and Deserialize Data
Modules versus Packages
User-Defined Functions
Functions versus Methods
Functions with Generic Arguments
Functions that Specify *args
Functions that Specify **kwargs
Summary
Chapter 2: Comprehensions, Iterators, and Generators
Lambda Expressions
Comprehensions
Magic Methods (Dunders)
The Iterator Protocol
The iter() Function and __iter__() Method
Dictionaries and Iterators
Examples of Iterators
Range versus a List
Functional Programming: the map() Function
Functional Programming: the filter() Function
Combining the filter() and map() Functions
The reduce() Function
What is a Pipe?
Working with Generators
The Yield Keyword
Generators and Comprehensions
A Generator Without a Loop
Miscellaneous Examples of Generators
Generate Squares of Numbers
Generate an Infinite List of Integers
Find Prime Numbers
Closures
Decorators
Examples of Decorators
Importing Custom Modules
Compiled Modules
Classes, Functions, and Methods
Function Annotations
Function Annotations (1)
Function Annotations (2)
Function Annotations (3)
Function Annotations (4)
Function Annotations (5)
Summary
Chapter 3: Regular Expressions
What are Regular Expressions?
Metacharacters
Character Sets
Working with “^” and “\”
Character Classes
Matching Character Classes with the re Module
Compilation Flags
Using the re.match() Method
Options for the re.match() Method
Matching Character Classes with the re.search() Method
Matching Character Classes with the findAll() Method
Finding Capitalized Words in a String
Additional Matching Functions for Regular Expressions
Grouping with Character Classes in Regular Expressions
Using Character Classes in Regular Expressions
Matching Strings with Multiple Consecutive Digits
Reversing Words in Strings
Modifying Text Strings with the re Module
Splitting Text Strings with the re.split() Method
Splitting Text Strings Using Digits and Delimiters
Substituting Text Strings with the re.sub() Method
Matching the Beginning and End of Text Strings
Compound Regular Expressions
Counting Character Types in a String
Regular Expressions and Grouping
Simple String Matches
Summary
Chapter 4: Custom Classes
Accessibility Conventions
Creating Custom Classes
Instance Variables versus Class Variables
Examples of Custom Classes
A Custom Class with an Iterator
A Custom Class with an Invalid Iterator
Construction and Initialization of Objects
Accessors and Mutators versus @property
The Methods __str__() and __repr__()
Creating a Point3D Custom Class
Comparing Two Instances of a Custom Class
Creating an Employee Custom Class
Working with a List of Employees
A Python Iterable Class
Custom Classes and Dictionaries
Custom Classes and Priority Queues
The Base Classes of a Given Class
Encapsulation
Single Inheritance
An Example of Inheritance
Inheritance and Overriding Methods
Multiple Inheritance
Polymorphism
The abc Module
Summary
Chapter 5: Queues and Stacks
What is a Queue?
Types of Queues
Creating a Queue Using a Python List
Creating a Rolling Queue
Creating a Shifting Queue using a List
Creating an Iterable Queue
What is a Stack?
Use Cases for Stacks
Operations with Stacks
Working with Stacks
Creating an Iterable Stack
Task: Reverse and Print Stack Values
Task: Display the Min and Max Stack Values
Creating Two Stacks Using an Array
Task: Reverse a String Using a Stack
Infix, Prefix, and Postfix Notations
Summary
Chapter 6: Recursion and Combinatorics
What is Recursion?
Arithmetic Series
Calculating an Arithmetic Series (Iterative)
Calculating an Arithmetic Series (Recursive)
Calculating a Partial Arithmetic Series
Geometric Series
Calculating a Geometric Series (Iterative)
Calculating a Geometric Series (Recursive)
Factorial Values
Calculating Factorial Values (Iterative)
Calculating Factorial Values (Recursive)
Calculating Factorial Values (Tail Recursion)
Fibonacci Numbers
Calculating Fibonacci Numbers (Recursive)
Calculating Fibonacci Numbers (Iterative)
Task: Reverse a String via Recursion
Task: Check for Balanced Parentheses (Iterative)
Task: Calculate the Number of Digits
Task: Determine if a Positive Integer is Prime (Iterative)
Task: Find the Prime Factorization of a Positive Integer (Iterative)