python_how-to_chapter_1

Python How-To - Chapter 1

Return to Python How-To Table of Contents, Python How-To, Python, Python Bibliography, Manning Python Series, Manning Data Science Series, Manning Books Purchased by Cloud Monk, Manning Bibliography, Cloud Monk's Book Purchases, Cloud Monk Library

“ (PyHow2 2023)

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

” (PyHow2 2023)

Python is an amazing programming language. Its open source, general-purpose, platform-independent nature has given it an enormous developer community, along with an incredible ecosystem that includes tens of thousands of freely available libraries for web development, machine learning (ML), data science, and many other domains. I hope that we share this belief: knowing how to code in Python is great, but knowing how to write truly efficient, secure, and maintainable applications gives you a tremendous advantage. This book will help you go from a Python beginner to confident programmer.” (PyHow2 2023)

“In the Python ecosystem, we use domain-specific Python tools, such as web frameworks and ML libraries, to complete various tasks in our jobs. The effective employment of these tools is nontrivial, as it requires considerable 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 is generally better than the others because it may be more concise, more readable, or more efficient, which we collectively term as Pythonic: an idiomatic coding style that all Python programmers strive to acquire. This book is about how to write Pythonic code to address programming tasks.” (PyHow2 2023)

Python is so well developed and has so many features to learn that it would be impossible or unwise to try to learn everything about it from this book. 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, I'll frequently mention how to use these skills with the consideration]] of readability and maintainability so that you can form good coding habits, which I'll bet that you and your teammates will greatly appreciate.” (PyHow2 2023)

Note You'll see callouts like this one throughout the book. Many of them are devoted to tips regarding readability and maintainability. Don't miss them!

1.1 Aiming at becoming a pragmatic programmer

We code for purposes, such as building websites, training ML models, or analyzing data. Whatever our purposes are, we want to be pragmatic; we write code]] to solve real 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 about our intentions. But even if you're unsure of what you desire to achieve with Python at this stage, the good news is that core Python features are universal knowledge. After you grasp the core features, you can apply them to any domain-specific Python tools.

Aiming to become a pragmatic programmer means that you should focus on the techniques that are most useful. Mastering these skills is just the first milestone in your journey, however; the long-term game in coding is writing readable code that not only works, 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. What we can control is how we write the code — how readable we make it. Consider some simple questions:

Are your variables named properly to indicate what they are? No one can appreciate your code if it's full of variables named var0, temp_var, or x, for example.

Do your functions have proper signatures to indicate what they do? People are lost if they see functions named do_data(data) or run_step1().

Do you organize your code consistently across files? People expect different files of the same type to use similar layouts. Do you place import statements at the top of your files, for example?

Is your 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 example questions pertain to readability. We don't just ask them from time to time; instead, we ask these kinds of readability questions throughout our projects. 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 practicing readability through these self-checking questions, we're training our brain's neural circuits. In the long term, your brain will be trained to know what behaviors constitute good practice in coding, and you'll write readable and maintainable code without even thinking about it.

1.1.2 Considering maintainability even before you write any code

In rare cases, we write code]] for one-time]] use. When we write a script, we almost always succeed in convincing ourselves that we'll never use the script again; thus, we don't care about creating good variable names, laying out the code properly, or refactoring functions and data models, not to mention making sure that we leave no comments (or outdated ones). But how many times did it turn out that we had to use the same script the next week or even the following day? This has probably happened to most of us.

The previous paragraph describes a mini-scale maintainability problem. In this case, it affects only your own productivity in a short span of time. If you work in a team environment, however, problems introduced by individual contributors add up to large-scale maintainability problems. The team members fail to follow the same naming rules for variables, functions, and files. Countless incidents of commented-out code remain. Outdated comments are everywhere.

To address maintainability problems in a later stage of your own projects, you should build a good mindset when you're learning to code. Following are some questions that you might consider to help you develop a goodmaintainability” mindset for the long run:

Is your code free of outdated comments and commented-out code? If the answer is no, update or delete them! These situations are even worse than those without any comments because they may provide conflicting information.

