What are the New Features in Python 3.9.0?

Introduction

On October 5, Python released a new version 3.9.0. In this article, we have given the information about the new release highlights, new features, new models, with some source to try it in your own environment.

Python is one of the preferred options for developing websites. Simplicity in use, versatility, and time saving are the main features that increase the demand of the Python framework for web development. Especially for machine learning, Python proves to be the highly preferred option. The simplicity, flexibility, consistency, and platform independence are some of the prominent factors that make Python suitable for machine learning projects.

1.  Dictionary Merge & Update Operators

Merge/union (|)  and update/in-place union  (|=) operators have been added to the built-in dict class. Those complement the existing dict.update and {**d1, **d2} methods of merging dictionaries.

Example

>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}

Merge

The | operator is used for calculating the union of sets

Example:

>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}

Update

The basic use of |= is to update a dictionary in place, similar to .update()

Example:

>>> x |= y
>>> x
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

2. New String Methods to Remove Prefixes and Suffixes

Example

>>> sentence = 'New String Methods to Remove Prefixes and Suffixes in python'
>>> sentence.removesuffix('python')
'New String Methods to Remove Prefixes and Suffixes in '
>>> sentence.removeprefix("New")
' String Methods to Remove Prefixes and Suffixes in python'

3. Type Hinting Generics in Standard Collections

In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing. Some other types in the standard library are also now generic, for example queue.Queue.

Example:

def greet_all(names: list[str]) -> None:
     for name in names:
          print("Hello", name)

4. New Modules

a. Zoneinfo

The zoneinfo module brings support for the IANA time zone database to the standard library. It adds zoneinfo.ZoneInfo, a concrete datetime.tzinfo implementation backed by the system’s time zone data

Example:

>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime, timedelta
>>> dt = datetime(2020, 11, 19, tzinfo=ZoneInfo("Asia/Kolkata"))
>>> print(dt)
2020-11-19 00:00:00+05:30
>>> dt.tzname()
'IST'

b. graphlib

A new module, graphlib, was added that contains the graphlib.TopologicalSorter class to offer functionality to perform topological sorting of graphs.

Example:

>>> from graphlib import TopologicalSorter
>>> graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}}
>>> ts = TopologicalSorter(graph)
>>> tuple(ts.static_order())
('A', 'C', 'B', 'D')

Mobio Solutions is a leading Python development company. With the ability to build custom applications Python becomes one of the most popular programming languages among various industries. At Mobio Solutions, our machine learning, data analysis & mining and Python development services deliver high-quality applications with enhanced features to increase the ROI of the business.