The world’s leading publication for data science, AI, and ML professionals.

6 New Awesome Features in Python 3.10

The new Python version is out with interesting new features.

Tips and Tricks

Photo by David Clode on Unsplash
Photo by David Clode on Unsplash

Python is one of the most popular Programming languages today. It is used in a wide variety of fields and applications, from learning the basics of computer science to performing complex and straightforward scientific computing tasks to building games. It is even used in advanced areas like data science and quantum computing.

Python gained popularity for many reasons. The two most important ones are how versatile Python is and how relatively easy it is to learn compared to other programming languages. In addition, Python is maintained and developed by the Python Software Foundation, which is always working on new ways to improve Python.

A week ago (Oct 4th, 2021), a new version of Python was released, Python 3.10. In this new version, unique and valuable features were added to Python, while some old features were removed. We can categorize the features added or removed to several categories in any new software release, such as syntax features, add to the default library, or improvements to an existing feature.

5 Python Books to Transfer Your Code to The Next Level

Python 3.10 has several new and cool features that make working with Python an even better experience. In this article, I will share with you 6 new features and add-ons that I am most excited about and glad to see added to Python.

№1: Better Error Tracking

As a person who uses Python every day to write code and teach coding, I am well aware of the frustration of getting a syntax error. Although syntax errors are easy to fix once you get ahold of Python and programming, sometimes we wish for better error messages that can help us locate the error better and save time on debugging.

In Python 3.10, dealing with errors is much better because of two features, better error messages and precise line numbers for debugging. For example, let’s consider the following code, where we have a dictionary and a function. However, in this code, we forgot to close the dictionary.

some_dict = {1: "jack", 2: "john", 3: "james" ,
a_results = a_useful_function()

In previous versions of Python, the error message for this would look something like this:

File "amazing_code.py", line 3
    a_results = a_useful_function()
              ^
SyntaxError: invalid syntax

But, with the new error messages and line numbering improvement, the new error message will have better information, like the exact type of error and its precise line number.

File "amazing_code.py", line 1
    expected = {1: "jack", 2: "john", 3: "james" ,
                                                 ^
SyntaxError: '{' was never closed

This new feature will help make debugging much faster and decrease the frustration for people starting to learn Python.

№2: Introducing Structural Pattern Matching

If you have used other programming languages like C++, you may have wished if Python had the switch statement, so you don’t have to go through the long if, elif, elif,…., else statement. Well, one of the new features of Python 3.10 is the addition of what they call the structural pattern matching, or in other words, the switch, case statement which has the following syntax.

match subject:
    case <patt1>:
        <act1>
    case <patt2>:
        <act2>
    case <patt3>:
        <act3>
    case _:
        <action_default>

6 Best Python IDEs and Text Editors for Data Science Applications

№3: New Type Union Operator

Although Python is a dynamically typed programming language, there are ways to make some portions of it statically typed. For example, if you’re writing a function and the type of attributes is significant for the commutations inside your function. In previous versions, you could specify the type of an attribute such as:

def func(num: int) -> int:
    return num + 5

But, if you wanted to accept two types, then you would need to use the Union keyword.

def func(num: Union[int, float]) -> Union[int, float]:
    return num + 5

In the new version of Python, you can choose between two types, using the | operator instead of Union for a more straightforward type decision.

def func(num: int | float) -> int | float:
    return num + 5

№4: Other Cool Features

4.1 Stricter Zipping

One of the fun Python functions in Python is the zip() function, which is a built-in function in Python that allows you to combine and iterate over elements from several sequences. In previous versions, you could’ve used zip with sequences of different lengths, but, now, a new parameter strict was introduced to check if all iterables passed to the zip function are of the same length.

9 Free Quality Resources to Learn and Expand Your Python Skills

4.2 Automatic Text Encoding

As a programmer, we come across or say the phrase "it works on my machine!". There are various reasons why a code will work on one machine but not the other; text encoding can cause such an error.

In previous versions of Python, if you don’t explicitly state an encoding type, the preferred local encoding may cause the code to fail on other machines. In Python 3.10, a warning can be activated to inform the user when/ if they open a text file without a specific encoding type.

4.3 Asynchronous Iteration

A powerful and advanced programming paradigm is Asynchronous programming, which has been a part of Python since version 3.5. In Python 3.10, there are two new asynchronous built-in functions [aiter()](https://docs.python.org/3.10/library/functions.html#aiter) and [anext(](https://docs.python.org/3.10/library/functions.html#anext)) to make your code more readable.

Final Thoughts

When I was doing my undergraduate degree, I took several classes where we used C++ or Java to write code and implement applications. But, when it came the time to work on my graduation thesis, I decided to learn and use Python. That was almost a decade ago, and I never looked back; Python has then become my programming language of choice whenever I tackle a problem.

Then, I started teaching computer science to kids, and I realized how Python could inspire the young generation to pursue a career in tech. Aside from the easiness of writing or reading code in Python and how fast you can start implementing code in Python, my favorite thing about this programming language is how hard the Python Software Foundation works to keep Python relevant.

The 5 Certificate to Prove Your Python Knowledge Level

With every new release of Python, incredible new features are added, features that most of the use has been asking for, features that will make it more efficient for us to write code in Python and make it easier for people to get into programming – in general – and Python – in particular. In this article, I shared with you the 6 new features in Python 3.10 that I am most excited about using with my students.


Related Articles