Is there considerable duplication in the code? If the answer is yes, refactoring is probably warranted. A rule of thumb in coding is DRY (Don't Repeat Yourself). By removing duplicates, you'll deal with a single shared portion, which is less prone to bugs than changes in repeated parts.

Do you use version-control tools such as Git? If the answer is no, look at the extensions or plugins of your integrated development environment (IDE). For Python, common IDEs include PyCharm and Visual Studio Code. Many IDEs have integrated version-control tools that make it much easier to manage versions.

Being a pragmatic Python programmer requires this type of maintainability training. After all, almost all Python tools are open source and evolving rapidly. Thus, maintainability should be the cornerstone of any viable project. Throughout the book, where applicable, we'll touch base on how to implement maintainability practices in our daily Python coding. Please re[[member that readability is the 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 as well as other languages

Python owes its growing popularity to the characteristics of the language itself. Although none of these characteristics is unique to Python, when they were organically combined, Python was set to grow into a widely adopted language. The following list summarizes Python's key characteristics:

Cross-platformPython runs on common platforms, such as Windows, Linux, and MacOS. Thus, Python code is transferrable. Any code that you write on your own platform can run on other computers without any restrictions imposed by the differences between platforms.

Expressive and readablePython's syntax is simpler than that of many other languages. The expressive, readable coding style is widely adopted by Python programmers. You'll find that well-written Python code is enjoyable to read, just like well-written prose.

Fast for prototyping — Given its simple syntax, Python code is generally more concise than code written in other languages. Thus, it requires less work to produce a functional prototype in Python than in other languages.

Standalone — When you install Python on your computer, it becomes ready to use right after “unboxing.” The basic Python installation package consists of all essential libraries that you need to perform any routine coding work.

Open source, free, and extensible — Although Python works standalone, you can write and use your own packages. If others have published any packages you need, you can install them with a one-line command without worrying about license or subscription fees.

These key characteristics have attracted many programmers, forming a tremendous developer community. The open source nature of Python allows interested users to contribute to this language and its ecosystem in general. Table 1.1 summarizes some notable domains and their respective Python tools. This table isn't an exhaustive list, and you're encouraged to explore Python tools in the specialty domain of your own interest.

Table 1.1 Overview of domain-specific Python tools

Domain

Tool

Highlights

Web development

Flask

A micro web framework; good for building lightweight web apps; flexible extensibility for third-party functionalities

Web development

Django

A complete web framework; good for building database-driven web apps; highly scalable as an enterprise solution

FastAPI

A web framework for building application programming interfaces (APIs); data validation and data conversion; automatic generation of API web interfaces

Streamlit

A web framework for easy building of data-related apps; popular among data scientists and ML engineers

Data science

NumPy

Specialized for processing large, multidimensional arrays; high computational efficiency; integral to many other libraries

pandas

A versatile package for processing spreadsheet-like two-dimensional data; comprehensive data manipulations

statsmodels

A popular package for statistics, such as linear regression]], correlation, Bayesian modeling, and survival analysis

Matplotlib

An object-oriented paradigm for drawing histograms, scatter plots, pie charts, and other common figures with a variety of customizable settings

Seaborn

An easy-to-use visualization library for drawing attractive graphics; high-level APIs based on Matplotlib

Machine learning

Scikit-learn

A wide range of preprocessing tools for building ML models; implementation of common ML algorithms

TensorFlow

A framework with both high- and low-level APIs; Tensor board visualization tool; good for building complex neural networks

Keras

High-level APIs for building neural networks; easy to use; good for building low-performance models

PyTorch

A framework for building neural networks; more intuitive code styles than TensorFlow; good for building complex neural networks

FastAI

High-level APIs for building neural networks on top of PyTorch; easy to use

Frameworks, libraries, packages, and modules

When we discuss tools, we use several closely related terms, including frameworks, libraries, packages, and modules. Different languages may use some of these terms and have slightly different meanings. Here, I discuss the meanings of these terms that most Python programmers accept.

Frameworks have the largest scope. Frameworks provide a complete set of functionalities that are designed to perform a dedicated job at a high level, such as web development.

Libraries are building blocks of frameworks, consisting of packages. Libraries provide functionalities without users having to worry about the underlying packages.

Packages provide specific functionalities. More specifically, packages bundle modules, and each module consists of a set of closely related data structures and functions in a single file, such as a .py file.

