Learn How to Program in Python

Learn How to Program in Python

Oct 13, 2022   ·  15 min read

Python is the most popular programming language in the world. This article is for beginners who want a comprehensive overview of the language to help get them started.

According to the the TIOBE Index at the time of this writing, Python is the most popular programming language in the world. And for good reason! You can do all sorts of things with Python

The language has found its way into a tremendous number of disciplines, such as Web/App Development (see Django, Flask, and fastapi), Data Analytics (see pandas, dash, and plotly), Data Science and Machine Learning (see scikit-learn, tensorflow, and streamlit), Data Engineering (see psycopg2, apache-airflow, and boto3), Hardware Development (see the Raspberry Pi project), and even Game Development (see pygame)! It's no wonder why it's the most popular programming language out there.

A Brief History

Python has come to be known as the "Swiss army knife" of programming due to its wide range of abilities. To learn how to program in Python, it's helpful to first have an awareness of its history. Below you can see my scrollable diagram of the summarized history that led to the development and release of the Python programming language.

www.jacoblyman.com - History of Python.pdf

Since the original release of Python in 1990, the language has gathered a vast and helpful community of enthusiasts and professionals. Some popular landing pages for the community include python.org and us.pycon.org.

Ongoing innovations from the community can be found in the "Python Package Index" (pypi.org). This is where you can download modules/packages from (more on that later!) and publish your own for others to use. Through the approval process of "Python Enhancement Proposals" (peps.python.org), the community grows the functionality of Python's core tooling. This same process has even developed a quirky, yet helpful, approach to Python programming, called the "Zen of Python." 

Some points from the Zen of Python are:

Programming Basics

Programming languages like Python continue to evolve each day, but the underlying mechanics that they abstract away remain ever-green. Knowing some basics of those underlying mechanics can help you more easily layer new tools and languages on top of your understanding. Those tools and languages often times will rotate to newer and better ones throughout the years, but the foundation remains constant.


First off, let's cover some basics that are easy to overlook when starting to learn how to program. Ultimately, programming boils down to a bunch of ones and zeros, true or false, if and else. That's it! But it's a lot easier said than done.

Illustrated by postman.com

These ones and zeroes are referred to as "binary," which is essentially the language that computers speak and understand. To make it easier for us humans, we can speak to a computer through the use of higher-level programming languages, like Python, that translate our input into the binary that a computer can understand.

Having a rough idea of how Python translates to binary can be helpful context as you start learning the language. There's a lot that can be said about that, but just keep the following diagram in mind for now.

Illustrated by prepinsta.com

Python code is usually saved in .py files.  When you install Python, you'll have a program on your computer called  python. This program is called the "interpreter" and its job is to look at your Python code, compile it to bytecode and serialize that into machine readable code (e.g., binary) that your computer can understand and then execute. This moment is often referred to as "runtime," which is a word you'll sometimes hear throughout your learning.


There are several styles of programming out there. The most common are object-oriented programming, functional programming, and procedural programming. Each of these styles are available options when programming in Python, though the language is primarily known as an object-oriented programming language. Everything in Python is treated as "objects," which can contain data and/or code: data, in the form of fields, and code, in the form of procedures.

Python Fundamentals

You can install Python onto your computer following these instructions. As a note, there are two major versions of Python that are in use today: Python 2 (legacy version) and Python 3. The older Python 2 is no longer actively maintained, but is still in operation due to the use of legacy applications from that time period. Python 3 is the recommended version of Python and is the version that is actively maintained today. 

As you start learning Python, there are important building blocks to be aware of, namely functions, classes, modules, packages, data types, and data structures. These topics are closely inter-connected with one another on their use-cases and how they operate. 


Functions:

A fundamental building block used across programming languages is the "function." Functions are just like what you might've learned about in some of your Math classes. A function is a pre-defined set of steps that takes an input and transforms it into a desired output. Python comes pre-equipped with built-in functions that are always available to you for solving common problems found in programming. You can also find more functions from modules and packages (more on this in the next section!) or you can create your own.


Classes:

A similar, yet different, building bock of the language is the "class." Classes serve as the essence of the object-oriented programming style in Python. Everything is treated as an "object" (i.e.,  A function, integer, list, etc.), every object has a "type," and every object type is created through the use of classes. A helpful distinction between functions and classes is that functions do specific things while classes are specific things

An object that belongs to a class is called an "instance" of that class. For example, the list data structure (see more on data structures below) is a class in Python. When we create a list, we have an instance of the list class.

Classes often have what are called "methods" built into them, which are functions that allow us to do things with instances of the class. For example, if I am working with an instance of the list class, I can use the method list.append() to add more items to that list! 


