This article was published as a part of the Data Science Blogathon
Our popular programming language, Python, just got a significant update. Python 3.9 was released in Oct 2020 with a pack of useful characteristics!
So, after practising the newest version for quite some time, I thought to write all the feature updates issued in Python 3.9 for everyone to learn.
I browsed through the Python 3.9 release docs and the associated debates. Based on my knowledge and personal experience applying Python 3.9, I desired to write a thorough guide to glimpse the characteristics and their particular operations.
Before I start, I have to tell, I am delighted to investigate version 3.9 of Python. Some of the features are positively excellent and are applied in my everyday applications.
Note: To catch along with me or try out the latest features, you should have Python 3.9 installed on your systems. Writing this article, Python 3.9.6 is the most latest version of Python available for me. We will go to the official python.org website and click the download button as shown in the image below to download the latest version depending upon your operating system.
1.Dictionary Functions
2.Annotated Type Hinting
3.String Methods
4.IANA timezone: ZoneInfo
. . .
Python makes it simple to handle standard data types. Python 3.9 stretches this facility with new features for dictionaries. They now have union operators for dictionaries, one to join two dictionaries into a different dictionary and one to update the elements of one dictionary with another dictionary.
The previous version (3.8) method of joining two dictionaries have several faults:
x = {1: 'A', 2: 'B', 3:'C'} y = {4: 'D', 5: 'E'} {**x, **y}
[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
x.update(y) print(x)
[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
PEP 584 has included two brand-new operators for dictionaries:
x = {1: 'A', 2: 'B', 3:'C'} y = {4: 'D', 5: 'E'} z = x | y print(z)
[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
x = {1: 'A', 2: 'B', 3:'C'} y = {4: 'D', 5: 'E'} x |= y print(x)
[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
Note: If our dictionaries have a key common to both the dictionaries in Python 3.9 version, the key-value pair in the second dictionary will be employed in the final result:
x = {1: 'A', 2: 'B', 3: 'C', 6: 'H'} y = {4: 'D', 5: 'E', 6: 'J'} print(x | y)
[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 6: ‘J’, 4: ‘D’, 5: ‘E’}
Python is dynamically written, indicating we don’t demand to define data types in our code. This is good, but often it can be complicated, and abruptly Python’s versatility displays more of a problem than anything else.
Over the previous versions, Python has extended relief for type hinting. Type hinting is an effective tool to guarantee consistency in huge codebases, so Python code can profit from producing type hints.
The extension to Python’s typing mechanisms is adaptable function and variable annotations. This provides the annotated type to specify a style utilizing metadata that can be reviewed ahead of time (with linting devices) or runtime.
Since version 3.5, we could define types, but it was rather cumbersome. This update has surely developed that. Let’s understand with an illustration:
def addition(n): return n+n addition('A')
We clearly desire to add the corresponding number to itself. Still, our editor doesn’t understand that. It is absolutely okay to add two strings commonly using the + operator, so no warning is delivered in previous versions.
def addition(n: int): return n+n addition('A')
[Output]: Expected type ‘int’, got ‘str’ instead
Proposing the generics to the conventional collections addresses it more comfortable to annotate the programs. It is not necessary anymore for a parallel type hierarchy in the writing module.
This enables for practicing the equivalent type of syntax in all circumstances.
We regularly demand to manipulate strings in a straightforward, readable fashion that gets the work done but doesn’t contaminate the code. PEP 616 has added modish methods for strings.
These are innovative approaches to removing prefixes and suffixes, which have long demanded a lot of manual effort to pull off. The new methods are as follows:
There have been numerous recurring concerns listed over all the influential forums like StackOverflow or GitHub encompassing the lstrip() and rstrip() methods of earlier versions of Python.
"Python 3.9 is the best ".strip("Python")
[Output]: “3.9 is the best”
"Python 3.9 is the best ".removeprefix("Python")
[Output]: “3.9 is the best”
"Python 3.9 is the best".removesuffix("best")
[Output]: “Python 3.9 is the”
Dates and time present a fundamental role in multiple applications. Python extends extensive support via the datetime module in the official library.
But there has forever been a hole concerning integrating time zones to certain timestamps.
Up till now, we’ve used third-party libraries like dateutil to implement the before-mentioned timezone-specific rules.
But now, Python 3.9 has introduced a new module zoneinfo to promote the IANA (Internet Assigned Numbers Authority) time zone database. IANA database is a well-maintained and extensively utilized database.
This assistance for the IANA time zone database has been integrated into the official library. A straightforward technique to apply in Python’s datetime library will be a great time-saver for many developers.
from datetime import datetime, timezone datetime.now(tz=timezone.utc)
[Output]: datetime.datetime(2021, 07, 15, 18, 25, 516713, tzinfo=datetime.timezone.utc)
from zoneinfo import ZoneInfo ZoneInfo("Asia/Kolkata")
[Output]: zoneinfo.ZoneInfo(key=’Asia/Kolkate’)
import zoneinfo import datetime my_zone = zoneinfo.ZoneInfo("America/Vancouver ") print(datetime.datetime.now(tz=my_zone))
[Output]: The above-shown code generates an object with zoneinfo data based on the Vancouver timezone and then outputs a datetime object based on that zone.
This is the usual way it operates: the zoneinfo module will manage the system’s time zone by default. If there is no system timezone free, it will then utilize tzdata as the timezone. You may require to pip install tzdata ahead of getting the possible timezone servers.
3.9.0 records a significant milestone in the journey of Python’s development and for society. New improvements are being united as we speak, and 3.10 will also have immeasurable features.
For now, you should investigate these soon-to-be extensively accepted features added in Python 3.9.
Try implementing your existing applications employing Python 3.9 and observe if upgrading to the latest version will be worth it for you or not.
That’s not the only feature for Python 3.9. There is a lot more to study and examine. If you desire to learn more about certain changes, please browse through the official guide here.
If you hold any questions or recommendations, please feel free to reach out to me via LinkedIn or in the comments section below.
Thanks for reading my article!