So you’ve decided to get to grips with one of the most popular coding languages, but which version to opt for? Here’s why Python 3.8 is the one for web developers.
First released in 2019, Python 3.8 has brought the language on leaps and bounds, particularly for web development. So, what can you expect from this release? Don’t worry—we’ve got you covered. Before we jump into the features and advantages of what 3.8 has to offer, first we’ll look at the language itself and how it’s used in the field.
If there’s a particular section that you’d like to skip straight to, just use the clickable menu:
- What is Python?
- How is Python used for web development?
- Python 3.8 features
- Advantages of Python 3.8
- How to learn Python 3.8
- Next steps
1. What is Python?
For anyone today diving into development for the first time, there’s a high chance that someone recommends that you start with Python—and with good reason! The language has taken a spot as one of the most widely used scripting languages today, primarily owing to the easy learning curve that comes with it. But that’s not all—Python’s ease of use is surprisingly paired with its versatility in the applications you’d find it in—machine learning, data analytics, image processing, scientific computing, web application frameworks, and even gaming!
Keeping that aside, the motive behind Python’s design is clear—to help programmers focus more on the core logic for their applications, rather than the setup that goes with it. This helps bring ideas into motion with minimal distractions along the way. All of this really pays off, with Python developer salaries around the world eye-catchingly competitive, whatever way you apply it.
2. How is Python used for web development?
Python frameworks are one of the most popular ways the language is used by web developers today. The use of web application frameworks cuts your development time to a fraction of what it used to be. They take care of all the heavy lifting involved with database access, templates, user session management, and more. What you’re left with is a simple, beautifully abstracted set of tools at your disposal that have been tried and tested. Python supports a number of popular frameworks available today—Django, Flask, web2py, Bottle, TurboGears, and Pyramid are just a few of them.
Of course, there are other libraries available across languages such as PHP, but what’s with the enthusiasm behind Python frameworks? It’s the ease with which your application logic can be integrated into these prebuilt frameworks. They’ve been designed for standardized, modularized development, where everything is organized and easily maintainable, making it effortless for anybody to jump in and continue development on someone else’s application. This means that collaboration isn’t a herculean task anymore.
Frameworks are just one of the many reasons why you should learn Python for web development. Overall, the language’s simplicity and flexibility allows for easier integration of business logic into your web apps, without having to grind yourself through figuring out complicated syntax before actually making things work.
Python also keeps things simple, where there’s no need to worry about any of the low-level details such as thread management, sockets, and HTTP protocols. It comes armed with built-in as well as publicly available packages that can easily be integrated into your project. Basically, this avoids reinventing the wheel—Python-based web application frameworks ensure that the bare essentials are already taken care of.
3. Python 3.8 features
Several iterations of the language have come through, with one of the major transitions being the move from Python 2 to 3. In fact, this was so big of a change that Python 2 code isn’t completely supported on Python 3 installations.
However, until Python 3.8, most of these changes have been under the guidance of Python’s creator, Guido van Rossum. This release saw the establishment of a steering council consisting of five core developers, where standardized processes are laid out to incorporate newer features into future releases of Python. Simply put, instead of making large-scale changes in a single shot, these changes are split up into smaller ones, each of which would be reviewed independently before they make it to the final release.
Most of these changes are also community-driven, where members suggest adding in new features to make development more convenient and feature-rich. The addition of these features is carried out in such a way that older Python 3 code can still run on newer 3.x releases without any significant change in results.
Here are some of the major changes that we’ve seen in Python 3.8’s release:
The all-new Walrus operator
Python 3.8 introduced the use of a new kind of an assignment operator (:=), so-called because it resembles a sideways walrus characterized by its eyes and tusks. This operator finds its use for temporary assignment of values inside expressions, in places like if-else statements or while loops. Here’s an example of how the walrus operator can make your code shorter and more readable:
Without the walrus operator:
With the walrus operator:
Positional-only arguments
This is one of those features that gives developers the option of rigidity amidst the flexible environment that Python provides. Although there isn’t much of an advantage in terms of convenience during development, it provides an additional sense of disciplined design while writing out functions or class methods. As you may already know, Python functions can take in values as either positional or keyword arguments. For instance, let’s look at a function that performs division between two numbers:
From the code above, it’s clear that keyword arguments provide flexibility in terms of positioning them while making function calls. However, these calls can break if keyword names are altered in the function definition, leading to errors in your application.
In order to avoid this, positional-only arguments enforce the use of positional arguments while calling functions. The following Python 3.8 code is an example of the same function with this feature:
From this code snippet we can first explain how to incorporate positional-only arguments into your function. Simply place a forward slash (/) as one of the parameters, in such a way that parameters before it would be treated as positional-only. In the example above, a, b, and c would be treated as such.
The second function call runs into an error, since we’re attempting to pass b and c as keyword arguments.
F-strings for easier debugging
For the uninitiated, the use of f-strings is a neat alternative to typing out repetitive print statements peppered across your code. Let’s assume that you’ve got some track info for some music:
Printing it out with f-strings is as easy as:
This gives an output like:
In Python 3.8 however, there’s an added feature that’s particularly useful during debugging, which allows for printing out the variable name right before its value. For example:
Usually we’d print its value like below, but it involves typing in “base64” twice:
You could instead use an f-string, with an equals sign added right after the variable name:
Which gives you the same result:
Syntax Warnings
Python 3.8 now comes strapped with a few useful warnings for any errors in your syntax. Instead of giving out irrelevant errors, it now has an idea of what you intended to do and then provides some useful suggestions on it.
As of now, it supports only a few SyntaxWarnings. Here are two of them:
If you’ve missed a comma while typing out a sequence of tuples.
Given a sequence like:
It’s clear that we’ve missed out on separating (1, 2) and (3, 4) with a comma. Earlier versions of Python would recognize that as a function call for (1, 2) with the parameters (3, 4), and would result in a TypeError, saying that (1, 2) isn’t a callable function.
Python 3.8, however, recognizes that you’ve intended to type in two tuples in a sequence, and instead gives you a helpful suggestion:
Mixing up between “is not” and “!=”
In Python, “a == b” is used to check if the values between aand b are the same. “a is b” however, is an identity check, where it checks if a and b are variables pointing to the same object. Pretty similar, eh? It’s a common mistake that’s a bit hard to catch when reviewing your code.
The same goes for the inverse logic as well, as “a != b” and “a is not b” aren’t the same either. To further demonstrate this, here’s a quick example where we’ll initialize two variables with the same list of values:
This is where Python 3.8 would give you a quick tip to verify your code for this mismatch, as shown in the following image:
The importlib.metadata module
This module is pretty useful during development, where you’d be able to grab more info on the libraries and modules that are in use. This module is built into Python 3.8’s installation, which means that you wouldn’t have to get it installed separately via pip. Pip is a package management system that gives you access to a diverse repository of packages that suit almost any application that you could think of. Some of the useful methods available in this module are:
requires
This method is useful to find out dependencies for any package that you may have installed. For instance, let’s say you’ve got the Django framework installed in your virtual environment—its package name shows up as “Django” from the pip list command. Here’s how we’ll find out the packages that it relies on:
As you can see in the code, this method churns out a list containing package names of all of Django’s direct dependencies.
version
A relatively simple method, metadata.version() is used to get the version number of any installed package. Taking Django as our example again, we get a simple string output containing the version number:
files
metadata.files() can be used to list out the paths of all the files contained under an installed package:
Note: In this example, the output above has been truncated down to a few lines. The actual number of file paths displayed for Django’s installation would be approximately 4000 in number.
read_lines
Using any one of the file paths above, you can even retrieve the source code contained in any of those files with the help of the metadata.read_lines() method available in PackagePath objects; let’s try to read out the contents of Django-3.1.5.dist-info/AUTHORS:
New methods under math and statistics
The math and statistics libraries have also been upgraded with new methods in Python 3.8. Let’s take a look at some of them.
math.prod
Similar to sum() that returns a sum of the values passed into it, math.prod() returns the product of all values contained in a sequence:
math.isqrt
This is a slightly modified reappearance of math.sqrt(), where it returns the integer part of the square root of the number passed into it. Let’s quickly illustrate the difference between the two:
math.dist
This is used to find the geometric distance between two n-dimensional points. Let’s take an example of two points on a flat plane, with the coordinates (1, 2) and (4, 5):
math.dist will return the length of the red line that joins the two points:
This method can also take in points with higher dimensions, like (10, 40, 90) or even (50, 24, -33, 67, 85).
math.hypot
Similar to math.dist(), this method gives the magnitude of a vector. For those unfamiliar with vectors, it simply gives you the distance between a given point and the origin (where all coordinates are zero). For instance, let’s take a vector shown in this diagram:
math.hypot(3, 4) would give you the distance between the origin (0, 0) and (3, 4), indicated by the red line in the graph.
The statistics library also hosts new methods like fmean(), geometric_mean(), multimode() and quantiles(), as well as a new class called NormalDist, but we won’t go into them in detail in this article. However, if you’d like to dig deeper into these, you can have a look at Python’s official documentation.
General performance optimizations
Apart from the usable features above, Python 3.8 also includes performance improvements. These include faster code execution, less memory usage, faster performance during pickling, and many more. More info on this can also be found on Python’s official website, as well as a comprehensive list of enhancements and features.
4. Advantages of Python 3.8
We’ve just gone through why it’s worth upgrading to Python 3.8 from older installations, but what if you’re considering learning Python from scratch? We’ll go through some of the major reasons why Python developers settled on this language in the first place:
Readability
One of the most striking features of Python is its readability. Anyone with basic experience in coding can quickly grasp the underlying flow beneath a snippet of code. The following is an example:
From a quick glance, you’re able to figure out that this code first takes a letter from the user, and then executes a simple if-else condition check on whether the entered character is part of the alphabet or not. Based on this, we’ll show the user relevant outputs using print() statements.
Python also makes use of indentation to separate blocks of code, which compels the developer to type out clean, readable code.
Dynamically typed language
Languages like C++ and Java are statically typed, which means that variables once bound to a specific data type during declaration (like strings, numbers etc.) cannot be reassigned to another datatype later. However, Python is dynamically typed, which means that there’s no need to specify what data type it is to be bound to from the start. A variable can first be linked to a number, and it can later be bound to a string if required, within the same script.
Python allows you to reassign a variable with different kinds of data without raising any errors:
Simple built-in package management
Here’s the feature that makes Python so versatile in where it can be used. A plethora of open-source and proprietary packages can be found to suit your needs, where you don’t have to reinvent the wheel by coding out complex operations`. These packages are tried and tested, and can be used reliably in your applications.
For instance, let’s write a program to calculate the factorial of a number.
Note: For those who aren’t familiar with factorials, the factorial of a number n is the product of all numbers between 1 and n, where n is a positive integer. For example, the factorial of 3 (denoted by 3!) is calculated as:
3! = 3 x 2 x 1 = 6
On the flip side, using the built-in math module in Python, you can calculate factorials over a single line of code by simply importing it and using its built-in factorial() method:
Looking at this example, it’s a no-brainer to make use of packages available in Python to keep your code simple and short.
As we’d mentioned earlier, Python also allows installations of third-party packages via pip. For example, if you’d like to install the Flask framework on your machine, simply open up your terminal and type:
With this simple command, pip gets the ball rolling with getting Flask installed in your Python environment.
Strong community support
Finally, it’s the rich community that surrounds Python that makes it easy to dip your toes into this language. You’ll find yourself within one of the largest and most cooperative communities out there, and no doubts go unanswered. Python also supports community feedback to incorporate new features into its future releases, supported by Python Enhancement Proposals, also known as PEPs.
5. How to learn Python 3.8
With all the reasons laid out above to move into developing on Python 3.8, who wouldn’t want to jump right into it? For both beginners and developers coming in from other languages, there are a number of courses available online to learn Python either from its basics, or with respect to specific domains like machine learning, data analysis, web application development, and much more. Python’s official documentation is also a tremendously useful resource to keep handy, although some amount of basic knowledge may be required to browse through it.
However, for those who prefer a hassle-free, guided approach, you can have a look at CareerFoundry’s Python for Web Development Course and various specializations to get started.
6. Next steps
Python was initially built and designed with ease of use and versatility in mind, and future releases, including version 3.8, add a host of new features and enhancements to bring a better flow to the development process, as well as to ensure efficiency during runtime.
We recommend that you get started with trying these new features out on your own machine to get familiarized with the latest that Python has to offer. If you’re still considering making a move to Python for the first time, why don’t you take a look at some of these articles to get a better idea of what it has to offer?