ebook img

Python How-To (MEAP V10) PDF

664 Pages·2023·11.576 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python How-To (MEAP V10)

Python How-To MEAP V10 1. Copyright_2023_Manning_Publications 2. welcome 3. 1_Developing_a_pragmatic_learning_strategy 4. 2_Processing_and_formatting_strings 5. 3_Using_built-in_data_containers 6. 4_Dealing_with_sequence_data 7. 5_Iterables_and_iterations 8. 6_Defining_user-friendly_functions 9. 7_Using_functions_beyond_the_basics 10. 8_Defining_user-friendly_classes 11. 9_Using_classes_beyond_the_basics 12. 10_Fundamentals_of_objects 13. 11_Dealing_with_files 14. 12_Logging_and_exception_handling 15. 13_Debugging_and_testing 16. 14_Completing_a_real_project 17. Appendix_A._Learn_Python_with_REPL 18. Appendix_B._Managing_Python_packages_with_pip 19. Appendix_C._Using_Jupyter_Notebook_–_A_web- based_interactive_Python_editor 20. Appendix_D._Integrating_version_control_in_your_project 21. Appendix_E._Preparing_your_package_for_public_distribution 22. Appendix_F._Solutions_to_the_challenges MEAP Edition Manning Early Access Program Python How-To Version 10 Copyright 2023 Manning Publications ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. https://livebook.manning.com/#!/book/python-how-to/discussion For more information on this and other Manning titles go to manning.com welcome Dear Readers, Thank you for purchasing the MEAP for Python How-To. As a scientist, I have been using Python for more than five years. As a self-learner, I know how important but also hard to find the right learning material, because there are just too many choices — YouTube videos, blog articles, online courses, and books. Among them, books are my favorite because compared to other media, text provides the most amount of information. After learning through several Python books and years of first-hand use experience, I put a great deal of efforts into this book, trying to consolidate all essential techniques about writing readable and maintainable Python code. I hope that this book can give you a head start in writing better Python code to fulfill your job duties. As you may realize, Python is so versatile that it has a plethora of tools for many specialty domains, such as finance, engineering, data science, and artificial intelligence. If you're entering or about to enter one of these domains, knowing Python will make your work much easier because of the access to these handy tools. However, using these tools requires you have a solid understanding of the essential Python skills, which are this book's focus — teaching you domain-independent Python knowledge. No matter how sophisticated your domain-specific Python tool is, it won't take long for you to figure it out! To get most out of this book, you may have used Python for several months and have some basic understanding of data models and functions. If you have additional background in programming, it will help too. Anyhow, even if you don't have any Python or programming experience, you can still get started with Python using this book, because each section is filled with realistic scenarios and code examples. Thus, you can simply copy and paste the code to run, without requiring you to do anything to learn! I hope you'll enjoy this book. If you have any questions, comments, or suggestions, please share them in Manning’s liveBook discussion forum for my book. —Yong Cui In this book Copyright 2023 Manning Publications welcome brief contents 1 Developing a pragmatic learning strategy 2 Processing and formatting strings 3 Using built-in data containers 4 Dealing with sequence data 5 Iterables and iterations 6 Defining user-friendly functions 7 Using functions beyond the basics 8 Defining user-friendly classes 9 Using classes beyond the basics 10 Fundamentals of objects 11 Dealing with files 12 Logging and exception handling 13 Debugging and testing 14 Completing a real project Appendix A. Learn Python with REPL Appendix B. Managing Python packages with pip Appendix C. Using Jupyter Notebook – A web-based interactive Python editor Appendix D. Integrating version control in your project Appendix E. Preparing your package for public distribution Appendix F. Solutions to the challenges 1 Developing a pragmatic learning strategy This chapter covers What being pragmatic means What Python can do When you should consider alternative languages What you can expect to learn from this book As one of the most popular languages, Python is the choice of language for many first-time coding learners. Its open-source, general-purpose, platform- independent nature has engendered probably the most active developer community, which has resulted in tens of thousands of freely available libraries for web development, machine learning (ML), data science, and many other specialties. No matter whether you just started with coding in Python or programming in general, I hope that we share the same belief: we’ve entered an era in which knowing to code in Python gives you a tremendous advantage — you’re having access to numerous tools that Python has to offer. In the Python ecosystem, we use domain-specific Python tools, such as web frameworks and ML libraries, to address specific tasks in our jobs. The effective employment of these tools is nontrivial, as it requires a considerable breadth of familiarity with essential Python skills, such as processing texts, dealing with structured data, creating control flows, and handling files. Python programmers can write different solutions to address the same tasks. Among these solutions, one of them is probably better than the remaining, because it may be more concise, more readable, or more efficient, which we term as Pythonic — an idiomatic coding style that all Python programmers strive to acquire. It is what this book is about — how to write Pythonic code to address programming tasks. We shall recognize that Python is so well-developed and there are just too many features to learn. Thus, it’s impossible or unwise if we try to learn everything about Python. Instead, I’ll take a pragmatic approach to defining what I’ll teach in this book —the essential skills that you’ll most likely use in your projects. Equally important is that I’ll frequently mention how to use them with the consideration of readability and maintainability such that you can form good coding habits, which, I bet, you and your teammates will much appreciate. Note You'll see callouts like this throughout the book. Many of them are devoted to tips regarding readability and maintainability. Don't miss them out! 1.1 Aiming at becoming a pragmatic programmer We code for purposes: building websites, training ML models, or analyzing data. Whatever the purposes are, we want to be pragmatic — we write code to solve problems. Thus, before we learn to code from the beginning or advance our coding skills in the middle of our career, we should be clear with our intentions. However, even if you're unsure about what you desire to achieve with Python at this stage, the good news is that core Python features are universal knowledge. Once you grasp the core features, it'll become much easier for you to learn any domain-specific Python tools. Aiming at becoming a pragmatic programmer means that you should focus on techniques that are most useful. Mastering these skills is just a first milestone of your journey, and the long-term game in coding is writing readable code that doesn't only work but also fosters maintainability. 1.1.1 Focusing on writing readable Python code As a developer, I’m obsessed with readability. Writing code is like speaking a real-world language. When we speak a language, don't we want others to understand us? If your answer is yes, you probably agree with me that we want others to understand our code too. Whether our code’s readers possess the necessary technical expertise to understand our code is out of our control. But what we can control is how we write the code — how readable our code is. Consider some simple questions: Are my variables properly named to indicate what they are? None can appreciate if your code is full of variables named as var0, temp_var, or x. Do my functions have proper signatures to indicate what they do? People are lost if they see functions named as do_data(data) or run_step1(). Do I organize my code consistently across different files? People expect that different files of the same type use similar layouts. For instance, do you place import statements at the top of your files? Is my project folder structured with specific files stored in the desired folders? When your project’s scope grows, you should create separate folders for related files. These are a few example questions pertaining to readability. We don’t just ask them from time to time. Instead, we continuously ask ourselves these and many other readability-related questions throughout the project. The reason is simple — (good) practice makes perfect. Trained as a neuroscientist, I know exactly how the brain works when it comes to behavioral learning. By focusing on readability through these self-checking questions, we’re training our brain's neural circuits. In the long term, our brain will be trained to know what behaviors constitute good practice of readability, making you write readable and maintainable code even without your consciousness. 1.1.2 Considering maintainability even before you write any code In rare cases, we write code just for one-time use. When we write a script, we almost always succeed in convincing ourselves that we'll never use the script again, and thus, we don’t care about giving good variable names, laying out the code properly, refactoring functions and data models, and not to mention leaving no or outdated comments. However, how many times did it turn out that we would have to use the same script the next week or even the following day? It probably has happened to most of us. The previous paragraph describes a mini-scale maintainability issue. In this case, it only affects your own productivity in a short span of time. However, if you work in a team environment, issues from individual contributors add up to large-scale maintainability issues. The team members fail to follow the same naming rules for variables, functions, and files. There are countless incidents of commented-out code. Outdated comments are everywhere. To address maintainability issues in a later stage in your own projects, you should build a good mindset when we're learning to code at this stage. The following are some questions that you may consider to develop a good "maintainability" mindset in the long run. Is your code free of outdated comments and commented-out code? If no, update or delete them! These are situations that are even worse than those without any comments because of conflicting information that they may provide. Is there any considerable duplication in the code? If yes, refactoring is probably warranted. A rule of thumb in coding is DRY – don’t repeat yourself. By removing duplicates, you’ll deal with just a single shared portion, which is less prone to bugs compared to the situation when you must track the changes of repeated parts. Do you use version control tools, like git? If no, look at the extensions or plugins of your IDEs (Integrated Development Environment; for Python, common IDEs include PyCharm and Visual Studio Code), many of which have integrated version control tools and made it much easier to manage versions. Being a pragmatic Python programmer requires these maintainability trainings. After all, almost all Python tools are open source and rapidly evolving. Thus, maintainability should be the cornerstone of any viable project. Throughout the book, where applicable, we’ll be touching base on how to implement maintainability practices in our daily Python coding. Please remember that readability is key to sustained maintainability. When you focus on writing readable code, your codebase’s maintainability improves consequentially. 1.2 What Python can do well or do equally well as

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.