Pydon'ts – Write elegant Python code
Book information
Description
Would you like me to teach you how to write Python ? code that is more elegant, more effective, and more expressive than what you have been writing until now... for free? I am looking to teach Python to people who want to master Python to the best of their abilities. Do you? If you do, then I know I can help you improve your skills. Python has dozens of built-ins, and all of them have multiple usage patterns. No human knows all of them by heart, but my research bundled them up nicely in a book for you to reference and learn from. With the help of the +20 chapters and +260 pages of this book, you will learn more about all the built-ins and their usage patterns, and your code will become more elegant, it will become more effective, and it will also become more expressive. Foreword Pydon’t disrespect the Zen of Python Zen of Python References Does elegance matter? Introduction Beware, opinions ahead Elegance is not a dispensable luxury Don’t write that, that’s unreadable The right tool for the job Optimise for rewrite Conclusion References Code style matters Introduction Code style Tools for your tool belt Conclusion References Naming matters Introduction Naming conventions PEP 8 recommendations Standard names Verbosity Picking a name Context is key Practical example Conclusion References Chaining comparison operators Introduction Chaining of comparison operators Pitfalls Ugly chains Examples in code Conclusion References Assignment expressions and the walrus operator := Walrus operator and assignment expressions Examples in code Conclusion References Truthy, Falsy, and bool “Truthy” and “Falsy” The __bool__ dunder method Remarks Examples in code Conclusion References Deep unpacking Introduction Assignments Examples in code Conclusion References Unpacking with starred assignments Starred Assignment Examples in code References EAFP and LBYL coding styles EAFP and LBYL EAFP instead of LBYL? Conclusion References Zip up Introduction How zip works Zip is lazy Three is a crowd Mismatched lengths Create a dictionary with zip Examples in code Conclusion References Enumerate me Introduction How enumerate works Optional start argument Unpacking when iterating Examples in code Conclusion References str and repr str and repr The __str__ and __repr__ dunder methods Examples in code Conclusion References Structural pattern matching tutorial Introduction Structural pattern matching Python could already do Your first match statement Pattern matching the basic structure Matching the structure of objects __match_args__ Wildcards Naming sub-patterns Traversing recursive structures Conclusion References Structural pattern matching anti-patterns Introduction There should be only one obvious way to do it A short and sweet if statement Be smart(er) Basic mappings Conclusion References Watch out for recursion Introduction Watch out for recursion Examples in code Conclusion References Sequence indexing Introduction Sequence indexing Maximum legal index and index errors Negative indices Indexing idioms To index or not to index? Best practices in code Conclusion Idiomatic sequence slicing Introduction Slicing syntax What to slice? Slicing from the beginning Slicing until the end Slicing with negative indices Slicing and range Idiomatic slicing patterns Empty slices More empty slices Examples in code Conclusion References Mastering sequence slicing Introduction Slicing step Sequence copying Manipulating mutable sequences More idiomatic slicing Conclusion References Inner workings of sequence slicing Introduction The slice class Getting items from sequences Setting items, deleting items, and container emulation Comma-separated indices and slices Examples in code Conclusion References Boolean short-circuiting Introduction Return values of the and and or operators Short-circuiting Short-circuiting in plain English all and any Short-circuiting in chained comparisons Examples in code Conclusion References The power of reduce Introduction How reduce works The rabbit hole of the built-in reductions Why bother? Far-fetched reductions The identity element… Why some people dislike reduce Examples in code Conclusion References Usages of underscore Introduction Recovering last result in the session Prefixes and suffixes for variable names Underscore as a sink Matching everything in the new match statement String localisation Improve number readability Conclusion References name dunder attribute Introduction What is __name__? The module attribute __name__ __name__ as an object type attribute Examples in code Conclusion References Bite-sized refactoring Introduction Refactoring What to refactor? Case study Conclusion References String translate and maketrans methods Introduction str.translate Non-equivalence to str.replace Generic translation tables str.maketrans Examples in code Conclusion References Boost your productivity with the REPL Introduction REPL Just fire up the REPL REPL mechanics The last result Getting help from within the REPL Tips for quick hacks Other tools Conclusion References set and frozenset Introduction (Mathematical) sets (Common) Operations on sets Differences between set and frozenset What are sets used for? Examples in code Conclusion References List comprehensions 101 Introduction What is a list comprehension? Anatomy of a list comprehension Example list comprehensions without filtering Example list comprehensions with filtering Equivalence with for loops Nesting for loops Nesting if statements Arbitrary nesting List comprehensions instead of map and filter Examples in code Conclusion References Conditional expressions Introduction What is a conditional expression? Rationale Short-circuiting Conditional expressions and if statements Precedence Conditional expressions that evaluate to Booleans Examples in code Conclusion References Pass-by-value, reference, and assignment Introduction Is Python pass-by-value? Is Python pass-by-reference? Python object model Python is pass-by-assignment Making copies Examples in code Conclusion References String formatting comparison Introduction String formatting rationale Three string formatting methods Value conversion Alignment Named placeholders Accessing nested data structures Parametrised formatting Custom formatting Examples in code Conclusion References Closing thoughts
Similar books
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36
2010 · PDF
THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.
1858 · PDF
Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.
2022 · PDF
The travels of Capts. Lewis and Clarke from St. Louis, by way of the Missouri and Columbia rivers, to the Pacific ocean; performed in the years 1804, 1805 & 1806, by order of the government of the United States. Containing delineations of the manners, customs, religion, &c. of the Indians, comp. from various authentic sources, and original documents, and a summary of the Statistical view of the Indian nations, from the official communication of Meriwether Lewis. Illustrated with a map of the country, inhabited by the western tribes of Indians
1809 · PDF
Professional Linux kernel architecture ''Wrox programmer to programmer''--Cover. - ''What you are reading right now is the result of an evolution over more than seven years: After two years of writing, the first edition was published in German by Carl Hanser Verlag in 2003. It then described kernel 2.6.0. The test was used as a basis for the low-level design documentation for the EAL4+ security evaluation of Red Hat Enterprise Linux 5, requiring to update it to kernel 2.6.18 (if the EAL acronym does not mean anything to you, then Wikipedia is once more your friend). Hewlett-Packard sponsored the translation into English and has, thankfully, granted the rights to publish the result. Updates to kernel 2.6.24 were then performed specifically for this book''--P. ix
2008 · PDF