Introducing Python, 2nd Edition

Book description

Easy to understand and fun to read, this updated edition of Introducing Python is ideal for beginning programmers as well as those new to the language. Author Bill Lubanovic takes you from the basics to more involved and varied topics, mixing tutorials with cookbook-style code recipes to explain concepts in Python 3. End-of-chapter exercises help you practice what you’ve learned.

You’ll gain a strong foundation in the language, including best practices for testing, debugging, code reuse, and other development tips. This book also shows you how to use Python for applications in business, science, and the arts, using various Python tools and open source packages.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Audience
    2. Changes in the Second Edition
    3. Outline
    4. Python Versions
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. I. Python Basics
  3. 1. A Taste of Py
    1. Mysteries
    2. Little Programs
    3. A Bigger Program
    4. Python in the Real World
    5. Python Versus the Language from Planet X
    6. Why Python?
    7. Why Not Python?
    8. Python 2 Versus Python 3
    9. Installing Python
    10. Running Python
      1. Using the Interactive Interpreter
      2. Using Python Files
      3. What’s Next?
    11. Your Moment of Zen
    12. Coming Up
    13. Things to Do
  4. 2. Data: Types, Values, Variables, and Names
    1. Python Data Are Objects
    2. Types
    3. Mutability
    4. Literal Values
    5. Variables
    6. Assignment
    7. Variables Are Names, Not Places
    8. Assigning to Multiple Names
    9. Reassigning a Name
    10. Copying
    11. Choose Good Variable Names
    12. Coming Up
    13. Things to Do
  5. 3. Numbers
    1. Booleans
    2. Integers
      1. Literal Integers
      2. Integer Operations
      3. Integers and Variables
      4. Precedence
      5. Bases
      6. Type Conversions
      7. How Big Is an int?
    3. Floats
    4. Math Functions
    5. Coming Up
    6. Things to Do
  6. 4. Choose with if
    1. Comment with #
    2. Continue Lines with \
    3. Compare with if, elif, and else
    4. What Is True?
    5. Do Multiple Comparisons with in
    6. New: I Am the Walrus
    7. Coming Up
    8. Things to Do
  7. 5. Text Strings
    1. Create with Quotes
    2. Create with str()
    3. Escape with \
    4. Combine by Using +
    5. Duplicate with *
    6. Get a Character with []
    7. Get a Substring with a Slice
    8. Get Length with len()
    9. Split with split()
    10. Combine by Using join()
    11. Substitute by Using replace()
    12. Strip with strip()
    13. Search and Select
    14. Case
    15. Alignment
    16. Formatting
      1. Old style: %
      2. New style: {} and format()
      3. Newest Style: f-strings
    17. More String Things
    18. Coming Up
    19. Things to Do
  8. 6. Loop with while and for
    1. Repeat with while
      1. Cancel with break
      2. Skip Ahead with continue
      3. Check break Use with else
    2. Iterate with for and in
      1. Cancel with break
      2. Skip with continue
      3. Check break Use with else
      4. Generate Number Sequences with range()
    3. Other Iterators
    4. Coming Up
    5. Things to Do
  9. 7. Tuples and Lists
    1. Tuples
      1. Create with Commas and ()
      2. Create with tuple()
      3. Combine Tuples by Using +
      4. Duplicate Items with *
      5. Compare Tuples
      6. Iterate with for and in
      7. Modify a Tuple
    2. Lists
      1. Create with []
      2. Create or Convert with list()
      3. Create from a String with split()
      4. Get an Item by [ offset ]
      5. Get Items with a Slice
      6. Add an Item to the End with append()
      7. Add an Item by Offset with insert()
      8. Duplicate All Items with *
      9. Combine Lists by Using extend() or +
      10. Change an Item by [ offset ]
      11. Change Items with a Slice
      12. Delete an Item by Offset with del
      13. Delete an Item by Value with remove()
      14. Get an Item by Offset and Delete It with pop()
      15. Delete All Items with clear()
      16. Find an Item’s Offset by Value with index()
      17. Test for a Value with in
      18. Count Occurrences of a Value with count()
      19. Convert a List to a String with join()
      20. Reorder Items with sort() or sorted()
      21. Get Length with len()
      22. Assign with =
      23. Copy with copy(), list(), or a Slice
      24. Copy Everything with deepcopy()
      25. Compare Lists
      26. Iterate with for and in
      27. Iterate Multiple Sequences with zip()
      28. Create a List with a Comprehension
      29. Lists of Lists
    3. Tuples Versus Lists
    4. There Are No Tuple Comprehensions
    5. Coming Up
    6. Things to Do
  10. 8. Dictionaries and Sets
    1. Dictionaries
      1. Create with {}
      2. Create with dict()
      3. Convert with dict()
      4. Add or Change an Item by [ key ]
      5. Get an Item by [key] or with get()
      6. Get All Keys with keys()
      7. Get All Values with values()
      8. Get All Key-Value Pairs with items()
      9. Get Length with len()
      10. Combine Dictionaries with {**a, **b}
      11. Combine Dictionaries with update()
      12. Delete an Item by Key with del
      13. Get an Item by Key and Delete It with pop()
      14. Delete All Items with clear()
      15. Test for a Key with in
      16. Assign with =
      17. Copy with copy()
      18. Copy Everything with deepcopy()
      19. Compare Dictionaries
      20. Iterate with for and in
      21. Dictionary Comprehensions
    2. Sets
      1. Create with set()
      2. Convert with set()
      3. Get Length with len()
      4. Add an Item with add()
      5. Delete an Item with remove()
      6. Iterate with for and in
      7. Test for a Value with in
      8. Combinations and Operators
      9. Set Comprehensions
      10. Create an Immutable Set with frozenset()
    3. Data Structures So Far
    4. Make Bigger Data Structures
    5. Coming Up
    6. Things to Do
  11. 9. Functions
    1. Define a Function with def
    2. Call a Function with Parentheses
    3. Arguments and Parameters
      1. None Is Useful
      2. Positional Arguments
      3. Keyword Arguments
      4. Specify Default Parameter Values
      5. Explode/Gather Positional Arguments with *
      6. Explode/Gather Keyword Arguments with **
      7. Keyword-Only Arguments
      8. Mutable and Immutable Arguments
    4. Docstrings
    5. Functions Are First-Class Citizens
    6. Inner Functions
      1. Closures
    7. Anonymous Functions: lambda
    8. Generators
      1. Generator Functions
      2. Generator Comprehensions
    9. Decorators
    10. Namespaces and Scope
    11. Uses of _ and __ in Names
    12. Recursion
    13. Async Functions
    14. Exceptions
      1. Handle Errors with try and except
      2. Make Your Own Exceptions
    15. Coming Up
    16. Things to Do
  12. 10. Oh Oh: Objects and Classes
    1. What Are Objects?
    2. Simple Objects
      1. Define a Class with class
      2. Attributes
      3. Methods
      4. Initialization
    3. Inheritance
      1. Inherit from a Parent Class
      2. Override a Method
      3. Add a Method
      4. Get Help from Your Parent with super()
      5. Multiple Inheritance
      6. Mixins
    4. In self Defense
    5. Attribute Access
      1. Direct Access
      2. Getters and Setters
      3. Properties for Attribute Access
      4. Properties for Computed Values
      5. Name Mangling for Privacy
      6. Class and Object Attributes
    6. Method Types
      1. Instance Methods
      2. Class Methods
      3. Static Methods
    7. Duck Typing
    8. Magic Methods
    9. Aggregation and Composition
    10. When to Use Objects or Something Else
    11. Named Tuples
    12. Dataclasses
    13. Attrs
    14. Coming Up
    15. Things to Do
  13. 11. Modules, Packages, and Goodies
    1. Modules and the import Statement
      1. Import a Module
      2. Import a Module with Another Name
      3. Import Only What You Want from a Module
    2. Packages
      1. The Module Search Path
      2. Relative and Absolute Imports
      3. Namespace Packages
      4. Modules Versus Objects
    3. Goodies in the Python Standard Library
      1. Handle Missing Keys with setdefault() and defaultdict()
      2. Count Items with Counter()
      3. Order by Key with OrderedDict()
      4. Stack + Queue == deque
      5. Iterate over Code Structures with itertools
      6. Print Nicely with pprint()
      7. Get Random
    4. More Batteries: Get Other Python Code
    5. Coming Up
    6. Things to Do
  14. II. Python in Practice
  15. 12. Wrangle and Mangle Data
    1. Text Strings: Unicode
      1. Python 3 Unicode Strings
      2. UTF-8
      3. Encode
      4. Decode
      5. HTML Entities
      6. Normalization
      7. For More Information
    2. Text Strings: Regular Expressions
      1. Find Exact Beginning Match with match()
      2. Find First Match with search()
      3. Find All Matches with findall()
      4. Split at Matches with split()
      5. Replace at Matches with sub()
      6. Patterns: Special Characters
      7. Patterns: Using Specifiers
      8. Patterns: Specifying match() Output
    3. Binary Data
      1. bytes and bytearray
      2. Convert Binary Data with struct
      3. Other Binary Data Tools
      4. Convert Bytes/Strings with binascii()
      5. Bit Operators
    4. A Jewelry Analogy
    5. Coming Up
    6. Things to Do
  16. 13. Calendars and Clocks
    1. Leap Year
    2. The datetime Module
    3. Using the time Module
    4. Read and Write Dates and Times
    5. All the Conversions
    6. Alternative Modules
    7. Coming Up
    8. Things to Do
  17. 14. Files and Directories
    1. File Input and Output
      1. Create or Open with open()
      2. Write a Text File with print()
      3. Write a Text File with write()
      4. Read a Text File with read(), readline(), or readlines()
      5. Write a Binary File with write()
      6. Read a Binary File with read()
      7. Close Files Automatically by Using with
      8. Change Position with seek()
    2. Memory Mapping
    3. File Operations
      1. Check Existence with exists()
      2. Check Type with isfile()
      3. Copy with copy()
      4. Change Name with rename()
      5. Link with link() or symlink()
      6. Change Permissions with chmod()
      7. Change Ownership with chown()
      8. Delete a File with remove()
    4. Directory Operations
      1. Create with mkdir()
      2. Delete with rmdir()
      3. List Contents with listdir()
      4. Change Current Directory with chdir()
      5. List Matching Files with glob()
    5. Pathnames
      1. Get a Pathname with abspath()
      2. Get a symlink Pathname with realpath()
      3. Build a Pathname with os.path.join()
      4. Use pathlib
    6. BytesIO and StringIO
    7. Coming Up
    8. Things to Do
  18. 15. Data in Time: Processes and Concurrency
    1. Programs and Processes
      1. Create a Process with subprocess
      2. Create a Process with multiprocessing
      3. Kill a Process with terminate()
      4. Get System Info with os
      5. Get Process Info with psutil
    2. Command Automation
      1. Invoke
      2. Other Command Helpers
    3. Concurrency
      1. Queues
      2. Processes
      3. Threads
      4. concurrent.futures
      5. Green Threads and gevent
      6. twisted
      7. asyncio
      8. Redis
      9. Beyond Queues
    4. Coming Up
    5. Things to Do
  19. 16. Data in a Box: Persistent Storage
    1. Flat Text Files
    2. Padded Text Files
    3. Tabular Text Files
      1. CSV
      2. XML
      3. An XML Security Note
      4. HTML
      5. JSON
      6. YAML
      7. Tablib
      8. Pandas
      9. Configuration Files
    4. Binary Files
      1. Padded Binary Files and Memory Mapping
      2. Spreadsheets
      3. HDF5
      4. TileDB
    5. Relational Databases
      1. SQL
      2. DB-API
      3. SQLite
      4. MySQL
      5. PostgreSQL
      6. SQLAlchemy
      7. Other Database Access Packages
    6. NoSQL Data Stores
      1. The dbm Family
      2. Memcached
      3. Redis
      4. Document Databases
      5. Time Series Databases
      6. Graph Databases
      7. Other NoSQL
    7. Full-Text Databases
    8. Coming Up
    9. Things to Do
  20. 17. Data in Space: Networks
    1. TCP/IP
      1. Sockets
      2. Scapy
      3. Netcat
    2. Networking Patterns
    3. The Request-Reply Pattern
      1. ZeroMQ
      2. Other Messaging Tools
    4. The Publish-Subscribe Pattern
      1. Redis
      2. ZeroMQ
      3. Other Pub-Sub Tools
    5. Internet Services
      1. Domain Name System
      2. Python Email Modules
      3. Other Protocols
    6. Web Services and APIs
    7. Data Serialization
      1. Serialize with pickle
      2. Other Serialization Formats
    8. Remote Procedure Calls
      1. XML RPC
      2. JSON RPC
      3. MessagePack RPC
      4. Zerorpc
      5. gRPC
      6. Twirp
    9. Remote Management Tools
    10. Big Fat Data
      1. Hadoop
      2. Spark
      3. Disco
      4. Dask
    11. Clouds
      1. Amazon Web Services
      2. Google Cloud
      3. Microsoft Azure
      4. OpenStack
    12. Docker
      1. Kubernetes
    13. Coming Up
    14. Things to Do
  21. 18. The Web, Untangled
    1. Web Clients
      1. Test with telnet
      2. Test with curl
      3. Test with httpie
      4. Test with httpbin
      5. Python’s Standard Web Libraries
      6. Beyond the Standard Library: requests
    2. Web Servers
      1. The Simplest Python Web Server
      2. Web Server Gateway Interface (WSGI)
      3. ASGI
      4. Apache
      5. NGINX
      6. Other Python Web Servers
    3. Web Server Frameworks
      1. Bottle
      2. Flask
      3. Django
      4. Other Frameworks
    4. Database Frameworks
    5. Web Services and Automation
      1. webbrowser
      2. webview
    6. Web APIs and REST
    7. Crawl and Scrape
      1. Scrapy
      2. BeautifulSoup
      3. Requests-HTML
    8. Let’s Watch a Movie
    9. Coming Up
    10. Things to Do
  22. 19. Be a Pythonista
    1. About Programming
    2. Find Python Code
    3. Install Packages
      1. Use pip
      2. Use virtualenv
      3. Use pipenv
      4. Use a Package Manager
      5. Install from Source
    4. Integrated Development Environments
      1. IDLE
      2. PyCharm
      3. IPython
      4. Jupyter Notebook
      5. JupyterLab
    5. Name and Document
    6. Add Type Hints
    7. Test
      1. Check with pylint, pyflakes, flake8, or pep8
      2. Test with unittest
      3. Test with doctest
      4. Test with nose
      5. Other Test Frameworks
      6. Continuous Integration
    8. Debug Python Code
      1. Use print()
      2. Use Decorators
      3. Use pdb
      4. Use breakpoint()
    9. Log Error Messages
    10. Optimize
      1. Measure Timing
      2. Algorithms and Data Structures
      3. Cython, NumPy, and C Extensions
      4. PyPy
      5. Numba
    11. Source Control
      1. Mercurial
      2. Git
    12. Distribute Your Programs
    13. Clone This Book
    14. How You Can Learn More
      1. Books
      2. Websites
      3. Groups
      4. Conferences
      5. Getting a Python Job
    15. Coming Up
    16. Things to Do
  23. 20. Py Art
    1. 2-D Graphics
      1. Standard Library
      2. PIL and Pillow
      3. ImageMagick
    2. 3-D Graphics
    3. 3-D Animation
    4. Graphical User Interfaces
    5. Plots, Graphs, and Visualization
      1. Matplotlib
      2. Seaborn
      3. Bokeh
    6. Games
    7. Audio and Music
    8. Coming Up
    9. Things to Do
  24. 21. Py at Work
    1. The Microsoft Office Suite
    2. Carrying Out Business Tasks
    3. Processing Business Data
      1. Extracting, Transforming, and Loading
      2. Data Validation
      3. Additional Sources of Information
    4. Open Source Python Business Packages
    5. Python in Finance
    6. Business Data Security
    7. Maps
      1. Formats
      2. Draw a Map from a Shapefile
      3. Geopandas
      4. Other Mapping Packages
      5. Applications and Data
    8. Coming Up
    9. Things to Do
  25. 22. Py Sci
    1. Math and Statistics in the Standard Library
      1. Math Functions
      2. Working with Complex Numbers
      3. Calculate Accurate Floating Point with decimal
      4. Perform Rational Arithmetic with fractions
      5. Use Packed Sequences with array
      6. Handling Simple Stats with statistics
      7. Matrix Multiplication
    2. Scientific Python
    3. NumPy
      1. Make an Array with array()
      2. Make an Array with arange()
      3. Make an Array with zeros(), ones(), or random()
      4. Change an Array’s Shape with reshape()
      5. Get an Element with []
      6. Array Math
      7. Linear Algebra
    4. SciPy
    5. SciKit
    6. Pandas
    7. Python and Scientific Areas
    8. Coming Up
    9. Things to Do
  26. A. Hardware and Software for Beginning Programmers
    1. Hardware
      1. Caveman Computers
      2. Electricity
      3. Inventions
      4. An Idealized Computer
      5. The CPU
      6. Memory and Caches
      7. Storage
      8. Inputs
      9. Outputs
      10. Relative Access Times
    2. Software
      1. In the Beginning Was the Bit
      2. Machine Language
      3. Assembler
      4. Higher-Level Languages
      5. Operating Systems
      6. Virtual Machines
      7. Containers
      8. Distributed Computing and Networks
      9. The Cloud
      10. Kubernetes
  27. B. Install Python 3
    1. Check Your Python Version
    2. Install Standard Python
      1. macOS
      2. Windows
      3. Linux or Unix
    3. Install the pip Package Manager
    4. Install virtualenv
    5. Other Packaging Solutions
    6. Install Anaconda
      1. Install Anaconda’s Package Manager conda
  28. C. Something Completely Different: Async
    1. Coroutines and Event Loops
    2. Asyncio Alternatives
    3. Async Versus…
    4. Async Frameworks and Servers
  29. D. Answers to Exercises
    1. 1. A Taste of Py
    2. 2. Data: Types, Values, Variables, and Names
    3. 3. Numbers
    4. 4. Choose with if
    5. 5. Text Strings
    6. 6. Loop with while and for
    7. 7. Tuples and Lists
    8. 8. Dictionaries
    9. 9. Functions
    10. 10. Oh Oh: Objects and Classes
    11. 11. Modules, Packages, and Goodies
    12. 12. Wrangle and Mangle Data
    13. 13. Calendars and Clocks
    14. 14. Files and Directories
    15. 15. Data in Time: Processes and Concurrency
    16. 16. Data in a Box: Persistent Storage
    17. 17. Data in Space: Networks
    18. 18. The Web, Untangled
    19. 19. Be a Pythonista
    20. 20. Py Art
    21. 21. Py at Work
    22. 22. PySci
  30. E. Cheat Sheets
    1. Operator Precedence
    2. String Methods
      1. Change Case
      2. Search
      3. Modify
      4. Format
      5. String Type
    3. String Module Attributes
    4. Coda
  31. Index

Product information

  • Title: Introducing Python, 2nd Edition
  • Author(s): Bill Lubanovic
  • Release date: November 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492051367