npressfetimg-3079.png

Python 3.10: 6 New Awesome Features – Built In

Python

Python is the most popular programming language today for many reasons, chief among them are Python’s versatility and how relatively easy it is to learn. The Python Software Foundation (PSF) maintains Python, develops the language and is always working on new ways to improve it.

On October 4, 2021, the PSF released Python 3.10. In this new version, the PSF added unique and valuable features, while also removing some old features. Here are the six newest features and add-ons I’m most excited about in Python 3.10.

Top New Features for Python 3.10

  1. Better error tracking
  2. Structural pattern matching
  3. New type union operator
  4. Stricter zipping
  5. Automatic text encoding
  6. Asynchronous iteration

 

1. Better Error Tracking

As a person who uses Python every day to write code and teach coding, I’m well aware of how frustrating it is to get a syntax error. Although syntax errors are easy to fix once you have a grasp on Python and programming, sometimes we need error messages that help us locate the error more efficiently and save time on debugging.

In Python 3.10, this is all much easier with 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

With the new error messages and line numbering improvement, the error message will have more complete 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 make debugging much faster and decrease frustration for people just starting to learn Python.

More From Sara A. Metwalli5 Ways to Write More Pythonic Code

 

2. Structural Pattern Matching

If you’ve used other programming languages like C++, you may have wished 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 structural pattern matching otherwise known as the switch, case statement which has the following syntax:

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

 

3. New Type Union Operator

Although Python is a dynamic programming language, there are ways to make some portions of it static (e.g. You’re writing a function and the attribute type 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

Learn New Skills on Built InHow to Write Pseudocode

 

4. Stricter Zipping

One of my favorite Python functions is zip(), a built-in function 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 Python 3.10 introduced a new parameter, strict was, to check if all iterables passed to the zip function are of the same length.

Looking for More Python Tutorials?4 Python Tools to Simplify Your Life

 

5. Automatic Text Encoding

As a programmer, we often hear  “it works on my machine!” when we’re troubleshooting errors. There are many reasons, like text encoding, why a code will work on one machine but not the other.

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 activates to inform the user when (or if) they open a text file without a specific encoding type.

 

6. Asynchronous Iteration

Asynchronous programming is a powerful and advanced programming paradigm that’s been a part of Python since version 3.5. In Python 3.10, there are two new asynchronous built-in functions, aiter() and anext(), to make your code even more readable.

Git Your Versions Under Control5 Git Commands That Don’t Get Enough Hype

When I was doing my undergraduate degree, I took several classes where we used C++ or Java to write code and implement applications. When it came the time to work on my senior thesis, I decided to learn and use Python. That was almost a decade ago, and I never looked back; Python has 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 younger generations to pursue a career in tech. Aside from the ease of writing or reading Python and how fast you can start implementing the language, my favorite thing about Python is how hard the Python Software Foundation works to keep it relevant.

With every new release of Python, the PSF adds incredible new features that make the language more efficient to write in and more accessible to learn.

This article was originally published on Towards Data Science. 

Source: https://builtin.com/software-engineering-perspectives/python-3.10

RSS Feeds

Leave a Reply

Your email address will not be published.

Related Posts