1.3 What Python can't do or can't do well

Everything has limits, and so does Python. There are many things that Python can't do, or at least can't do well compared with alternative tools. Although some people are trying to push Python in such a way that we can use it for other purposes, at this stage, we should know its limits in two important areas:

Mobile applications — In this mobile age, we all have smartphones and use apps in almost every aspect of life, such as banking, online shopping, health, communications, and certainly gaming. Unfortunately, there have been no great Python frameworks for developing smartphone apps despite attempts such as Kivy and BeeWare. If you work in mobile development, you should consider mature alternatives such as Swift for iOS apps and Kotlin for Android apps. As a pragmatic programmer, you choose a language that leads to a product with the best user experience.

Low-level development — When it comes to developing software that interacts directly with hardware, Python isn't the best choice. Due to the interpreted nature of Python, the overall execution speed isn't fast enough for developing low-level software, such as device drivers, which require instant responsiveness. If you're interested in developing software at a low level, you should consider alternative languages that are better at interfacing with the hardware. C and C++ are good options for developing device drivers, for example.

1.4 What you'll learn in this book

We've talked a little bit about what it means to be a pragmatic programmer. Now let's talk about how you're going to get there. As you write programs, you'll inevitably run into new programming challenges. In this book, we've identified the programming techniques you'll need to take for the tasks you're most likely to encounter.

1.4.1 Focusing on domain-independent knowledge

All things are connected in some way directly or indirectly, and so is Python knowledge. To put this discussion]] in a context, consider figure 1.1. We can conceptualize Python features and its applications as three related entities.

Figure 1.1 The relationship between domain-independent and domain-specific Python knowledge. Domain-independent knowledge includes basic and advanced Python features, which are closely related. Together, they form the basis for domain-specific knowledge in distinct content domains.

The goal of learning Python for most of us is to apply Python to address problems in the domain where we work, which requires domain-specific Python knowledge, such as web development and data science. As a prerequisite for fulfilling your job, your knowledge base should encompass essential Python features — more specifically, domain-independent Python knowledge. Even when your job role switches or evolves, you can apply the essential Python knowledge to your new position.

In this book, you'll focus on gaining domain-independent Python knowledge. To facilitate the learning process, we can operationally define domain-independent Python knowledge as two building components: the basic and the advanced.

For the basics, we should know common data structures and their manipulations. We also need to know how to evaluate conditions to construct the if…elsestatement. When we perform repeated work, we can take advantage of for and while loops. To reuse blocks of code, we can refactor them into functions and classes. Mastering these basics is sufficient for writing useful Python code to perform your job tasks. If you know most of the basics, you're ready to learn the advanced skills.

The advanced skills enable you to write better code that's more efficient and that takes advantage of versatile Python features. Let's see a simple example to feel the versatility of Python. When we use a for loop to iterate a list object, we often need to show the position of each item beside the item itself, such as

prime_numbers = [2, 3, 5] # desired output: Prime Number

  1. 1: 2 Prime Number #2: 3 Prime Number #3: 5

If we use only the basic features, we may come up with the following solution. In the solution, we create a range object that allows retrieval of the 0-based index to produce the position information. For the output, we use string concatenation:

for num_i in range(len(prime_numbers)): num_pos = num_i + 1 num = prime_numbers[num_i] print(“Prime Number

  1. ” + str(num_pos) + “: ” + str(num))

However, after you read this book, you'll become a more experienced Python user and should be able to produce the following solution that is cleaner and more Pythonic:

for num_pos, num in enumerate(prime_numbers, start=1): print(f“Prime Number

  1. {num_pos}: {num}”)

The above solution involves three techniques: tuple unpacking to obtain num_pos and num (section 4.4), creating the enumerate object (section 5.3), and formatting the output using f-strings (section 2.1). I'm not going to expand the discussion]] of these techniques here since they'll be covered in their respective sections. Nevertheless, this example is simply showing you what this book is all about — how to use a variety of techniques to produce Pythonic solutions.

Besides these techniques, you'll learn and apply advanced function concepts, such as decorators and closures, for example. When you define classes, you'll know how to make them work together to minimize the code and reduce the potential for bugs. When your program is done, you'll know how to log and test your code to make it production-ready.

