1. Pythonic Thinking¶
The idioms of a programming language are defined by its users. Over the years, the Python community has come to use the adjective Pythonic to describe code that follows a particular style. The Pythonic style isn’t regimented or enforced by the compiler. It has emerged over time through experience using the language and working with others. Python programmers prefer to be explicit, to choose simple over complex, and to maximize readability. (Type import this into your interpreter to read The Zen of Python.)
Programmers familiar with other languages may try to write Python as if it’s C++, Java, or whatever they know best. New programmers may still be getting comfortable with the vast range of concepts that can be expressed in Python. It’s important for you to know the best—the Pythonic—way to do the most common things in Python. These patterns will affect every program you write.
- 1.1. Know Which Version of Python You’re Using
- 1.2. Follow the PEP 8 Style Guide
- 1.3. Know the Differences Between bytes and str
- 1.4. Prefer Interpolated F-Strings Over C-style Format Strings and str.format
- 1.5. Write Helper Functions Instead of Complex Expressions
- 1.6. Prefer Multiple Assignment Unpacking Over Indexing
- 1.7. The range built-in function is useful for loops
- 1.8. Use zip to Process Iterators in Parallel
- 1.9. Avoid else Blocks After for and while Loops
- 1.10. Prevent Repetition with Assignment Expressions