In this Python tutorial we want to learn about Python 3.9 New Features with Examples, as you know python 3.9 was introduced in 5th October 2020, so there has been some new improvements on python language, from two new method of merge and update on dictionary class, introduction of new string methods and zoneinfo library up to a number of new features have been added. also a new stable and high-performing parser has been introduced in python 3.9. in this lesson we want to introduce some New Features in Python 3.9 with examples.
Dictionary Merge & Update Operators
Merge ( | ) and update ( |= ) operators have been added to the built-in dict class. Those complement the existing dict.update and {**d1, **d2} methods of merging dictionaries, now let’s create a simple example.
In here I have two dictionaries and i want to merge and update these two dictionaries, now first let’s do this without Python 3.9 operators.
1 2 3 4 5 6 7 8 9 |
dict1 = {'name':'Parwiz', 'last_name':'Forogh'} dict2 = {'email':'par@gmail.com'} dict3 = {**dict1 , **dict2} print(dict3) dict1.update(dict2) print(dict1) |
If you run the code, you can see that the dictionaries are merged and updated.
Now let’s do this using Python 3.9 new operators of Merge (|) and update (|=).
1 2 3 4 5 6 7 8 9 10 11 12 |
dict1 = {'name':'Parwiz', 'last_name':'Forogh'} dict2 = {'email':'par@gmail.com'} #merge operator dict3 = dict1|dict2 print(dict3) #update operator dict1 |= dict2 print(dict1) |
And if you run the code we will receive the same result.
String Methods for Removing Prefix and Suffix
Two new string methods for removing prefixes and suffixes have been added.
1 2 |
print("Parwiz ".removeprefix("Pa")) print("Parwiz".removesuffix("iz")) |
If you run the code this is the result.
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.
Let’s first create an example before Python 3.9.
1 2 3 4 5 6 7 8 |
from typing import List def greet_all(names: List[str]): for name in names: print("Hello", name) greet_all(['World']) |
You can see in the above example we have imported List from typing, if we don’t do this we will receive error in our code.
So now let’s create our same example with Python 3.9.
1 2 3 4 5 6 |
def greet_all(names: list[str]): for name in names: print("Hello", name) greet_all(['Geekscoders']) |
You can see that in Python 3.9 we don’t need to import the List from typing.
Run the code and this is the result.
ZoneInfo Module
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. if you are using Mac or Linux you can use the zoneinfo directly, but if you are using window you to install a third party library that is called tzdata.
1 |
pip install tzdata |
And this is the code.
1 2 3 4 5 6 |
from zoneinfo import ZoneInfo from datetime import datetime dt = datetime(2020, 11, 18, 12, tzinfo=ZoneInfo("America/Los_Angeles")) print(dt) print(dt.tzinfo) print(dt.tzname()) |