Home Page Icon
Home Page
Table of Contents for
III. Statements and Syntax
Close
III. Statements and Syntax
by Mark Lutz
Learning Python, 3rd Edition
Dedication
A Note Regarding Supplemental Files
Preface
About This Third Edition
This Edition’s Python Language Changes
This Edition’s Python Training Changes
This Edition’s Structural Changes
This Edition’s Scope Changes
About This Book
This Book’s Prerequisites
This Book’s Scope and Other Books
This Book’s Style and Structure
Book Updates
About the Programs in This Book
Preparing for Python 3.0
About This Series
Using Code Examples
Font Conventions
Safari® Books Online
How to Contact Us
Acknowledgments
I. Getting Started
1. A Python Q&A Session
Why Do People Use Python?
Software Quality
Developer Productivity
Is Python a “Scripting Language”?
OK, but What’s the Downside?
Who Uses Python Today?
What Can I Do with Python?
Systems Programming
GUIs
Internet Scripting
Component Integration
Database Programming
Rapid Prototyping
Numeric and Scientific Programming
Gaming, Images, AI, XML, Robots, and More
What Are Python’s Technical Strengths?
It’s Object Oriented
It’s Free
It’s Portable
It’s Powerful
It’s Mixable
It’s Easy to Use
It’s Easy to Learn
It’s Named After Monty Python
How Does Python Stack Up to Language X?
Chapter Summary
2. How Python Runs Programs
Introducing the Python Interpreter
Program Execution
The Programmer’s View
Python’s View
Byte code compilation
The Python Virtual Machine (PVM)
Performance implications
Development implications
Execution Model Variations
Python Implementation Alternatives
CPython
Jython
IronPython
Execution Optimization Tools
The Psyco just-in-time compiler
The Shedskin C++ translator
Frozen Binaries
Future Possibilities?
Chapter Summary
3. How You Run Programs
Interactive Coding
Using the Interactive Prompt
System Command Lines and Files
Using Command Lines and Files
Unix Executable Scripts (#!)
Clicking File Icons
Clicking Icons on Windows
The raw_input Trick
Other Icon-Click Limitations
Module Imports and Reloads
The Grander Module Story: Attributes
Modules and namespaces
import and reload Usage Notes
The IDLE User Interface
IDLE Basics
Using IDLE
Advanced IDLE Tools
Other IDEs
Embedding Calls
Frozen Binary Executables
Text Editor Launch Options
Other Launch Options
Future Possibilities?
Which Option Should I Use?
Chapter Summary
BRAIN BUILDER
Part I Exercises
II. Types and Operations
4. Introducing Python Object Types
Why Use Built-in Types?
Python’s Core Data Types
Numbers
Strings
Sequence Operations
Immutability
Type-Specific Methods
Getting Help
Other Ways to Code Strings
Pattern Matching
Lists
Sequence Operations
Type-Specific Operations
Bounds Checking
Nesting
List Comprehensions
Dictionaries
Mapping Operations
Nesting Revisited
Sorting Keys: for Loops
Iteration and Optimization
Missing Keys: if Tests
Tuples
Why Tuples?
Files
Other File-Like Tools
Other Core Types
How to Break Your Code’s Flexibility
User-Defined Classes
And Everything Else
Chapter Summary
5. Numbers
Python Numeric Types
Numeric Literals
Built-in Numeric Tools and Extensions
Python Expression Operators
Mixed Operators Follow Operator Precedence
Parentheses Group Subexpressions
Mixed Types Are Converted Up
Preview: Operator Overloading
Numbers in Action
Variables and Basic Expressions
Numeric Display Formats
Division: Classic, Floor, and True
Bitwise Operations
Long Integers
Complex Numbers
Hexadecimal and Octal Notation
Other Built-in Numeric Tools
Other Numeric Types
Decimal Numbers
Sets
Booleans
Third-Party Extensions
Chapter Summary
6. The Dynamic Typing Interlude
The Case of the Missing Declaration Statements
Variables, Objects, and References
Types Live with Objects, Not Variables
Objects Are Garbage-Collected
Shared References
Shared References and In-Place Changes
Shared References and Equality
Dynamic Typing Is Everywhere
Chapter Summary
7. ofsmallStrings
String Literals
Single- and Double-Quoted Strings Are the Same
Escape Sequences Represent Special Bytes
Raw Strings Suppress Escapes
Triple Quotes Code Multiline Block Strings
Unicode Strings Encode Larger Character Sets
Strings in Action
Basic Operations
Indexing and Slicing
Extended slicing: the third limit
String Conversion Tools
Character code conversions
Changing Strings
String Formatting
Advanced String Formatting
Dictionary-Based String Formatting
String Methods
String Method Examples: Changing Strings
String Method Examples: Parsing Text
Other Common String Methods in Action
The Original string Module
General Type Categories
Types Share Operation Sets by Categories
Mutable Types Can Be Changed In-Place
Chapter Summary
8. Lists and Dictionaries
Lists
Lists in Action
Basic List Operations
Indexing, Slicing, and Matrixes
Changing Lists In-Place
Index and slice assignments
List method calls
Other common list operations
Dictionaries
Dictionaries in Action
Basic Dictionary Operations
Changing Dictionaries In-Place
More Dictionary Methods
A Languages Table
Dictionary Usage Notes
Using dictionaries to simulate flexible lists
Using dictionaries for sparse data structures
Avoiding missing-key errors
Using dictionaries as “records”
Other ways to make dictionaries
Chapter Summary
9. Tuples, Files, and Everything Else
Tuples
Tuples in Action
Tuple syntax peculiarities: commas and parentheses
Conversions and immutability
Why Lists and Tuples?
Files
Opening Files
Using Files
Files in Action
Storing and parsing Python objects in files
Storing native Python objects with pickle
Storing and parsing packed binary data in files
Other File Tools
Type Categories Revisited
Object Flexibility
References Versus Copies
Comparisons, Equality, and Truth
The Meaning of True and False in Python
Python’s Type Hierarchies
Other Types in Python
Built-in Type Gotchas
Assignment Creates References, Not Copies
Repetition Adds One Level Deep
Beware of Cyclic Data Structures
Immutable Types Can’t Be Changed In-Place
Chapter Summary
BRAIN BUILDER
Part II Exercises
III. Statements and Syntax
10. Introducing Python Statements
Python Program Structure Revisited
Python’s Statements
A Tale of Two ifs
What Python Adds
What Python Removes
Parentheses are optional
End of line is end of statement
End of indentation is end of block
Why Indentation Syntax?
A Few Special Cases
Statement rule special cases
Block rule special case
A Quick Example: Interactive Loops
A Simple Interactive Loop
Doing Math on User Inputs
Handling Errors by Testing Inputs
Handling Errors with try Statements
Nesting Code Three Levels Deep
Chapter Summary
11. rwordsAssignment, Expressions, and print
Assignment Statements
Assignment Statement Forms
Sequence Assignments
Advanced sequence assignment patterns
Multiple-Target Assignments
Multiple-target assignment and shared references
Augmented Assignments
Augmented assignment and shared references
Variable Name Rules
Naming conventions
Names have no type, but objects do
Expression Statements
Expression Statements and In-Place Changes
print Statements
The Python “Hello World” Program
Redirecting the Output Stream
The print >> file Extension
Chapter Summary
12. if Tests
if Statements
General Format
Basic Examples
Multiway Branching
Python Syntax Rules
Block Delimiters
Statement Delimiters
A Few Special Cases
Truth Tests
The if/else Ternary Expression
Chapter Summary
13. while and for Loops
while Loops
General Format
Examples
break, continue, pass, and the Loop else
General Loop Format
Examples
pass
continue
break
else
More on the loop else clause
for Loops
General Format
Examples
Basic usage
Other data types
Tuple assignment in for
Nested for loops
Iterators: A First Look
File Iterators
Other Built-in Type Iterators
Other Iteration Contexts
User-Defined Iterators
Loop Coding Techniques
Counter Loops: while and range
Nonexhaustive Traversals: range
Changing Lists: range
Parallel Traversals: zip and map
Dictionary construction with zip
Generating Both Offsets and Items: enumerate
List Comprehensions: A First Look
List Comprehension Basics
Using List Comprehensions on Files
Extended List Comprehension Syntax
Chapter Summary
14. The Documentation Interlude
Python Documentation Sources
# Comments
The dir Function
Docstrings: _ _doc_ _
User-defined docstrings
Docstring standards
Built-in docstrings
PyDoc: The help Function
PyDoc: HTML Reports
Standard Manual Set
Web Resources
Published Books
Common Coding Gotchas
Chapter Summary
BRAIN BUILDER
Part III Exercises
IV. Functions
15. Function Basics
Why Use Functions?
Coding Functions
def Statements
def Executes at Runtime
A First Example: Definitions and Calls
Definition
Calls
Polymorphism in Python
A Second Example: Intersecting Sequences
Definition
Calls
Polymorphism Revisited
Local Variables
Chapter Summary
16. Scopes and Arguments
Scope Rules
Python Scope Basics
Name Resolution: The LEGB Rule
Scope Example
The Built-in Scope
The global Statement
Minimize Global Variables
Minimize Cross-File Changes
Other Ways to Access Globals
Scopes and Nested Functions
Nested Scope Details
Nested Scope Examples
Factory functions
Retaining enclosing scopes’ state with defaults
Nested scopes and lambdas
Scopes versus defaults with loop variables
Arbitrary scope nesting
Passing Arguments
Arguments and Shared References
Avoiding Mutable Argument Changes
Simulating Output Parameters
Special Argument-Matching Modes
Keyword and Default Examples
Keywords
Defaults
Arbitrary Arguments Examples
Collecting arguments
Unpacking arguments
Combining Keywords and Defaults
The min Wakeup Call
Full credit
Bonus points
The punch line
A More Useful Example: General Set Functions
Argument Matching: The Gritty Details
Chapter Summary
17. Advanced Function Topics
Anonymous Functions: lambda
lambda Expressions
Why Use lambda?
How (Not) to Obfuscate Your Python Code
Nested lambdas and Scopes
Applying Functions to Arguments
The apply Built-in
Passing keyword arguments
apply-Like Call Syntax
Mapping Functions over Sequences: map
Functional Programming Tools: filter and reduce
List Comprehensions Revisited: Mappings
List Comprehension Basics
Adding Tests and Nested Loops
List Comprehensions and Matrixes
Comprehending List Comprehensions
Iterators Revisited: Generators
Generator Function Example
Extended Generator Function Protocol: send Versus next
Iterators and Built-in Types
Generator Expressions: Iterators Meet List Comprehensions
Timing Iteration Alternatives
Function Design Concepts
Functions Are Objects: Indirect Calls
Function Gotchas
Local Names Are Detected Statically
Defaults and Mutable Objects
Functions Without returns
Enclosing Scope Loop Variables
Chapter Summary
BRAIN BUILDER
Part IV Exercises
V. Modules
18. Modules: The Big Picture
Why Use Modules?
Python Program Architecture
How to Structure a Program
Imports and Attributes
Standard Library Modules
How Imports Work
1. Find It
The module search path
The sys.path list
Module file selection
Advanced module selection concepts
2. Compile It (Maybe)
3. Run It
Chapter Summary
19. Module Coding Basics
Module Creation
Module Usage
The import Statement
The from statement
The from * Statement
Imports Happen Only Once
import and from Are Assignments
Cross-File Name Changes
import and from Equivalence
Potential Pitfalls of the from Statement
When import is required
Module Namespaces
Files Generate Namespaces
Attribute Name Qualification
Imports Versus Scopes
Namespace Nesting
Reloading Modules
reload Basics
reload Example
Chapter Summary
20. Module Packages
Package Import Basics
Packages and Search Path Settings
Package _ _init_ _.py Files
Package Import Example
from Versus import with Packages
Why Use Package Imports?
A Tale of Three Systems
Chapter Summary
21. Advanced Module Topics
Data Hiding in Modules
Minimizing from * Damage: _X and _ _all_ _
Enabling Future Language Features
Mixed Usage Modes: _ _name_ _ and _ _main_ _
Unit Tests with _ _name_ _
Changing the Module Search Path
The import as Extension
Relative Import Syntax
Why Relative Imports?
Module Design Concepts
Modules Are Objects: Metaprograms
Module Gotchas
Statement Order Matters in Top-Level Code
Importing Modules by Name String
from Copies Names but Doesn’t Link
from * Can Obscure the Meaning of Variables
reload May Not Impact from Imports
reload, from, and Interactive Testing
reload Isn’t Applied Transitively
Recursive from Imports May Not Work
Chapter Summary
BRAIN BUILDER
Part V Exercises
VI. Classes and OOP
22. OOP: The Big Picture
Why Use Classes?
OOP from 30,000 Feet
Attribute Inheritance Search
Classes and Instances
Class Method Calls
Coding Class Trees
OOP Is About Code Reuse
Chapter Summary
23. Class Coding Basics
Classes Generate Multiple Instance Objects
Class Objects Provide Default Behavior
Instance Objects Are Concrete Items
A First Example
Classes Are Customized by Inheritance
A Second Example
Classes Are Attributes in Modules
Classes Can Intercept Python Operators
A Third Example
Why Use Operator Overloading?
The World’s Simplest Python Class
Chapter Summary
24. Class Coding Details
The class Statement
General Form
Example
Methods
Example
Calling Superclass Constructors
Other Method Call Possibilities
Inheritance
Attribute Tree Construction
Specializing Inherited Methods
Class Interface Techniques
Abstract Superclasses
Operator Overloading
Common Operator Overloading Methods
_ _getitem_ _ Intercepts Index References
_ _getitem_ _ and _ _iter_ _ Implement Iteration
User-Defined Iterators
Multiple iterators on one object
_ _getattr_ _ and _ _setattr_ _ Catch Attribute References
Emulating Privacy for Instance Attributes
_ _repr_ _ and _ _str_ _ Return String Representations
_ _radd_ _ Handles Right-Side Addition
_ _call_ _ Intercepts Calls
Function Interfaces and Callback-Based Code
_ _del_ _ Is a Destructor
Namespaces: The Whole Story
Simple Names: Global Unless Assigned
Attribute Names: Object Namespaces
The “Zen” of Python Namespaces: Assignments Classify Names
Namespace Dictionaries
Namespace Links
A More Realistic Example
Chapter Summary
25. Designing with Classes
Python and OOP
Overloading by Call Signatures (or Not)
Classes As Records
OOP and Inheritance: “Is-a” Relationships
OOP and Composition: “Has-a” Relationships
Stream Processors Revisited
OOP and Delegation
Multiple Inheritance
Classes Are Objects: Generic Object Factories
Why Factories?
Methods Are Objects: Bound or Unbound
Documentation Strings Revisited
Classes Versus Modules
Chapter Summary
26. Advanced Class Topics
Extending Built-in Types
Extending Types by Embedding
Extending Types by Subclassing
Pseudoprivate Class Attributes
Name Mangling Overview
Why Use Pseudoprivate Attributes?
New-Style Classes
Diamond Inheritance Change
Diamond inheritance example
Explicit conflict resolution
Other New-Style Class Extensions
Static and class methods
Instance slots
Class properties
New _ _getattribute_ _ overloading method
Static and Class Methods
Using Static and Class Methods
Function Decorators
Decorator Example
Class Gotchas
Changing Class Attributes Can Have Side Effects
Multiple Inheritance: Order Matters
Methods, Classes, and Nested Scopes
“Overwrapping-itis”
Chapter Summary
BRAIN BUILDER
Part VI Exercises
VII. Exceptions and Tools
27. Exception Basics
Why Use Exceptions?
Exception Roles
Exception Handling: The Short Story
The try/except/else Statement
try Statement Clauses
The try/else Clause
Example: Default Behavior
Example: Catching Built-in Exceptions
The try/finally Statement
Example: Coding Termination Actions with try/finally
Unified try/except/finally
Combining finally and except by Nesting
Unified try Example
The raise Statement
Example: Raising and Catching User-Defined Exceptions
Example: Passing Extra Data with raise
Example: Propagating Exceptions with raise
The assert Statement
Example: Trapping Constraints (but Not Errors)
with/as Context Managers
Basic Usage
The Context Management Protocol
Chapter Summary
28. Exception Objects
String-Based Exceptions
String Exceptions Are Right Out!
Class-Based Exceptions
Class Exception Example
Why Class Exceptions?
Built-in Exception Classes
Specifying Exception Text
Sending Extra Data and Behavior in Instances
Example: Extra data with classes and strings
General raise Statement Forms
Chapter Summary
29. Designing with Exceptions
Nesting Exception Handlers
Example: Control-Flow Nesting
Example: Syntactic Nesting
Exception Idioms
Exceptions Aren’t Always Errors
Functions Signal Conditions with raise
Debugging with Outer try Statements
Running In-Process Tests
More on sys.exc_info
Exception Design Tips
What Should Be Wrapped
Catching Too Much: Avoid Empty excepts
Catching Too Little: Use Class-Based Categories
Exception Gotchas
String Exceptions Match by Identity, Not by Value
Catching the Wrong Thing
Core Language Summary
The Python Toolset
Development Tools for Larger Projects
Chapter Summary
BRAIN BUILDER
Part VII Exercises
VIII. Appendixes
A. Installation and Configuration
Installing the Python Interpreter
Is Python Already Present?
Where to Fetch Python
Installation Steps
Configuring Python
Python Environment Variables
How to Set Configuration Options
Unix/Linux shell variables
DOS variables (Windows)
Other Windows options
Path files
B. Solutions to End-of-Part Exercises
Part I, Getting Started
Part II, Types and Operations
Part III, Statements and Syntax
Part IV, Functions
Part V, Modules
Part VI, Classes and OOP
Part VII, Exceptions and Tools
About the Author
Colophon
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
9. Tuples, Files, and Everything Else
Next
Next Chapter
10. Introducing Python Statements
Part III. Statements and Syntax
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset