Python: Do This, Not That!
Preface
This book shows how to write beautiful, idiomatic Python code with simple, effective examples. There are no long explanations, but the intention is to show and demonstrate preferred ways of writing code in Python. As such, the intended audience is for intermediate Python programmers looking to improve ways to write and express their code in a readable way while still preserving computational efficiency and effectiveness.
Under each major section, you will see two sub-sections: Don't Do This
and Do This
. Code under Don't Do This
are discouraged, and following the adjective of Jeff Knupp [JeK], are harmful. Code under Do This
are the encouraged, beautiful and idiomatic Pythonic way to write the code instead. All examples are geared for Python 3 (specifically, v3.7) and higher (though a lot of examples may work for Python 2).
Ideas and code examples are borrowed from [YaK, RaH, JeK].
To follow along and execute the code samples, you will need Docker installed. The Docker container is located on Docker Hub. After you have installed Docker, you may run the container as follows.
docker run -it \
-p 8888:8888 \
oneoffcoder/book-python-dothis
Note that this Docker container has Jupyter Lab running on port 8888
. You may access Jupyter Lab at http://localhost:8888 when the Docker container is running.
- 1. Collection initialization
- 2. Chained comparison operators
- 3. Falsy and truthy
- 4. Ternary operator
- 5. Looping over a range of numbers
- 6. Looping backward
- 7. Looping over a collection
- 8. Looping over a collection with indices
- 9. Looping over two collections
- 10. Use list comprehensions
- 11. Filtering a list
- 12. Most frequent item in list
- 13. Default dictionary values: defaultdict
- 14. Accessing a dictionary value with a default value
- 15. Updating dictionaries
- 16. Merging dictionaries
- 17. Using a dictionary to store counts
- 18. Dictionary comprehension
- 19. Set comprehension
- 20. Unpacking sequences and tuples
- 21. Ignoring unpacked values from a tuple
- 22. Avoid accessing tuple elements by index
- 23. String concatenation
- 24. String interpolation
- 25. String debug
- 26. Don’t repeat yourself (DRY)
- 27. String reversal
- 28. Clarify function calls with keyword arguments
- 29. Lambdas
- 30. Generator functions
- 31. New vs old classes
- 32. Classes and dunders (double underscores)
- 33. Enumerations
- 34. Transforming data with map, filter, reduce
- 35. Flattening data
- 36. Caching data and results
- 37. Reading a file
- 38. Deleting a file
- 39. Filtering files
- 40. Saving objects to file
- 41. Combinations of a list
- 42. Cycling through a list
- 43. Product of lists
- 44. Simultaneous state updates
- 45. Pandas apply and numpy vectorization
About
One-Off Coder is an educational, service and product company. Please visit us online to discover how we may help you achieve life-long success in your personal coding career or with your company’s business goals and objectives.
Copyright
Cite this book as follows.:
@misc{oneoffcoder_python_dothis_2019,
title={Python: Do This, Not That!},
url={https://python.oneoffcoder.com},
author={One-Off Coder},
year={2019},
month={Oct}}
Bibliography
Intermediate python. https://book.pythontips.com/en/latest/index.html. Accessed: 2019-10-18.
Transforming code into beautiful, idiomatic python. https://www.youtube.com/watch?v=OSGv2VnC0go. Accessed: 2019-10-18.
Writing idiomatic python. https://jeffknupp.com/writing-idiomatic-python-ebook/. Accessed: 2019-10-18.