This book is all about synthesizing domain-independent Python knowledge. You'll not only learn pragmatic advanced features, but also basic Python features and fundamental computer programming concepts where applicable. The key term here is synthesizing, as discussed in section 1.4.2.

1.4.2 Solving problems through synthesis

A common dilemma that beginners run into is that they seem to know a variety of techniques, but don't know how and when to use them to solve problems. For each technique we discuss in this book, we'll show you how it works independently, and we'll also show you how it fits with other techniques. We hope that you'll start to see how all the different pieces can be composed into an infinite number of new programs.

As a fundamental note on learning and synthesizing various techniques, you should expect that learning to code isn't a linear path. After all, Python's technical features are closely interrelated. Although you'll focus on learning intermediate and advanced Python techniques, they can't be isolated completely from basic vs. Instead, you'll notice that I'll frequently make remarks on basic techniques or intentionally reiterate techniques that I've already covered.

1.4.3 Learning skills in context

As we mentioned earlier, this book focuses on learning skills that are built on domain-independent Python knowledge. Being domain-independent means that you can apply the skills covered in this book to any domain where you'd like to use Python. It's almost impossible to learn anything without an example, however. We'll show most techniques in this book by using an ongoing project to provide a consistent context within which to discuss specific skills. If you're familiar with a particular skill, you can skip to the section's Discussion]] part, in which I'll discuss some key aspects of the covered skills.

As a heads-up, the generic project is a task-management web app. In the application, you can manage tasks, including adding, editing, and removing tasks — everything that will be implemented with pure Python, such as data models, functions, classes, and anything else you can think of that an application may have. Moving forward, the important thing to note is that the goal is not to get a perfect, shiny application from this book. Instead, you want to learn all the essential Python techniques in the process of creating this web app so you can apply your domain-independent knowledge to projects in your own jobs.

Summary

“It's critical for you to build a pragmatic learning strategy. By focusing on learning the domain-independent features of Python, you'll get yourself ready for any Python-related job role.” (PyHow2 2023)

Python is a general-purpose, open source programming language that fosters a tremendous community of developers who make and share Python packages.” (PyHow2 2023)

Python is competitive in many domains, including web development, data science, and ML. Each domain has specific Python frameworks and packages that you can use.” (PyHow2 2023)

Python has its limitations. If you consider developing mobile apps or low-level device drivers, you should use Swift, Kotlin, Java, C, C++, Rust, or any other applicable language.” (PyHow2 2023)

“I make a distinction between domain-independent Python knowledge and domain-dependent Python knowledge. This book focuses on teaching domain-independent Python knowledge.” (PyHow2 2023)

Learning to code is not a linear path. Although you'll learn advanced features in this book, I'll frequently mention basic ones. Also, you'll encounter some difficult vs, which will create an upward spiral learning path.

The essential recipe for learning Python or any programming language is synthesizing individual technical skills to form a comprehensive skill set. Through the synthesis process, you'll learn the language in a pragmatic way, knowing what works for the problem that you're addressing.

“ (PyHow2 2023)

Fair Use Sources

Python: Python Variables, Python Data Types, Python Control Structures, Python Loops, Python Functions, Python Modules, Python Packages, Python File Handling, Python Errors and Exceptions, Python Classes and Objects, Python Inheritance, Python Polymorphism, Python Encapsulation, Python Abstraction, Python Lists, Python Dictionaries, Python Tuples, Python Sets, Python String Manipulation, Python Regular Expressions, Python Comprehensions, Python Lambda Functions, Python Map, Filter, and Reduce, Python Decorators, Python Generators, Python Context Managers, Python Concurrency with Threads, Python Asynchronous Programming, Python Multiprocessing, Python Networking, Python Database Interaction, Python Debugging, Python Testing and Unit Testing, Python Virtual Environments, Python Package Management, Python Data Analysis, Python Data Visualization, Python Web Scraping, Python Web Development with Flask/Django, Python API Interaction, Python GUI Programming, Python Game Development, Python Security and Cryptography, Python Blockchain Programming, Python Machine Learning, Python Deep Learning, Python Natural Language Processing, Python Computer Vision, Python Robotics, Python Scientific Computing, Python Data Engineering, Python Cloud Computing, Python DevOps Tools, Python Performance Optimization, Python Design Patterns, Python Type Hints, Python Version Control with Git, Python Documentation, Python Internationalization and Localization, Python Accessibility, Python Configurations and Environments, Python Continuous Integration/Continuous Deployment, Python Algorithm Design, Python Problem Solving, Python Code Readability, Python Software Architecture, Python Refactoring, Python Integration with Other Languages, Python Microservices Architecture, Python Serverless Computing, Python Big Data Analysis, Python Internet of Things (IoT), Python Geospatial Analysis, Python Quantum Computing, Python Bioinformatics, Python Ethical Hacking, Python Artificial Intelligence, Python Augmented Reality and Virtual Reality, Python Blockchain Applications, Python Chatbots, Python Voice Assistants, Python Edge Computing, Python Graph Algorithms, Python Social Network Analysis, Python Time Series Analysis, Python Image Processing, Python Audio Processing, Python Video Processing, Python 3D Programming, Python Parallel Computing, Python Event-Driven Programming, Python Reactive Programming.