Data Types & Structures:

We use the above building blocks to work with and manipulate different data types and structures. Data types are common across programming languages and are simply forms of data like text (e.g., strings), numeric values (e.g., integers, floats, and complex numbers), and boolean values (e.g., true or false). Along with these data types, data structures are templates of how we can organize data in useful ways. 

Python comes with powerful data structures that cover ~80% of what you'll need for programming. The other 20% of use-cases can be met through data structures found in other Python modules/packages, such as the Pandas "dataframe." Included data structures are as follows:


Modules & Packages:

The last important building blocks that I want to mention are "modules" and "packages," which will lead us into our next section!  A module is a collection of related functions stored in a .py file. You can source that file into your various Python projects so that you can re-use those functions, as needed. A package is very similar, but the difference is that a module is a singular .py file, while a package is a collection of modules. 

Python Modules & Packages

Python is a toy that comes with batteries included. Right from the start you'll have access to a set of helpful modules and packages that come from the standard library of Python.  In order to effectively learn the language, I recommend mastering this.

Python's Standard Library:

"Python’s standard library is very extensive, offering a wide range of facilities... The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs.

- The Python Standard Library

The standard library is quite large. Because of that, here are some module highlights that you can keep in mind:

Other Modules/Packages:

"The standard library isn't the only place you'll find excellent importable modules to use with your code. The Python community also supports a thriving collection of third-party modules... If you want a preview, check out the community-run repository: http://pypi.python.org" - Python Head First, 2nd edition

Adding new modules and packages to your Python toolkit should be secondary to learning the standard library. However, you'll inevitably want to use other tools available from the Python community. I've found that it helps to first narrow the focus of your learning to a small, manageable set of functions, modules, and packages. 


For example, here is a list of modules/packages that I like! Each of these can be installed from pypi.org (see the next section).

Using Python

You Need an IDE:


An Integrated Development Environment (IDE) is an important tool that you will need when learning Python. Out of the box, the language comes with IDLE, which is a small, yet usable, IDE. However, the IDE I recommend is either VS Code or Jupyter. If I am doing typical engineering work, I prefer VS Code. If I am doing data science work, I prefer using Jupyter. A powerful combination is the use of VS Code + Docker Dev Container + Jupyter Notebook together, but that explanation requires a separate article (coming soon!).

An example of using a Jupyter Notebook in VS Code

If you choose to use VS Code as your preferred IDE, these are some of the Python extensions I would recommend:

You Need Package Management:


To learn more about package management, I highly recommend watching the Pluralsight course Managing Python Packages and Virtual Environments by Reindert-Jan Ekker. You can sign up for a free Pluralsight trial here.


One of the most troublesome issues in Python development is "package management." It's been a headache for the community that has spurred the creation of a variety of helpful tools. To avoid this pesky issue, I recommend familiarizing yourself with both pip and poetry. Understanding how to use these tools will also help you further master the above modules/packages. A popular alternative worth noting is conda, which is a package manager focused on the data science community.


The Package Installer for Python, aka pip,  is the standard tool for installing Python packages and comes pre-installed with all major Python versions. 

You can easily install modules from pypi.org using the pip install command. You can also use a requirements.txt file to specify a list of module versions that you would like to bulk install together for a project using pip install requirements.txt command.

Poetry helps you declare, manage, and install the versions and dependencies of the Python modules/packages that you need for a project. Using such a tool helps ensure that the software needed for your project can be easily passed on to colleagues and other computers that you might need to work on. You can set up poetry to manage your Python project with poetry init and install necessary module versions and dependencies for a project using poetry install and/or poetry update.

Working in Virtual Environments:

Illustrated by dataquest.io

A "virtual environment" is an isolated container for using different Python versions and installing modules/packages. Using a virtual environment together with your package management tools is a powerful and important combination. Doing so helps ensure that your projects are reproducible and can stand independent from one another, avoiding dependency conflicts across your projects. My advice to you is to always work inside a virtual environment by using popular tools, like venv,or virtualenv. As a side note, I also highly recommend working in Docker Dev Containers with VS Code (future article coming soon), which is another approach to virtual environments.

Ways to Learn Python

There comes a point where you will need to put down the textbooks/courses and start building something with Python. Having a project to apply your learning towards is the best way to learning a new programming language. Find a way to use Python for your benefit in the form of a project, whether that might be a homework assignment, school project, your job, or a personal project that you're interested in.


Don't be afraid to approach your learning in a variety of ways. Other tools I would recommend are:

If you found any of my content helpful, please consider donating
using one of the following options   Anything is appreciated!