A new version of Python is coming!. Python has released a beta version of Python 3.9 (3.9.0b3) and will release the full version soon.
The new version introduces some exciting features and new modules. So, let's dive and learn about them.
NEW FEATURES
Dictionary merge and update operators
Python 3.9 introduces merge ( | ) and update ( |= ) operators in the dict class. So, if you have two dictionaries let's say x and y, you can use these operators to merge and update the two dictionaries.
x = {1: "hello", 2: "everyone"}
y = {3: "welcome"}
Use | to merge the both dictionaries.
z = x|y
print(z)
#Output : {1: "hello", 2: "everyone", 3: "welcome"}
If both the dictionaries have a common key, then the output will display the second key-value pair.
For updating the dictionary use |= operator.
x = {1: "hello", 2: "everyone"}
y = {2: "welcome"}
x |= y
print(x)
#Output : {1: "hello", 2: "welcome"}
removeprefix() and removesuffix() string methods
In Python's str class, the new update introduces new removeprefix() and removesuffix() method.
We can use removeprefix() method to remove the prefix from any string.
'PythonSpace'.removeprefix('Python')
#Output : 'Space'
And we can use removesuffix() method to remove the prefix from any string.
'PythonSpace'.removesuffix('Space')
#Output : 'Python'
New Parser
Python 3.9 uses a new parser which is PEG-based parser. Previously, Python used LL(1).
PEG is more flexible than LL(1) when it comes to building new features in the language.
Type Hinting
Python dynamically specifies datatypes to a variable. For static allocation of data types, type hinting is used. This was introduced in Python 3.5.
You can now use built-in collection types (list and dict) as generic types. Earlier, you had to import the capital types (List or Dict) from typing.
def hello_all(names: list[str]) -> None:
for name in names:
print("Welcome", name)
New Libraries
zoneinfo
The new Python update brings a new module, zoneinfo. It will bring support for the IANA time zone database to the standard library.
from zoneinfo import ZoneInfo
from datetime import datetime
dt = datetime(21, 6, 2020,
tzinfo = ZoneInfo("India/Delhi"))
print(dt)
#OUTPUT : 21-6-2020 12:00:00-07:00
graphlib
You can add graphlib module to your code if you are looking to perform topological sorting of graphs.
REFERENCES
Python 3.9
Graphlib module