Learn everything about Analytics

Home » Top 4 Features To Learn About Python 3.9

Top 4 Features To Learn About Python 3.9

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.

Outline Of The Article

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.

features python 3.9
Image By Author

We’ll check out the following features today:

1.Dictionary Functions
2.Annotated Type Hinting
3.String Methods
4.IANA timezone: ZoneInfo

.  .  .

1.Dictionary Functions

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:

Python 3.8

  • Dict unpacking looks messy and is not clearly discoverable.
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’}

  • Another method is dict.update which updates the actual dictionary in place:
x.update(y)
print(x)

[Output]: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}

Python 3.9

PEP 584 has included two brand-new operators for dictionaries:

  • (|) union — to join two dictionaries. It maintains the real 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’}

  • (|=) update — merge two dictionaries in place.
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’}

2.Annotated Type Hinting

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:

Python 3.8

  • No support for type hinting in previous versions of Python.
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.

Python 3.9

  • What we can now do is define the required input type as int. Practicing this, our editor picks up on the query promptly.
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.

3.String Methods

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:

  • removeprefix()
  • removesuffix()

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.8

  • We’ve been stripping off letters from each end of the string applying the strip() method as follows:
"Python 3.9 is the best ".strip("Python")

[Output]: “3.9 is the best”

Python 3.9

  • Here’s an illustration for the prefixremoval in Python 3.9:
"Python 3.9 is the best ".removeprefix("Python")

[Output]: “3.9 is the best”

  • Here’s an illustration for the suffixremoval in Python 3.9:
"Python 3.9 is the best".removesuffix("best")

[Output]: “Python 3.9 is the”

4.IANA timezone: ZoneInfo

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.

Python 3.8

  • Till now, we’ve been accessing time zone aware timestamps applying the tzinfo argument as below:
from datetime import datetime, timezone
datetime.now(tz=timezone.utc)

[Output]: datetime.datetime(2021, 07, 15, 18, 25, 516713, tzinfo=datetime.timezone.utc)

Python 3.9

  • But with the extension of zoneinfo, we now hold access to the IANA Time Zone Database.
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.

Conclusion

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!

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.
You can also read this article on our Mobile APP Get it on Google Play