Variables, Data Types, Control Structures, Loops, Functions, Modules, Packages, File Handling, Errors and Exceptions, Classes and Objects, Inheritance, Polymorphism, Encapsulation, Abstraction, Lists, Dictionaries, Tuples, Sets, String Manipulation, Regular Expressions, Comprehensions, Lambda Functions, Map, Filter, and Reduce, Decorators, Generators, Context Managers, Concurrency with Threads, Asynchronous Programming, Multiprocessing, Networking, Database Interaction, Debugging, Testing and Unit Testing, Virtual Environments, Package Management, Data Analysis, Data Visualization, Web Scraping, Web Development with Flask/Django, API Interaction, GUI Programming, Game Development, Security and Cryptography, Blockchain Programming, Machine Learning, Deep Learning, Natural Language Processing, Computer Vision, Robotics, Scientific Computing, Data Engineering, Cloud Computing, DevOps Tools, Performance Optimization, Design Patterns, Type Hints, Version Control with Git, Documentation, Internationalization and Localization, Accessibility, Configurations and Environments, Continuous Integration/Continuous Deployment, Algorithm Design, Problem Solving, Code Readability, Software Architecture, Refactoring, Integration with Other Languages, Microservices Architecture, Serverless Computing, Big Data Analysis, Internet of Things (IoT), Geospatial Analysis, Quantum Computing, Bioinformatics, Ethical Hacking, Artificial Intelligence, Augmented Reality and Virtual Reality, Blockchain Applications, Chatbots, Voice Assistants, Edge Computing, Graph Algorithms, Social Network Analysis, Time Series Analysis, Image Processing, Audio Processing, Video Processing, 3D Programming, Parallel Computing, Event-Driven Programming, Reactive Programming.


Python Glossary, Python Fundamentals, Python Inventor: Python Language Designer: Guido van Rossum on 20 February 1991; PEPs, Python Scripting, Python Keywords, Python Built-In Data Types, Python Data Structures - Python Algorithms, Python Syntax, Python OOP - Python Design Patterns, Python Module Index, pymotw.com, Python Package Manager (pip-PyPI), Python Virtualization (Conda, Miniconda, Virtualenv, Pipenv, Poetry), Python Interpreter, CPython, Python REPL, Python IDEs (PyCharm, Jupyter Notebook), Python Development Tools, Python Linter, Pythonista-Python User, Python Uses, List of Python Software, Python Popularity, Python Compiler, Python Transpiler, Python DevOps - Python SRE, Python Data Science - Python DataOps, Python Machine Learning, Python Deep Learning, Functional Python, Python Concurrency - Python GIL - Python Async (Asyncio), Python Standard Library, Python Testing (Pytest), Python Libraries (Flask), Python Frameworks (Django), Python History, Python Bibliography, Manning Python Series, Python Official Glossary - Python Glossary, Python Topics, Python Courses, Python Research, Python GitHub, Written in Python, Python Awesome List, Python Versions. (navbar_python - see also navbar_python_libaries, navbar_python_standard_library, navbar_python_virtual_environments, navbar_numpy, navbar_datascience)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


python_how-to_chapter_1.txt · Last modified: 2024/04/28 03:52 by 127.0.0.1