0%

Book Description

You’ve completed a basic Python programming tutorial or finished Al Sweigart’s best selling Automate the Boring Stuff with Python. What’s the next step toward becoming a capable, confident software developer?

Welcome to Beyond the Basic Stuff with Python. More than a mere collection of advanced syntax and masterful tips for writing clean code, you’ll learn how to advance your Python programming skills by using the command line and other professional tools like code formatters, type checkers, linters, and version control. Sweigart takes you through best practices for setting up your development environment, naming variables, and improving readability, then tackles documentation, organization and performance measurement, as well as object-oriented design and the Big-O algorithm analysis commonly used in coding interviews. The skills you learn will boost your ability to program—not just in Python but in any language.

You’ll learn:

•Coding style, and how to use Python’s Black auto-formatting tool for cleaner code
•Common sources of bugs, and how to detect them with static analyzers
•How to structure the files in your code projects with the Cookiecutter template tool
•Functional programming techniques like lambda and higher-order functions
•How to profile the speed of your code with Python’s built-in timeit and cProfile modules
•The computer science behind Big-O algorithm analysis
•How to make your comments and docstrings informative, and how often to write them
•How to create classes in object-oriented programming, and why they’re used to organize code

Toward the end of the book you’ll read a detailed source-code breakdown of two classic command-line games, the Tower of Hanoi (a logic puzzle) and Four-in-a-Row (a two-player tile-dropping game), and a breakdown of how their code follows the book’s best practices. You’ll test your skills by implementing the program yourself.

Of course, no single book can make you a professional software developer. But Beyond the Basic Stuff with Python will get you further down that path and make you a better programmer in the process as you learn to write readable code that’s easy to debug and perfectly Pythonic.

