Tapping into the full power of Python doesn’t have to be difficult. Supercharged Python is written for people who’ve learned the fundamentals of the language but want to take their skills to the next level.
Using the example-driven approach featured in Python Without Fear, Brian Overland and John Bennett start with simple examples and carefully build more complex applications. After briefly reviewing the fundamental concepts of Python, the authors carefully explain more advanced topics, including text formatting, regular expressions, and chart plotting.
The book reviews advanced techniques for list and string classes, and then covers all the ways of handling text and binary files; decimal, money, and other special classes; advanced techniques for writing classes; generators and decorators; and the random, math, and Numpy (Numeric Python) packages, which can supercharge your applications.
- Use profilers and “magic methods” to code like a pro
- Harness the power of regular expressions to process text quickly with a single statement
- Take advantage of 22 coding shortcuts, along with performance tips to save time and optimize your code
- Create really useful classes and objects, for games, simulations, money, mathematics, and more
- Use multiple modules to build powerful apps while avoiding the “gotchas”
- Import packages to dramatically speed up statistical operations—by as much as 100 times!
- Refer to the five-part language reference to look up fine points of the language
Supercharged Python demonstrates techniques that allow you to write faster and more powerful code, whether you’re manipulating large amounts of data or building sophisticated applications.
Author(s): Brian Overland; John Bennett
Publisher: Addison-Wesley Professional
Year: 2019
Language: English
Commentary: True PDF
Pages: 633
Tags: Python;Programming;Scripting;Computers;Software Egineering;Computer Science;NumPy;Regular Expressions;OOP;
Cover
Title Page
Copyright Page
Contents
Preface
What Makes Python Special?
Paths to Learning: Where Do I Start?
Clarity and Examples Are Everything
Learning Aids: Icons
What You’ll Learn
Have Fun
Acknowledgments
About the Authors
Chapter 1 Review of the Fundamentals
____1.1 Python Quick Start
____1.2 Variables and Naming Names
____1.3 Combined Assignment Operators
____1.4 Summary of Python Arithmetic Operators
____1.5 Elementary Data Types: Integer and Floating Point
____1.6 Basic Input and Output
____1.7 Function Definitions
____1.8 The Python “if” Statement
____1.9 The Python “while” Statement
____1.10 A Couple of Cool Little Apps
____1.11 Summary of Python Boolean Operators
____1.12 Function Arguments and Return Values
____1.13 The Forward Reference Problem
____1.14 Python Strings
____1.15 Python Lists (and a Cool Sorting App)
____1.16 The “for” Statement and Ranges
____1.17 Tuples
____1.18 Dictionaries
____1.19 Sets
____1.20 Global and Local Variables
____Summary
____Review Questions
____Suggested Problems
Chapter 2 Advanced String Capabilities
____2.1 Strings Are Immutable
____2.2 Numeric Conversions, Including Binary
____2.3 String Operators (+, =, *, >, etc.)
____2.4 Indexing and Slicing
____2.5 Single-Character Functions (Character Codes)
____2.6 Building Strings Using “join”
____2.7 Important String Functions
____2.8 Binary, Hex, and Octal Conversion Functions
____2.9 Simple Boolean (“is”) Methods
____2.10 Case Conversion Methods
____2.11 Search-and-Replace Methods
____2.12 Breaking Up Input Using “split”
____2.13 Stripping
____2.14 Justification Methods
____Summary
____Review Questions
____Suggested Problems
Chapter 3 Advanced List Capabilities
____3.1 Creating and Using Python Lists
____3.2 Copying Lists Versus Copying List Variables
____3.3 Indexing
________3.3.1 Positive Indexes
________3.3.2 Negative Indexes
________3.3.3 Generating Index Numbers Using “enumerate”
____3.4 Getting Data from Slices
____3.5 Assigning into Slices
____3.6 List Operators
____3.7 Shallow Versus Deep Copying
____3.8 List Functions
____3.9 List Methods: Modifying a List
____3.10 List Methods: Getting Information on Contents
____3.11 List Methods: Reorganizing
____3.12 Lists as Stacks: RPN Application
____3.13 The “reduce” Function
____3.14 Lambda Functions
____3.15 List Comprehension
____3.16 Dictionary and Set Comprehension
____3.17 Passing Arguments Through a List
____3.18 Multidimensional Lists
________3.18.1 Unbalanced Matrixes
________3.18.2 Creating Arbitrarily Large Matrixes
____Summary
____Review Questions
____Suggested Problems
Chapter 4 Shortcuts, Command Line, and Packages
____4.1 Overview
____4.2 Twenty-Two Programming Shortcuts
________4.2.1 Use Python Line Continuation as Needed
________4.2.2 Use “for” Loops Intelligently
________4.2.3 Understand Combined Operator Assignment (+= etc.)
________4.2.4 Use Multiple Assignment
________4.2.5 Use Tuple Assignment
________4.2.6 Use Advanced Tuple Assignment
________4.2.7 Use List and String “Multiplication”
________4.2.8 Return Multiple Values
________4.2.9 Use Loops and the “else” Keyword
________4.2.10 Take Advantage of Boolean Values and “not”
________4.2.11 Treat Strings as Lists of Characters
________4.2.12 Eliminate Characters by Using “replace”
________4.2.13 Don’t Write Unnecessary Loops
________4.2.14 Use Chained Comparisons (n < x< m)
________4.2.15 Simulate “switch” with a Table of Functions
________4.2.16 Use the “is” Operator Correctly
________4.2.17 Use One-Line “for” Loops
________4.2.18 Squeeze Multiple Statements onto a Line
________4.2.19 Write One-Line if/then/else Statements
________4.2.20 Create Enum Values with “range”
________4.2.21 Reduce the Inefficiency of the “print” Function Within IDLE
________4.2.22 Place Underscores Inside Large Numbers
____4.3 Running Python from the Command Line
________4.3.1 Running on a Windows-Based System
________4.3.2 Running on a Macintosh System
________4.3.3 Using pip or pip3 to Download Packages
____4.4 Writing and Using Doc Strings
____4.5 Importing Packages
____4.6 A Guided Tour of Python Packages
____4.7 Functions as First-Class Objects
____4.8 Variable-Length Argument Lists
________4.8.1 The *args List
________4.8.2 The “**kwargs” List
____4.9 Decorators and Function Profilers
____4.10 Generators
________4.10.1 What’s an Iterator?
________4.10.2 Introducing Generators
____4.11 Accessing Command-Line Arguments
____Summary
____Questions for Review
____Suggested Problems
Chapter 5 Formatting Text Precisely
____5.1 Formatting with the Percent Sign Operator (%)
____5.2 Percent Sign (%) Format Specifiers
____5.3 Percent Sign (%) Variable-Length Print Fields
____5.4 The Global “format” Function
____5.5 Introduction to the “format” Method
____5.6 Ordering by Position (Name or Number)
____5.7 “Repr” Versus String Conversion
____5.8 The “spec” Field of the “format” Function and Method
________5.8.1 Print-Field Width
________5.8.2 Text Justification: “fill” and “align” Characters
________5.8.3 The “sign” Character
________5.8.4 The Leading-Zero Character (0)
________5.8.5 Thousands Place Separator
________5.8.6 Controlling Precision
________5.8.7 “Precision” Used with Strings (Truncation)
________5.8.8 “Type” Specifiers
________5.8.9 Displaying in Binary Radix
________5.8.10 Displaying in Octal and Hex Radix
________5.8.11 Displaying Percentages
________5.8.12 Binary Radix Example
____5.9 Variable-Size Fields
____Summary
____Review Questions
____Suggested Problems
Chapter 6 Regular Expressions, Part I
____6.1 Introduction to Regular Expressions
____6.2 A Practical Example: Phone Numbers
____6.3 Refining Matches
____6.4 How Regular Expressions Work: Compiling Versus Running
____6.5 Ignoring Case, and Other Function Flags
____6.6 Regular Expressions: Basic Syntax Summary
________6.6.1 Meta Characters
________6.6.2 Character Sets
________6.6.3 Pattern Quantifiers
________6.6.4 Backtracking, Greedy, and Non-Greedy
____6.7 A Practical Regular-Expression Example
____6.8 Using the Match Object
____6.9 Searching a String for Patterns
____6.10 Iterative Searching (“findall”)
____6.11 The “findall” Method and the Grouping Problem
____6.12 Searching for Repeated Patterns
____6.13 Replacing Text
____Summary
____Review Questions
____Suggested Problems
Chapter 7 Regular Expressions, Part II
____7.1 Summary of Advanced RegEx Grammar
____7.2 Noncapture Groups
________7.2.1 The Canonical Number Example
________7.2.2 Fixing the Tagging Problem
____7.3 Greedy Versus Non-Greedy Matching
____7.4 The Look-Ahead Feature
____7.5 Checking Multiple Patterns (Look-Ahead)
____7.6 Negative Look-Ahead
____7.7 Named Groups
____7.8 The “re.split” Function
____7.9 The Scanner Class and the RPN Project
____7.10 RPN: Doing Even More with Scanner
____Summary
____Review Questions
____Suggested Problems
Chapter 8 Text and Binary Files
____8.1 Two Kinds of Files: Text and Binary
________8.1.1 Text Files
________8.1.2 Binary Files
____8.2 Approaches to Binary Files: A Summary
____8.3 The File/Directory System
____8.4 Handling File-Opening Exceptions
____8.5 Using the “with” Keyword
____8.6 Summary of Read/Write Operations
____8.7 Text File Operations in Depth
____8.8 Using the File Pointer (“seek”)
____8.9 Reading Text into the RPN Project
________8.9.1 The RPN Interpreter to Date
________8.9.2 Reading RPN from a Text File
________8.9.3 Adding an Assignment Operator to RPN
____8.10 Direct Binary Read/Write
____8.11 Converting Data to Fixed-Length Fields (“struct”)
________8.11.1 Writing and Reading One Number at a Time
________8.11.2 Writing and Reading Several Numbers at a Time
________8.11.3 Writing and Reading a Fixed-Length String
________8.11.4 Writing and Reading a Variable-Length String
________8.11.5 Writing and Reading Strings and Numerics Together
________8.11.6 Low-Level Details: Big Endian Versus Little Endian
____8.12 Using the Pickling Package
____8.13 Using the “shelve” Package
____Summary
____Review Questions
____Suggested Problems
Chapter 9 Classes and Magic Methods
____9.1 Classes and Objects: Basic Syntax
____9.2 More About Instance Variables
____9.3 The “_ _init_ _” and “_ _new_ _” Methods
____9.4 Classes and the Forward Reference Problem
____9.5 Methods Generally
____9.6 Public and Private Variables and Methods
____9.7 Inheritance
____9.8 Multiple Inheritance
____9.9 Magic Methods, Summarized
____9.10 Magic Methods in Detail
________9.10.1 String Representation in Python Classes
________9.10.2 The Object Representation Methods
________9.10.3 Comparison Methods
________9.10.4 Arithmetic Operator Methods
________9.10.5 Unary Arithmetic Methods
________9.10.6 Reflection (Reverse-Order) Methods
________9.10.7 In-Place Operator Methods
________9.10.8 Conversion Methods
________9.10.9 Collection Class Methods
________9.10.10 Implementing “_ _iter_ _” and “_ _next_ _”
____9.11 Supporting Multiple Argument Types
____9.12 Setting and Getting Attributes Dynamically
____Summary
____Review Questions
____Suggested Problems
Chapter 10 Decimal, Money, and Other Classes
____10.1 Overview of Numeric Classes
____10.2 Limitations of Floating-Point Format
____10.3 Introducing the Decimal Class
____10.4 Special Operations on Decimal Objects
____10.5 A Decimal Class Application
____10.6 Designing a Money Class
____10.7 Writing the Basic Money Class (Containment)
____10.8 Displaying Money Objects (“_ _str_ _”, “_ _repr_ _”)
____10.9 Other Monetary Operations
____10.10 Demo: A Money Calculator
____10.11 Setting the Default Currency
____10.12 Money and Inheritance
____10.13 The Fraction Class
____10.14 The Complex Class
____Summary
____Review Questions
____Suggested Problems
Chapter 11 The Random and Math Packages
____11.1 Overview of the Random Package
____11.2 A Tour of Random Functions
____11.3 Testing Random Behavior
____11.4 A Random-Integer Game
____11.5 Creating a Deck Object
____11.6 Adding Pictograms to the Deck
____11.7 Charting a Normal Distribution
____11.8 Writing Your Own Random-Number Generator
________11.8.1 Principles of Generating Random Numbers
________11.8.2 A Sample Generator
____11.9 Overview of the Math Package
____11.10 A Tour of Math Package Functions
____11.11 Using Special Values (pi)
____11.12 Trig Functions: Height of a Tree
____11.13 Logarithms: Number Guessing Revisited
________11.13.1 How Logarithms Work
________11.13.2 Applying a Logarithm to a Practical Problem
____Summary
____Review Questions
____Suggested Problems
Chapter 12 The “numpy” (Numeric Python) Package
____12.1 Overview of the “array,” “numpy,” and “matplotlib” Packages
________12.1.1 The “array” Package
________12.1.2 The “numpy” Package
________12.1.3 The “numpy.random” Package
________12.1.4 The “matplotlib” Package
____1 2.2 Using the “array” Package
____12.3 Downloading and Importing “numpy”
____12.4 Introduction to “numpy”: Sum 1 to 1 Million
____12.5 Creating “numpy” Arrays
________12.5.1 The “array” Function (Conversion to an Array)
________12.5.2 The “arange” Function
________12.5.3 The “linspace” Function
________12.5.4 The “empty” Function
________12.5.5 The “eye” Function
________12.5.6 The “ones” Function
________12.5.7 The “zeros” Function
________12.5.8 The “full” Function
________12.5.9 The “copy” Function
________12.5.10 The “fromfunction” Function
____12.6 Example: Creating a Multiplication Table
____12.7 Batch Operations on “numpy” Arrays
____12.8 Ordering a Slice of “numpy”
____12.9 Multidimensional Slicing
____12.10 Boolean Arrays: Mask Out That “numpy”!
____12.11 “numpy” and the Sieve of Eratosthenes
____12.12 Getting “numpy” Stats (Standard Deviation)
____1 2.13 Getting Data on “numpy” Rows and Columns
____Summary
____Review Questions
____Suggested Problems
Chapter 13 Advanced Uses of “numpy”
____13.1 Advanced Math Operations with “numpy”
____13.2 Downloading “mat plotlib”
____13.3 Plotting Lines with “numpy” and “matplotlib”
____13.4 Plotting More Than One Line
____13.5 Plotting Compound Interest
____13.6 Creating Histograms with “matplotlib”
____13.7 Circles and the Aspect Ratio
____13.8 Creating Pie Charts
____13.9 Doing Linear Algebra with “numpy”
________13.9.1 The Dot Product
________13.9.2 The Outer-Product Function
________13.9.3 Other Linear Algebra Functions
____13.10 Three-Dimensional Plotting
____13.11 “numpy” Financial Applications
____13.12 Adjusting Axes with “xticks” and “yticks”
____13.13 “numpy” Mixed-Data Records
____13.14 Reading and Writing “numpy” Data from Files
____Summary
____Review Questions
____Suggested Problems
Chapter 14 Multiple Modules and the RPN Example
____14.1 Overview of Modules in Python
____14.2 Simple Two-Module Example
____14.3 Variations on the “import” Statement
____14.4 Using the “_ _all_ _” Symbol
____14.5 Public and Private Module Variables
____14.6 The Main Module and “_ _main_ _”
____14.7 Gotcha! Problems with Mutual Importing
____14.8 RPN Example: Breaking into Two Modules
____14.9 RPN Example: Adding I/O Directives
____14.10 Further Changes to the RPN Example
________14.10.1 Adding Line-Number Checking
________14.10.2 Adding Jump-If-Not-Zero
________14.10.3 Greater-Than (>) and Get-Random-Number (!)
____14.11 RPN: Putting It All Together
____Summary
____Review Questions
____Suggested Problems
Chapter 15 Getting Financial Data off the Internet
____15.1 Plan of This Chapter
____15.2 Introducing the Pandas Package
____15.3 “stock_load”: A Simple Data Reader
____15.4 Producing a Simple Stock Chart
____15.5 Adding a Title and Legend
____15.6 Writing a “makeplot” Function (Refactoring)
____15.7 Graphing Two Stocks Together
____15.8 Variations: Graphing Other Data
____15.9 Limiting the Time Period
____15.10 Split Charts: Subplot the Volume
____15.11 Adding a Moving-Average Line
____15.12 Giving Choices to the User
____Summary
____Review Questions
____Suggested Problems
Appendix A: Python Operator Precedence Table
Appendix B: Built-In Python Functions
____abs(x)
____all(iterable)
____any(iterable)
____ascii(obj)
____bin(n)
____bool(obj)
____bytes(source, encoding)
____callable(obj)
____chr(n)
____compile(cmd_str, filename, mode_str, flags=0, dont_inherit=False, optimize=–1)
____complex(real=0, imag=0)
____complex(complex_str)
____delattr(obj, name_str)
____dir([obj])
____divmod(a, b)
____enumerate(iterable, start=0)
____eval(expr_str [, globals [,locals]] )
____exec(object [, global [,locals]])
____filter(function, iterable)
____float([x])
____format(obj, [format_spec])
____frozenset([iterable])
____getattr(obj, name_str [,default])
____globals()
____hasattr(obj, name_str)
____hash(obj)
____help([obj])
____hex(n)
____id(obj)
____input([prompt_str])
____int(x, base=10)
____int()
____isinstance(obj, class)
____issubclass(class1, class2)
____iter(obj)
____len(sequence)
____list([iterable])
____locals()
____map(function, iterable1 [, iterable2…])
____max(arg1 [, arg2]…)
____max(iterable)
____min(arg1 [, arg2]…)
____min(iterable)
____oct(n)
____open(file_name_str, mode='rt')
____ord(char_str)
____pow(x, y[, z])
____print(objects, sep='', end='\n', file=sys.stdout)
____range(n)
____range(start, stop [, step])
____repr(obj)
____reversed(iterable)
____round(x [,ndigits])
____set([iterable])
____setattr(obj, name_str, value)
____sorted(iterable [, key] [, reverse])
____str(obj='')
____str(obj=b'' [, encoding='utf-8'])
____sum(iterable [, start])
____super(type)
____tuple([iterable])
____type(obj)
____zip(*iterables)
Appendix C: Set Methods
____set_obj.add(obj)
____set_obj.clear()
____set_obj.copy()
____set_obj.difference(other_set)
____set_obj.difference_update(other_set)
____set_obj.discard(obj)
____set_obj.intersection(other_set)
____set_obj.intersection_update(other_set)
____set_obj.isdisjoint(other_set)
____set_obj.issubset(other_set)
____set_obj.issuperset(other_set)
____set_obj.pop()
____set_obj.remove(obj)
____set_obj.symmetric_difference(other_set)
____set_obj.symmetric_difference_update(other_set)
____set_obj.union(other_set)
____set_obj.union_update(other_set)
Appendix D: Dictionary Methods
____dict_obj.clear()
____dict_obj.copy()
____dict_obj.get(key_obj, default_val = None)
____dict_obj.items()
____dict_obj.keys()
____dict_obj.pop(key [, default_value])
____dict_obj.popitem()
____dict_obj.setdefault(key, default_value=None)
____dict_obj.values()
____dict_obj.update(sequence)
Appendix E: Statement Reference
____Variables and Assignments
____Spacing Issues in Python
____Alphabetical Statement Reference
________assert Statement
________break Statement
________class Statement
________continue Statement
________def Statement
________del Statement
________elif Clause
________else Clause
________except Clause
________for Statement
________global Statement
________if Statement
________import Statement
________nonlocal Statement
________pass Statement
________raise Statement
________return Statement
________try Statement
________while Statement
________with Statement
________yield Statement
Index