Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Yong Cui

Rating No ratings yet

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

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...
View in text
Excerpt 2
rent format specifiers: >>> create_formatted_records('^15') 1 Do homework 5 2 Laundry 3 3 Pay bills 4 >>> create_formatted_records('^18') 1 Do homework 5 2 L...
View in text
Excerpt 3
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...
View in text
Excerpt 4
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...
View in text
Excerpt 5
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...
View in text
Excerpt 6
rite understandable functions? 157 >>> help(generate_stats) Help on function generatate_stats in module __main__: generate_stats(measures: list) -> tuple Typ...
View in text
Excerpt 7
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...
View in text
Excerpt 8
the necessary arguments to the __init__ method: class Task: def __init__(self, title, desc, urgency): self.title = title self.desc = desc self.urgency = urge...
View in text
Tags
AI categories
PythonProgrammingSoftware
ISBN: 161729974X
Publisher: Manning Publicatons
Publish Year: 2023
Language: English
Pages: 506
File Format: PDF
File Size: 12.7 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…