Table of Contents

  1. Acknowledgments
  2. Introduction
    1. Who Should Read This Book and Why
    2. About This Book
    3. Your Programming Journey
  3. Part 1: Getting Started
    1. Chapter 1: Dealing with Errors and Asking for Help
      1. How to Understand Python Error Messages
        1. Examining Tracebacks
        2. Searching for Error Messages
      2. Preventing Errors with Linters
      3. How to Ask for Programming Help
        1. Limit Back and Forth by Providing Your Information Upfront
        2. State Your Question in the Form of an Actual Question
        3. Ask Your Question on the Appropriate Website
        4. Summarize Your Question in the Headline
        5. Explain What You Want the Code to Do
        6. Include the Full Error Message
        7. Share Your Complete Code
        8. Make Your Code Readable with Proper Formatting
        9. Tell Your Helper What You’ve Already Tried
        10. Describe Your Setup
      4. Examples of Asking a Question
      5. Summary
    2. Chapter 2: Environment Setup and the Command Line
      1. The Filesystem
        1. Paths in Python
        2. The Home Directory
        3. The Current Working Directory
        4. Absolute vs. Relative Paths
      2. Programs and Processes
      3. The Command Line
        1. Opening a Terminal Window
        2. Running Programs from the Command Line
        3. Using Command Line Arguments
        4. Running Python Code from the Command Line with -c
        5. Running Python Programs from the Command Line
        6. Running the py.exe Program
        7. Running Commands from a Python Program
        8. Minimizing Typing with Tab Completion
        9. Viewing the Command History
        10. Working with Common Commands
      4. Environment Variables and PATH
        1. Viewing Environment Variables
        2. Working with the PATH Environment Variable
        3. Changing the Command Line’s PATH Environment Variable
        4. Permanently Adding Folders to PATH on Windows
        5. Permanently Adding Folders to PATH on macOS and Linux
      5. Running Python Programs Without the Command Line
        1. Running Python Programs on Windows
        2. Running Python Programs on macOS
        3. Running Python Programs on Ubuntu Linux
      6. Summary
  4. Part 2: Best Practices, Tools, and Techniques
    1. Chapter 3: Code Formatting with Black
      1. How to Lose Friends and Alienate Co-Workers
      2. Style Guides and PEP 8
      3. Horizontal Spacing
        1. Use Space Characters for Indentation
        2. Spacing Within a Line
      4. Vertical Spacing
        1. A Vertical Spacing Example
        2. Vertical Spacing Best Practices
      5. Black: The Uncompromising Code Formatter
        1. Installing Black
        2. Running Black from the Command Line
        3. Disabling Black for Parts of Your Code
      6. Summary
    2. Chapter 4: Choosing Understandable Names
      1. Casing Styles
      2. PEP 8’s Naming Conventions
      3. Appropriate Name Length
        1. Too Short Names
        2. Too Long Names
      4. Make Names Searchable
      5. Avoid Jokes, Puns, and Cultural References
      6. Don’t Overwrite Built-in Names
      7. The Worst Possible Variable Names Ever
      8. Summary
    3. Chapter 5: Finding Code Smells
      1. Duplicate Code
      2. Magic Numbers
      3. Commented-Out Code and Dead Code
      4. Print Debugging
      5. Variables with Numeric Suffixes
      6. Classes That Should Just Be Functions or Modules
      7. List Comprehensions Within List Comprehensions
      8. Empty except Blocks and Poor Error Messages
      9. Code Smell Myths
        1. Myth: Functions Should Have Only One return Statement at the End
        2. Myth: Functions Should Have at Most One try Statement
        3. Myth: Flag Arguments Are Bad
        4. Myth: Global Variables Are Bad
        5. Myth: Comments Are Unnecessary
      10. Summary
    4. Chapter 6: Writing Pythonic Code
      1. The Zen of Python
      2. Learning to Love Significant Indentation
      3. Commonly Misused Syntax
        1. Use enumerate() Instead of range()
        2. Use the with Statement Instead of open() and close()
        3. Use is to Compare with None Instead of ==
      4. Formatting Strings
        1. Use Raw Strings If Your String Has Many Backslashes
        2. Format Strings with F-Strings
      5. Making Shallow Copies of Lists
      6. Pythonic Ways to Use Dictionaries
        1. Use get() and setdefault() with Dictionaries
        2. Use collections.defaultdict for Default Values
        3. Use Dictionaries Instead of a switch Statement
      7. Conditional Expressions: Python’s “Ugly” Ternary Operator
      8. Working with Variable Values
        1. Chaining Assignment and Comparison Operators
        2. Checking Whether a Variable Is One of Many Values
      9. Summary
    5. Chapter 7: Programming Jargon
      1. Definitions
        1. Python the Language and Python the Interpreter
        2. Garbage Collection
        3. Literals
        4. Keywords
        5. Objects, Values, Instances, and Identities
        6. Items
        7. Mutable and Immutable
        8. Indexes, Keys, and Hashes
        9. Containers, Sequences, Mapping, and Set Types
        10. Dunder Methods and Magic Methods
        11. Modules and Packages
        12. Callables and First-Class Objects
      2. Commonly Confused Terms
        1. Statements vs. Expressions
        2. Block vs. Clause vs. Body
        3. Variable vs. Attribute
        4. Function vs. Method
        5. Iterable vs. Iterator
        6. Syntax vs. Runtime vs. Semantic Errors
        7. Parameters vs. Arguments
        8. Type Coercion vs. Type Casting
        9. Properties vs. Attributes
        10. Bytecode vs. Machine Code
        11. Script vs. Program, Scripting Language vs. Programming Language
        12. Library vs. Framework vs. SDK vs. Engine vs. API
      3. Summary
      4. Further Reading
    6. Chapter 8: Common Python Gotchas
      1. Don’t Add or Delete Items from a List While Looping Over It
      2. Don’t Copy Mutable Values Without copy.copy() and copy.deepcopy()
      3. Don’t Use Mutable Values for Default Arguments
      4. Don’t Build Strings with String Concatenation
      5. Don’t Expect sort() to Sort Alphabetically
      6. Don’t Assume Floating-Point Numbers Are Perfectly Accurate
      7. Don’t Chain Inequality != Operators
      8. Don’t Forget the Comma in Single-Item Tuples
      9. Summary
    7. Chapter 9: Esoteric Python Oddities
      1. Why 256 Is 256 but 257 Is Not 257
      2. String Interning
      3. Python’s Fake Increment and Decrement Operators
      4. All of Nothing
      5. Boolean Values Are Integer Values
      6. Chaining Multiple Kinds of Operators
      7. Python’s Antigravity Feature
      8. Summary
    8. Chapter 10: Writing Effective Functions
      1. Function Names
      2. Function Size Trade-Offs
      3. Function Parameters and Arguments
        1. Default Arguments
        2. Using * and ** to Pass Arguments to Functions
        3. Using * to Create Variadic Functions
        4. Using ** to Create Variadic Functions
        5. Using * and ** to Create Wrapper Functions
      4. Functional Programming
        1. Side Effects
        2. Higher-Order Functions
        3. Lambda Functions
        4. Mapping and Filtering with List Comprehensions
      5. Return Values Should Always Have the Same Data Type
      6. Raising Exceptions vs. Returning Error Codes
      7. Summary
    9. Chapter 11: Comments, Docstrings, and Type Hints
      1. Comments
        1. Comment Style
        2. Inline Comments
        3. Explanatory Comments
        4. Summary Comments
        5. “Lessons Learned” Comments
        6. Legal Comments
        7. Professional Comments
        8. Codetags and TODO Comments
        9. Magic Comments and Source File Encoding
      2. Docstrings
      3. Type Hints
        1. Using Static Analyzers
        2. Setting Type Hints for Multiple Types
        3. Setting Type Hints for Lists, Dictionaries, and More
        4. Backporting Type Hints with Comments
      4. Summary
    10. Chapter 12: Organizing Your Code Projects with Git
      1. Git Commits and Repos
      2. Using Cookiecutter to Create New Python Projects
      3. Installing Git
        1. Configuring Your Git Username and Email
        2. Installing GUI Git Tools
      4. The Git Workflow
        1. How Git Keeps Track of File Status
        2. Why Stage Files?
      5. Creating a Git Repo on Your Computer
        1. Adding Files for Git to Track
        2. Ignoring Files in the Repo
        3. Committing Changes
        4. Deleting Files from the Repo
        5. Renaming and Moving Files in the Repo
      6. Viewing the Commit Log
      7. Recovering Old Changes
        1. Undoing Uncommitted Local Changes
        2. Unstaging a Staged File
        3. Rolling Back the Most Recent Commits
        4. Rolling Back to a Specific Commit for a Single File
        5. Rewriting the Commit History
      8. GitHub and the git push Command
        1. Pushing an Existing Repository to GitHub
        2. Cloning a Repo from an Existing GitHub Repo
      9. Summary
    11. Chapter 13: Measuring Performance and Big O Algorithm Analysis
      1. The timeit Module
      2. The cProfile Profiler
      3. Big O Algorithm Analysis
      4. Big O Orders
        1. A Bookshelf Metaphor for Big O Orders
        2. Big O Measures the Worst-Case Scenario
      5. Determining the Big O Order of Your Code
        1. Why Lower Orders and Coefficients Don’t Matter
        2. Big O Analysis Examples
        3. The Big O Order of Common Function Calls
        4. Analyzing Big O at a Glance
        5. Big O Doesn’t Matter When n Is Small, and n Is Usually Small
      6. Summary
    12. Chapter 14: Practice Projects
      1. The Tower of Hanoi
        1. The Output
        2. The Source Code
        3. Writing the Code
      2. Four-in-a-Row
        1. The Output
        2. The Source Code
        3. Writing the Code
      3. Summary
  5. Part 3: Object-Oriented Python
    1. Chapter 15: Object-Oriented Programming and Classes
      1. Real-World Analogy: Filling Out a Form
      2. Creating Objects from Classes
      3. Creating a Simple Class: WizCoin
        1. Methods, __init__(), and self
        2. Attributes
        3. Private Attributes and Private Methods
      4. The type() Function and __qualname__ Attribute
      5. Non-OOP vs. OOP Examples: Tic-Tac-Toe
      6. Designing Classes for the Real World Is Hard
      7. Summary
    2. Chapter 16: Object-Oriented Programming and Inheritance
      1. How Inheritance Works
        1. Overriding Methods
        2. The super() Function
        3. Favor Composition Over Inheritance
        4. Inheritance’s Downside
      2. The isinstance() and issubclass() Functions
      3. Class Methods
      4. Class Attributes
      5. Static Methods
      6. When to Use Class and Static Object-Oriented Features
      7. Object-Oriented Buzzwords
        1. Encapsulation
        2. Polymorphism
      8. When Not to Use Inheritance
      9. Multiple Inheritance
      10. Method Resolution Order
      11. Summary
    3. Chapter 17: Pythonic OOP: Properties and Dunder Methods
      1. Properties
        1. Turning an Attribute into a Property
        2. Using Setters to Validate Data
        3. Read-Only Properties
        4. When to Use Properties
      2. Python’s Dunder Methods
        1. String Representation Dunder Methods
        2. Numeric Dunder Methods
        3. Reflected Numeric Dunder Methods
        4. In-Place Augmented Assignment Dunder Methods
        5. Comparison Dunder Methods
      3. Summary
  6. Index
44.204.24.82