Python 3
Ready to master Python? Learn to write effective code, whether you’re a beginner or a professional programmer. Review core Python concepts, including functions, modularization, and object orientation, and walk through the available data types. Then dive into more advanced topics, such as using Django and working with GUIs. With plenty of code examples throughout, this hands-on reference guide has everything you need to become proficient in Python!
- The complete Python 3 handbook
- Learn basic Python principles and work with functions, methods, data types, and more
- Walk through GUIs, network programming, debugging, optimization, and other advanced topics
- Download and consult practical code examples
You'll learn about:
- Coding with Python:
Learn about Python syntax and structure! Follow examples to start developing and testing your own programs using downloadable code.
- The Standard Library:
Explore Python’s built-in library and see how it can be used for a variety of different tasks, from running your mathematical functions to debugging your code.
- Advanced Programming Techniques:
Already know the basics? Enhance your professional skills with more advanced concepts, including GUIs, Django, scientific computing, and connecting to other languages.
Key Highlights:
- Functions
- Methods
- Attributes
- Data types
- GUIs
- Debugging
- Network communication
- Modularization
- Object orientation
- Iterators
- Generators
- Exception handling
View Full Table of Contents
- 1 Introduction
- 1.1 Why Did We Write This Book?
- 1.2 What Does This Book Provide?
- 1.3 Structure of the Book
- 1.4 How Should You Read This Book?
- 1.5 Sample Programs
- 1.6 Preface To the First English Edition (2022)
- 1.7 Acknowledgments
- 2 The Python Programming Language
- 2.1 History, Concepts, and Areas of Application
- 2.1.1 History and Origin
- 2.1.2 Basic Concepts
- 2.1.3 Possible Areas of Use and Strengths
- 2.1.4 Examples of Use
- 2.2 Installing Python
- 2.2.1 Installing Anaconda on Windows
- 2.2.2 Installing Anaconda on Linux
- 2.2.3 Installing Anaconda on macOS
- 2.3 Installing Third-Party Modules
- 2.4 Using Python
- PART I Getting Started with Python
- 3 Getting Started with the Interactive Mode
- 3.1 Integers
- 3.2 Floats
- 3.3 Character Strings
- 3.4 Lists
- 3.5 Dictionaries
- 3.6 Variables
- 3.6.1 The Special Meaning of the Underscore
- 3.6.2 Identifiers
- 3.7 Logical Expressions
- 3.8 Functions and Methods
- 3.8.1 Functions
- 3.8.2 Methods
- 3.9 Screen Outputs
- 3.10 Modules
- 4 The Path to the First Program
- 4.1 Typing, Compiling, and Testing
- 4.1.1 Windows
- 4.1.2 Linux and macOS
- 4.1.3 Shebang
- 4.1.4 Internal Processes
- 4.2 Basic Structure of a Python Program
- 4.2.1 Wrapping Long Lines
- 4.2.2 Joining Multiple Lines
- 4.3 The First Program
- 4.3.1 Initialization
- 4.3.2 Loop Header
- 4.3.3 Loop Body
- 4.3.4 Screen Output
- 4.4 Comments
- 4.5 In Case of Error
- 5 Control Structures
- 5.1 Conditionals
- 5.1.1 The if Statement
- 5.1.2 Conditional Expressions
- 5.2 Loops
- 5.2.1 The while Loop
- 5.2.2 Termination of a Loop
- 5.2.3 Detecting a Loop Break
- 5.2.4 Aborting the Current Iteration
- 5.2.5 The for Loop
- 5.2.6 The for Loop as a Counting Loop
- 5.3 The pass Statement
- 5.4 Assignment Expressions
- 5.4.1 The Guessing Numbers Game with Assignment Expressions
- 6 Files
- 6.1 Data Streams
- 6.2 Reading Data from a File
- 6.2.1 Opening and Closing a File
- 6.2.2 The with Statement
- 6.2.3 Reading the File Content
- 6.3 Writing Data to a File
- 6.4 Generating the File Object
- 6.4.1 The Built-In open Function
- 6.4.2 Attributes and Methods of a File Object
- 6.4.3 Changing the Write/Read Position
- 7 The Data Model
- 7.1 The Structure of Instances
- 7.1.1 Data Type
- 7.1.2 Value
- 7.1.3 Identity
- 7.2 Deleting References
- 7.3 Mutable versus Immutable Data Types
- 8 Functions, Methods, and Attributes
- 8.1 Parameters of Functions and Methods
- 8.1.1 Positional Parameters
- 8.1.2 Keyword Arguments
- 8.1.3 Optional Parameters
- 8.1.4 Keyword-Only Parameters
- 8.2 Attributes
- 9 Sources of Information on Python
- 9.1 The Built-In Help Function
- 9.2 The Online Documentation
- 9.3 PEPs
- PART II Data Types
- 10 Basic Data Types: An Overview
- 10.1 Nothingness: NoneType
- 10.2 Operators
- 10.2.1 Operator Precedence
- 10.2.2 Evaluation Order
- 10.2.3 Concatenating Comparisons
- 11 Numeric Data Types
- 11.1 Arithmetic Operators
- 11.2 Comparison Operators
- 11.3 Conversion between Numeric Data Types
- 11.4 Integers: int
- 11.4.1 Numeral Systems
- 11.4.2 Bit Operations
- 11.4.3 Methods
- 11.5 Floats: float
- 11.5.1 Exponential Notation
- 11.5.2 Precision
- 11.5.3 Infinite and Not a Number
- 11.6 Boolean Values: bool
- 11.6.1 Logical Operators
- 11.6.2 Truth Values of Non-Boolean Data Types
- 11.6.3 Evaluating Logical Operators
- 11.7 Complex Numbers: complex
- 12 Sequential Data Types
- 12.1 The Difference between Text and Binary Data
- 12.2 Operations on Instances of Sequential Data Types
- 12.2.1 Checking for Elements
- 12.2.2 Concatenation
- 12.2.3 Repetition
- 12.2.4 Indexing
- 12.2.5 Slicing
- 12.2.6 Length of a Sequence
- 12.2.7 The Smallest and the Largest Element
- 12.2.8 Searching for an Element
- 12.2.9 Counting Elements
- 12.3 The list Data Type
- 12.3.1 Changing a Value within the List: Assignment via []
- 12.3.2 Replacing Sublists and Inserting New Elements: Assignment via []
- 12.3.3 Deleting Elements and Sublists: del in Combination with []
- 12.3.4 Methods of list Instances
- 12.3.5 Sorting Lists: s.sort([key, reverse])
- 12.3.6 Side Effects
- 12.3.7 List Comprehensions
- 12.4 Immutable Lists: tuple
- 12.4.1 Packing and Unpacking
- 12.4.2 Immutable Doesn’t Necessarily Mean Unchangeable!
- 12.5 Strings: str, bytes, bytearray
- 12.5.1 Control Characters
- 12.5.2 String Methods
- 12.5.3 Formatting Strings
- 12.5.4 Character Sets and Special Characters
- 13 Mappings and Sets
- 13.1 Dictionary: dict
- 13.1.1 Creating a Dictionary
- 13.1.2 Keys and Values
- 13.1.3 Iteration
- 13.1.4 Operators
- 13.1.5 Methods
- 13.1.6 Dict Comprehensions
- 13.2 Sets: set and frozenset
- 13.2.1 Creating a Set
- 13.2.2 Iteration
- 13.2.3 Operators
- 13.2.4 Methods
- 13.2.5 Mutable Sets: set
- 13.2.6 Immutable Sets: frozenset
- 14 Collections
- 14.1 Chained Dictionaries
- 14.2 Counting Frequencies
- 14.3 Dictionaries with Default Values
- 14.4 Doubly Linked Lists
- 14.5 Named Tuples
- 15 Date and Time
- 15.1 Elementary Time Functions—time
- 15.1.1 The struct_time Data Type
- 15.1.2 Constants
- 15.1.3 Functions
- 15.2 Object-Oriented Date Management: datetime
- 15.2.1 datetime.date
- 15.2.2 datetime.time
- 15.2.3 datetime.datetime
- 15.2.4 datetime.timedelta
- 15.2.5 Operations for datetime.datetime and datetime.date
- 15.3 Time Zones: zoneinfo
- 15.3.1 The IANA Time Zone Database
- 15.3.2 Specifying the Time in Local Time Zones
- 15.3.3 Calculating with Time Indications in Local Time Zones
- 16 Enumerations and Flags
- 16.1 Enumeration Types: enum
- 16.2 Enumeration Types for Bit Patterns: flag
- 16.3 Integer Enumeration Types: IntEnum
- PART III Advanced Programming Techniques
- 17 Functions
- 17.1 Defining a Function
- 17.2 Return Values
- 17.3 Function Objects
- 17.4 Optional Parameters
- 17.5 Keyword Arguments
- 17.6 Arbitrarily Many Parameters
- 17.7 Keyword-Only Parameters
- 17.8 Positional-Only Parameters
- 17.9 Unpacking When Calling a Function
- 17.10 Side Effects
- 17.11 Namespaces
- 17.11.1 Accessing Global Variables: global
- 17.11.2 Accessing the Global Namespace
- 17.11.3 Local Functions
- 17.11.4 Accessing Parent Namespaces: nonlocal
- 17.11.5 Unbound Local Variables: A Stumbling Block
- 17.12 Anonymous Functions
- 17.13 Recursion
- 17.14 Built-In Functions
- 17.14.1 abs(x)
- 17.14.2 all(iterable)
- 17.14.3 any(iterable)
- 17.14.4 ascii(object)
- 17.14.5 bin(x)
- 17.14.6 bool([x])
- 17.14.7 bytearray([source, encoding, errors])
- 17.14.8 bytes([source, encoding, errors])
- 17.14.9 chr(i)
- 17.14.10 complex([real, imag])
- 17.14.11 dict([source])
- 17.14.12 divmod(a, b)
- 17.14.13 enumerate(iterable, [start])
- 17.14.14 eval(expression, [globals, locals])
- 17.14.15 exec(object, [globals, locals])
- 17.14.16 filter(function, iterable)
- 17.14.17 float([x])
- 17.14.18 format(value, [format_spec])
- 17.14.19 frozenset([iterable])
- 17.14.20 globals()
- 17.14.21 hash(object)
- 17.14.22 help([object])
- 17.14.23 hex(x)
- 17.14.24 id(object)
- 17.14.25 input([prompt])
- 17.14.26 int([x, base])
- 17.14.27 len(s)
- 17.14.28 list([sequence])
- 17.14.29 locals()
- 17.14.30 map(function, [*iterable])
- 17.14.31 max(iterable, {default, key}), max(arg1, arg2, [*args], {key})
- 17.14.32 min(iterable, {default, key}), min(arg1, arg2, [*args], {key})
- 17.14.33 oct(x)
- 17.14.34 ord(c)
- 17.14.35 pow(x, y, [z])
- 17.14.36 print([*objects], {sep, end, file, flush})
- 17.14.37 range([start], stop, [step])
- 17.14.38 repr(object)
- 17.14.39 reversed(sequence)
- 17.14.40 round(x, [n])
- 17.14.41 set([iterable])
- 17.14.42 sorted(iterable, [key, reverse])
- 17.14.43 str([object, encoding, errors])
- 17.14.44 sum(iterable, [start])
- 17.14.45 tuple([iterable])
- 17.14.46 type(object)
- 17.14.47 zip([*iterables], {strict})
- 18 Modules and Packages
- 18.1 Importing Global Modules
- 18.2 Local Modules
- 18.2.1 Name Conflicts
- 18.2.2 Module-Internal References
- 18.2.3 Executing Modules
- 18.3 Packages
- 18.3.1 Importing All Modules of a Package
- 18.3.2 Namespace Packages
- 18.3.3 Relative Import Statements
- 18.4 The importlib Package
- 18.4.1 Importing Modules and Packages
- 18.4.2 Changing the Import Behavior
- 18.5 Planned Language Elements
- 19 Object-Oriented Programming
- 19.1 Example: A Non-Object-Oriented Account
- 19.1.1 Creating a New Account
- 19.1.2 Transferring Money
- 19.1.3 Depositing and Withdrawing Money
- 19.1.4 Viewing the Account Balance
- 19.2 Classes
- 19.2.1 Defining Methods
- 19.2.2 The Constructor
- 19.2.3 Attributes
- 19.2.4 Example: An Object-Oriented Account
- 19.3 Inheritance
- 19.3.1 A Simple Example
- 19.3.2 Overriding Methods
- 19.3.3 Example: Checking Account with Daily Turnover
- 19.3.4 Outlook
- 19.4 Multiple Inheritance
- 19.5 Property Attributes
- 19.5.1 Setters and Getters
- 19.5.2 Defining Property Attributes
- 19.6 Static Methods
- 19.7 Class Methods
- 19.8 Class Attributes
- 19.9 Built-in Functions for Object-Oriented Programming
- 19.9.1 Functions for Managing the Attributes of an Instance
- 19.9.2 Functions for Information about the Class Hierarchy
- 19.10 Inheriting Built-In Data Types
- 19.11 Magic Methods and Magic Attributes
- 19.11.1 General Magic Methods
- 19.11.2 Overloading Operators
- 19.11.3 Emulating Data Types: Duck Typing
- 19.12 Data Classes
- 19.12.1 Tuples and Lists
- 19.12.2 Dictionaries
- 19.12.3 Named Tuples
- 19.12.4 Mutable Data Classes
- 19.12.5 Immutable Data Classes
- 19.12.6 Default Values in Data Classes
- 20 Exception Handling
- 20.1 Exceptions
- 20.1.1 Built-In Exceptions
- 20.1.2 Raising an Exception
- 20.1.3 Handling an Exception
- 20.1.4 Custom Exceptions
- 20.1.5 Re-Raising an Exception
- 20.1.6 Exception Chaining
- 20.2 Assertions
- 20.3 Warnings
- 21 Generators and Iterators
- 21.1 Generators
- 21.1.1 Subgenerators
- 21.1.2 Generator Expressions
- 21.2 Iterators
- 21.2.1 The Iterator Protocol
- 21.2.2 Example: The Fibonacci Sequence
- 21.2.3 Example: The Golden Ratio
- 21.2.4 A Generator for the Implementation of __iter__
- 21.2.5 Using Iterators
- 21.2.6 Multiple Iterators for the Same Instance
- 21.2.7 Disadvantages of Iterators Compared to Direct Access via Indexes
- 21.2.8 Alternative Definition for Iterable Objects
- 21.2.9 Function Iterators
- 21.3 Special Generators: itertools
- 21.3.1 accumulate(iterable, [func])
- 21.3.2 chain([*iterables])
- 21.3.3 combinations(iterable, r)
- 21.3.4 combinations_with_replacement(iterable, r)
- 21.3.5 compress(data, selectors)
- 21.3.6 count([start, step])
- 21.3.7 cycle(iterable)
- 21.3.8 dropwhile(predicate, iterable)
- 21.3.9 filterfalse(predicate, iterable)
- 21.3.10 groupby(iterable, [key])
- 21.3.11 islice(iterable, [start], stop, [step])
- 21.3.12 permutations(iterable, [r])
- 21.3.13 product([*iterables], [repeat])
- 21.3.14 repeat(object, [times])
- 21.3.15 starmap(function, iterable)
- 21.3.16 takewhile(predicate, iterable)
- 21.3.17 tee(iterable, [n])
- 21.3.18 zip_longest([*iterables], {fillvalue})
- 22 Context Manager
- 22.1 The with Statement
- 22.1.1 __enter__(self)
- 22.1.2 __exit__(self, exc_type, exc_value, traceback)
- 22.2 Helper Functions for with Contexts: contextlib
- 22.2.1 Dynamically Assembled Context Combinations - ExitStack
- 22.2.2 Suppressing Certain Exception Types
- 22.2.3 Redirecting the Standard Output Stream
- 22.2.4 Optional Contexts
- 22.2.5 Simple Functions as Context Manager
- 23 Decorators
- 23.1 Function Decorators
- 23.1.1 Decorating Functions and Methods
- 23.1.2 Name and Docstring after Applying a Decorator
- 23.1.3 Nested Decorators
- 23.1.4 Example: A Cache Decorator
- 23.2 Class Decorators
- 23.3 The functools Module
- 23.3.1 Simplifying Function Interfaces
- 23.3.2 Simplifying Method Interfaces
- 23.3.3 Caches
- 23.3.4 Completing Orderings of Custom Classes
- 23.3.5 Overloading Functions
- 24 Annotations for Static Type Checking
- 24.1 Annotations
- 24.1.1 Annotating Functions and Methods
- 24.1.2 Annotating Variables and Attributes
- 24.1.3 Accessing Annotations at Runtime
- 24.1.4 When are Annotations Evaluated?
- 24.2 Type Hints: The typing Module
- 24.2.1 Valid Type Hints
- 24.2.2 Container Types
- 24.2.3 Abstract Container Types
- 24.2.4 Type Aliases
- 24.2.5 Type Unions and Optional Values
- 24.2.6 Type Variables
- 24.3 Static Type Checking in Python: mypy
- 24.3.1 Installation
- 24.3.2 Example
- 25 Structural Pattern Matching
- 25.1 The match Statement
- 25.2 Pattern Types in the case Statement
- 25.2.1 Literal and Value Patterns
- 25.2.2 OR Pattern
- 25.2.3 Patterns with Type Checking
- 25.2.4 Specifying Conditions for Matches
- 25.2.5 Grouping Subpatterns
- 25.2.6 Capture and Wildcard Patterns
- 25.2.7 Sequence Patterns
- 25.2.8 Mapping Patterns
- 25.2.9 Patterns for Objects and Their Attribute Values
- PART IV The Standard Library
- 26 Mathematics
- 26.1 Mathematical Functions: math, cmath
- 26.1.1 General Mathematical Functions
- 26.1.2 Exponential and Logarithm Functions
- 26.1.3 Trigonometric and Hyperbolic Functions
- 26.1.4 Distances and Norms
- 26.1.5 Converting Angles
- 26.1.6 Representations of Complex Numbers
- 26.2 Random Number Generator: random
- 26.2.1 Saving and Loading the Random State
- 26.2.2 Generating Random Integers
- 26.2.3 Generating Random Floats
- 26.2.4 Random Operations on Sequences
- 26.2.5 SystemRandom([seed])
- 26.3 Statistical Calculations: statistics
- 26.4 Intuitive Decimal Numbers: decimal
- 26.4.1 Using the Data Type
- 26.4.2 Nonnumeric Values
- 26.4.3 The Context Object
- 26.5 Hash Functions: hashlib
- 26.5.1 Using the Module
- 26.5.2 Other Hash Algorithms
- 26.5.3 Comparing Large Files
- 26.5.4 Passwords
- 27 Screen Outputs and Logging
- 27.1 Formatted Output of Complex Objects: pprint
- pprint(object, [stream, indent, width, depth], {compact})
- 27.2 Log Files: logging
- 27.2.1 Customizing the Message Format
- 27.2.2 Logging Handlers
- 28 Regular Expressions
- 28.1 Syntax of Regular Expressions
- 28.1.1 Any Character
- 28.1.2 Character Classes
- 28.1.3 Quantifiers
- 28.1.4 Predefined Character Classes
- 28.1.5 Other Special Characters
- 28.1.6 Nongreedy Quantifiers
- 28.1.7 Groups
- 28.1.8 Alternatives
- 28.1.9 Extensions
- 28.2 Using the re Module
- 28.2.1 Searching
- 28.2.2 Matching
- 28.2.3 Splitting a String
- 28.2.4 Replacing Parts of a String
- 28.2.5 Replacing Problem Characters
- 28.2.6 Compiling a Regular Expression
- 28.2.7 Flags
- 28.2.8 The Match Object
- 28.3 A Simple Sample Program: Searching
- 28.4 A More Complex Sample Program: Matching
- 28.5 Comments in Regular Expressions
- 29 Interface to Operating System and Runtime Environment
- 29.1 Operating System Functionality: os
- 29.1.1 environ
- 29.1.2 getpid()
- 29.1.3 cpu_count()
- 29.1.4 system(cmd)
- 29.1.5 popen(command, [mode, buffering])
- 29.2 Accessing the Runtime Environment: sys
- 29.2.1 Command Line Parameters
- 29.2.2 Default Paths
- 29.2.3 Standard Input/Output Streams
- 29.2.4 Exiting the Program
- 29.2.5 Details of the Python Version
- 29.2.6 Operating System Details
- 29.2.7 Hooks
- 29.3 Command Line Parameters: argparse
- 29.3.1 Calculator: A Simple Example
- 29.3.2 A More Complex Example
- 30 File System
- 30.1 Accessing the File System: os
- 30.1.1 access(path, mode)
- 30.1.2 chmod(path, mode)
- 30.1.3 listdir([path])
- 30.1.4 mkdir(path, [mode]) and makedirs(path, [mode])
- 30.1.5 remove(path)
- 30.1.6 removedirs(path)
- 30.1.7 rename(src, dst) and renames(old, new)
- 30.1.8 walk(top, [topdown, onerror])
- 30.2 File Paths: os.path
- 30.2.1 abspath(path)
- 30.2.2 basename(path)
- 30.2.3 commonprefix(list)
- 30.2.4 dirname(path)
- 30.2.5 join(path, *paths)
- 30.2.6 normcase(path)
- 30.2.7 split(path)
- 30.2.8 splitdrive(path)
- 30.2.9 splitext(path)
- 30.3 Accessing the File System: shutil
- 30.3.1 Directory and File Operations
- 30.3.2 Archive Operations
- 30.4 Temporary Files: tempfile
- 30.4.1 TemporaryFile([mode, [bufsize, suffix, prefix, dir])
- 30.4.2 tempfile.TemporaryDirectory([suffix, prefix, dir])
- 31 Parallel Programming
- 31.1 Processes, Multitasking, and Threads
- 31.1.1 The Lightweights among the Processes: Threads
- 31.1.2 Threads or Processes?
- 31.1.3 Cooperative Multitasking: A Third Way
- 31.2 Python's Interfaces for Parallelization
- 31.3 The Abstract Interface: concurrent.futures
- 31.3.1 An Example with a futures.ThreadPoolExecutor
- 31.3.2 Executor Instances as Context Managers
- 31.3.3 Using futures.ProcessPoolExecutor
- 31.3.4 Managing the Tasks of an Executor
- 31.4 The Flexible Interface: threading and multiprocessing
- 31.4.1 Threads in Python: threading
- 31.4.2 Processes in Python: multiprocessing
- 31.5 Cooperative Multitasking
- 31.5.1 Cooperative Functions: Coroutines
- 31.5.2 Awaitable Objects
- 31.5.3 The Cooperation of Coroutines: Tasks
- 31.5.4 A Cooperative Web Crawler
- 31.5.5 Blocking Operations in Coroutines
- 31.5.6 Other Asynchronous Language Features
- 31.6 Conclusion: Which Interface Is the Right One?
- 31.6.1 Is Cooperative Multitasking an Option?
- 31.6.2 Abstraction or Flexibility?
- 31.6.3 Threads or Processes?
- 32 Data Storage
- 32.1 XML
- 32.1.1 ElementTree
- 32.1.2 Simple API for XML
- 32.2 Databases
- 32.2.1 The Built-In Database in Python: sqlite3
- 32.3 Compressed Files and Archives
- 32.3.1 gzip.open(filename, [mode, compresslevel])
- 32.3.2 Other Modules for Accessing Compressed Data
- 32.4 Serializing Instances: pickle
- 32.4.1 Functional Interface
- 32.4.2 Object-Oriented Interface
- 32.5 The JSON Data Exchange Format: json
- 32.6 The CSV Table Format: csv
- 32.6.1 Reading Data from a CSV File with reader Objects
- 32.6.2 Using Custom Dialects: Dialect Objects
- 33 Network Communication
- 33.1 Socket API
- 33.1.1 Client-Server Systems
- 33.1.2 UDP
- 33.1.3 TCP
- 33.1.4 Blocking and Nonblocking Sockets
- 33.1.5 Creating a Socket
- 33.1.6 The Socket Class
- 33.1.7 Network Byte Order
- 33.1.8 Multiplexing Servers: selectors
- 33.1.9 Object-Oriented Server Development: socketserver
- 33.2 XML-RPC
- 33.2.1 The Server
- 33.2.2 The Client
- 33.2.3 Multicall
- 33.2.4 Limitations
- 34 Accessing Resources on the Internet
- 34.1 Protocols
- 34.1.1 Hypertext Transfer Protocol
- 34.1.2 File Transfer Protocol
- 34.2 Solutions
- 34.2.1 Outdated Solutions for Python 2
- 34.2.2 Solutions in the Standard Library
- 34.2.3 Third-Party Solutions
- 34.3 The Easy Way: requests
- 34.3.1 Simple Requests via GET and POST
- 34.3.2 Web APIs
- 34.4 URLs: urllib
- 34.4.1 Accessing Remote Resources: urllib.request
- 34.4.2 Reading and Processing URLs: urllib.parse
- 34.5 FTP: ftplib
- 34.5.1 Connecting to an FTP Server
- 34.5.2 Executing FTP commands
- 34.5.3 Working with Files and Directories
- 34.5.4 Transferring Files
- 35 Email
- 35.1 SMTP: smtplib
- 35.1.1 SMTP([host, port, local_hostname, timeout, source_address])
- 35.1.2 Establishing and Terminating a Connection
- 35.1.3 Sending an Email
- 35.1.4 Example
- 35.2 POP3: poplib
- 35.2.1 POP3(host, [port, timeout])
- 35.2.2 Establishing and Terminating a Connection
- 35.2.3 Listing Existing Emails
- 35.2.4 Retrieving and Deleting Emails
- 35.2.5 Example
- 35.3 IMAP4: imaplib
- 35.3.1 IMAP4([host, port, timeout])
- 35.3.2 Establishing and Terminating a Connection
- 35.3.3 Finding and Selecting a Mailbox
- 35.3.4 Operations with Mailboxes
- 35.3.5 Searching Emails
- 35.3.6 Retrieving Emails
- 35.3.7 Example
- 35.4 Creating Complex Emails: email
- 35.4.1 Creating a Simple Email
- 35.4.2 Creating an Email with Attachments
- 35.4.3 Reading an Email
- 36 Debugging and Quality Assurance
- 36.1 The Debugger
- 36.2 Automated Testing
- 36.2.1 Test Cases in Docstrings: doctest
- 36.2.2 Unit Tests: unittest
- 36.3 Analyzing the Runtime Performance
- 36.3.1 Runtime Measurement: timeit
- 36.3.2 Profiling: cProfile
- 36.3.3 Tracing: trace
- 37 Documentation
- 37.1 Docstrings
- 37.2 Automatically Generated Documentation: pydoc
- PART V Advanced Topics
- 38 Distributing Python Projects
- 38.1 A History of Distributions in Python
- 38.1.1 The Classic Approach: distutils
- 38.1.2 The New Standard: setuptools
- 38.1.3 The Package Index: PyPI
- 38.2 Creating Distributions: setuptools
- 38.2.1 Installation
- 38.2.2 Writing the Module
- 38.2.3 The Installation Script
- 38.2.4 Creating a Source Distribution
- 38.2.5 Creating a Binary Distribution
- 38.2.6 Installing Distributions
- 38.3 Creating EXE files: cx_Freeze
- 38.3.1 Installation
- 38.3.2 Usage
- 38.4 Package Manager
- 38.4.1 The Python Package Manager: pip
- 38.4.2 The conda Package Manager
- 38.5 Localizing Programs: gettext
- 38.5.1 Example of Using gettext
- 38.5.2 Creating the Language Compilation
- 39 Virtual Environments
- 39.1 Using Virtual Environments: venv
- 39.1.1 Activating a Virtual Environment
- 39.1.2 Working in a Virtual Environment
- 39.1.3 Deactivating a Virtual Environment
- 39.2 Virtual Environments in Anaconda
- 40 Alternative Interpreters and Compilers
- 40.1 Just-in-Time Compilation: PyPy
- 40.1.1 Installation and Use
- 40.1.2 Example
- 40.2 Numba
- 40.2.1 Installation
- 40.2.2 Example
- 40.3 Connecting to C and C++: Cython
- 40.3.1 Installation
- 40.3.2 The Functionality of Cython
- 40.3.3 Compiling a Cython Program
- 40.3.4 A Cython Program with Static Typing
- 40.3.5 Using a C Library
- 40.4 The Interactive Python Shell: IPython
- 40.4.1 Installation
- 40.4.2 The Interactive Shell
- 40.4.3 The Jupyter Notebook
- 41 Graphical User Interfaces
- 41.1 Toolkits
- 41.1.1 Tkinter (Tk)
- 41.1.2 PyGObject (GTK)
- 41.1.3 Qt for Python (Qt)
- 41.1.4 wxPython (wxWidgets)
- 41.2 Introduction to tkinter
- 41.2.1 A Simple Example
- 41.2.2 Control Variables
- 41.2.3 The Packer
- 41.2.4 Events
- 41.2.5 Widgets
- 41.2.6 Drawings: The Canvas Widget
- 41.2.7 Other Modules
- 41.3 Introduction to PySide6
- 41.3.1 Installation
- 41.3.2 Basic Concepts of Qt
- 41.3.3 Development Process
- 41.4 Signals and Slots
- 41.5 Important Widgets
- 41.5.1 QCheckBox
- 41.5.2 QComboBox
- 41.5.3 QDateEdit, QTimeEdit, and QDateTimeEdit
- 41.5.4 QDialog
- 41.5.5 QLineEdit
- 41.5.6 QListWidget and QListView
- 41.5.7 QProgressBar
- 41.5.8 QPushButton
- 41.5.9 QRadioButton
- 41.5.10 QSlider and QDial
- 41.5.11 QTextEdit
- 41.5.12 QWidget
- 41.6 Drawing Functionality
- 41.6.1 Tools
- 41.6.2 Coordinate System
- 41.6.3 Simple Shapes
- 41.6.4 Images
- 41.6.5 Text
- 41.6.6 Eye Candy
- 41.7 Model-View Architecture
- 41.7.1 Sample Project: An Address Book
- 41.7.2 Selecting Entries
- 41.7.3 Editing Entries
- 42 Python as a Server-Side Programming Language on the Web: An Introduction to Django
- 42.1 Concepts and Features of Django
- 42.2 Installing Django
- 42.3 Creating a New Django Project
- 42.3.1 The Development Web Server
- 42.3.2 Configuring the Project
- 42.4 Creating an Application
- 42.4.1 Importing the Application into the Project
- 42.4.2 Defining a Model
- 42.4.3 Relationships between Models
- 42.4.4 Transferring the Model to the Database
- 42.4.5 The Model API
- 42.4.6 The Project Gets a Face
- 42.4.7 Django's Template System
- 42.4.8 Processing Form Data
- 42.4.9 Django’s Admin Control Panel
- 43 Scientific Computing and Data Science
- 43.1 Installation
- 43.2 The Model Program
- 43.2.1 Importing numpy, scipy, and matplotlib
- 43.2.2 Vectorization and the numpy.ndarray Data Type
- 43.2.3 Visualizing Data Using matplotlib.pyplot
- 43.3 Overview of the numpy and scipy Modules
- 43.3.1 Overview of the numpy.ndarray Data Type
- 43.3.2 Overview of scipy
- 43.4 An Introduction to Data Analysis with pandas
- 43.4.1 The DataFrame Object
- 43.4.2 Selective Data Access
- 43.4.3 Deleting Rows and Columns
- 43.4.4 Inserting Rows and Columns
- 43.4.5 Logical Expressions on Data Records
- 43.4.6 Manipulating Data Records
- 43.4.7 Input and Output
- 43.4.8 Visualization
- 44 Inside Knowledge
- 44.1 Opening URLs in the Default Browser: webbrowser
- 44.2 Interpreting Binary Data: struct
- 44.3 Hidden Password Entry
- 44.3.1 getpass([prompt, stream])
- 44.3.2 getpass.getuser()
- 44.4 Command Line Interpreter
- 44.5 File Interface for Strings: io.StringIO
- 44.6 Generators as Consumers
- 44.6.1 A Decorator for Consuming Generator Functions
- 44.6.2 Triggering Exceptions in a Generator
- 44.6.3 A Pipeline as a Chain of Consuming Generator Functions
- 44.7 Copying Instances: copy
- 44.8 Image Processing: Pillow
- 44.8.1 Installation
- 44.8.2 Loading and Saving Image Files
- 44.8.3 Accessing Individual Pixels
- 44.8.4 Manipulating Images
- 44.8.5 Interoperability
- 45 From Python 2 to Python 3
- 45.1 The Main Differences
- 45.1.1 Input/Output
- 45.1.2 Iterators
- 45.1.3 Strings
- 45.1.4 Integers
- 45.1.5 Exception Handling
- 45.1.6 Standard Library
- 45.2 Automatic Conversion
- Appendices
- A Appendix
- A.1 Reserved Words
- A.2 Operator Precedence
- A.3 Built-In Functions
- A.4 Built-In Exceptions
- A.5 Python IDEs
- B The Authors
- Index