Have you ever asked yourself, “How do I do that in Python?” If so, you’ll love this practical collection of the most important Python techniques. Python How-To is full of techniques and best practices for writing readable and maintainable Python code, with careful cross-referencing that reveals how the same concept can be used in different contexts. Each chapter stands alone, so you can dip in whenever you need a practical solution to a common Python problem. Whether you’re doing data science, building web applications, or writing admin scripts, you’ll find answers to your “how-to” questions in Python How-To. Author Yong Cui’s clear and practical writing is perfect for beginners and veterans alike, with insightful code snippets, interesting graphics, and challenging exercises. Along the way, you’ll learn how to take advantage of Python’s versatile tools and libraries.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A practical, technique-driven guide to writing clearer and more maintainable Python, organized as 63 self-contained "how do I…?" answers you can dip into on demand. Best for beginner-to-intermediate developers who already know some Python and want to level up their everyday code.
【Book Arc】
- **Opening (~0%–10%)**: Frames the whole book around a maintainability mindset—naming, avoiding duplication (DRY), cleaning up stale comments, and using version control—before any syntax.
- **Early (~10%–32%)**: Part 1 on built-in data models: strings and formatting, safe use of eval/exec, regular expressions (including raw strings), and choosing between lists, tuples, dictionaries, and sets.
- **Middle (~32%–48%)**: Deeper container and sequence work—sorting complex data with custom functions, accessing dict keys/values/items, hashing, set operations, and slicing/slice surgery on mutable sequences.
- **Late (~48%–70%)**: Functions in depth—return values, type hints, *args/**kwargs, docstrings, lambdas, functions as objects, and decorators for measuring performance.
- **Ending (~70%–100%)**: The excerpts do not cover this range; based on the table of contents, later material moves into object-oriented and beyond-the-basics topics, but specifics are not shown here.
【Key Takeaways】
- **Maintainability is a mindset, not a cleanup phase** (Opening): naming, DRY refactoring, and version control pay off long after the script "works."
- **Format strings are a first-class tool** (Early): f-strings with nested format specifiers let you parameterize output cleanly instead of hardcoding layouts.
- **eval and exec are dangerous by default** (Early): they can crash or destroy data; prefer `ast.literal_eval` or manual parsing when handling untrusted strings.
- **Raw strings tame regex backslash chaos** (Early): `r"\\task"` is far more readable than quadruple-escaped literals, and this matters as patterns grow.
- **Choose containers by mutability and semantics** (Middle): lists for homogeneous mutable sequences, tuples for distinct fixed records, dicts/sets when you need hashing and fast membership.
- **Sorting complex data needs a key function** (Middle): `list.sort()` fails on dicts; custom functions unlock meaningful orderings.
- **Slicing is more than reading** (Middle): slice surgery on mutable sequences supports replacement, extension, shrinkage, and removal.
- **Functions are objects with rich contracts** (Late): type hints, docstrings, lambdas, higher-order functions, and decorators all shape how readable and reusable your functions are.
【Reading Tips】
- Treat it as a reference: jump to the technique you need rather than reading linearly, since each chapter stands alone.
- Deep-read the maintainability framing in the opening—it colors every later technique.
- Slow down on regex escaping and eval/exec safety; these are the highest-risk spots for subtle bugs.
- Work the "Challenge" exercises at the end of sections; they force you to apply the technique rather than just recognize it.
- Skim code listings you already understand and focus on the "Discussion" notes, which carry the design rationale.
【Coverage Limits】
This guide is based on stratified excerpts covering roughly the first half of the book (through functions); later chapters on object-oriented programming and advanced topics are not represented, so claims about the ending are inferred only from the table of contents.
Page 10
Examining the basic structure of a function’s docstring 167 Specifying the function’s action as the summary 168 ■ Documenting the parameters and the return v...
e choose list or tuple to store the latitude and longitude? HINT Consider whether the stored data is mutable and/or homogeneous to help you make the decision...
cally distinct, and these items form a structural sequence. Default sorting can sort a list only by using the numeric or lexicographic order, which is rather...
pping is a file-compression concept. In Python, the zipfile module provides the related functionalities of zipping and unzipping files. 5.1.4 Discussion Besi...
rite understandable functions? 157 >>> help(generate_stats) Help on function generatate_stats in module __main__: generate_stats(measures: list) -> tuple Typ...
same docstring and name that matches the inner function! We can’t make things happen this way. Fortunately, Python provides a solution: we can use the wraps...
the necessary arguments to the __init__ method: class Task: def __init__(self, title, desc, urgency): self.title = title self.desc = desc self.urgency = urge...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Python How-To 63 techniques to improve your Python code (Final Release) (Yong Cui)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Python How-To 63 techniques to improve your Python code (Final Release) (Yong Cui)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment