Python 3.9 is out! Explore 7 Exciting Python 3.9 Features That You Should Know
This article was published as a part of the Data Science Blogathon.
As famous author Wayne W.Dyer puts it,
Change the way you look at things and the things you look at change.
When the new version of Python came out, many were worried about backward compatibility issues and other things. But if you love Python, you are sure to be excited by the cool features that have been released in the new update.
The latest version of Python is out on Monday, October 5th, 2020. This article presents you with a list of Python 3.9 features that you can try right now.
Let’s begin by updating to the new version of python. If you’re unsure of the version you are currently using, then use the code below to check your current version.
In cmd, type
To update your version, go to the Python downloads page, get the installation and start installing. Note: Make sure to update the PATH in environment variables.
Now that we have the latest version, it’s time to check what’s new. Let’s dive in.
1. Dictionary updates
Dictionaries are one of the most useful and used data structures in Python. The new release has optimized the way of merging and updating dictionaries.
1.1 Merge dictionaries
Let’s say we have two dictionaries dict1 and dict2,
dict1 contains the name and model of a car whereas dict2 contains the engine and curb weight.
Now we would like to merge the two dictionaries as they contain information about the same car. In Python 3.8 and earlier versions, to merge two dictionaries we use either
Built-in Update method,
Or the expression **,
This can be inconvenient and cumbersome at times.
With Python 3.9.0, we have a sleek syntax update with the | union operator to merge two dicts,
This way is very clean, concise, and candid. It also improves the readability of code.
If two dictionaries have a common key, then the value in the second dictionary will be kept.
1.2 Update dictionary
In order to update an existing dictionary with a new key-value pair in Python 3.8 or earlier versions, we would
Use the Update method,
Or update with an iterable,
In 3.9, we now have the update operator |= which does the same job in a much simpler way.
Here, the |= works like the augmented assignment operator.
dict1 |= dict2 means dict1 = dict1 | dict2
2. Type Hinting
Under normal conditions, we don’t specify the datatype in Python. But there can be situations where we might need a variable to behave in a certain type. In such cases, Python’s flexibility can be annoying. Starting with Python 3.5, we could specify types but this update has made things way too easier.
Consider a function which calculates the diameter of the circle,
In this scenario, the type of value passed to the function really matters. Although there is no error in the code, passing a string concatenates the same value twice.
This issue is overcome in the latest version with the help of type hinting where we can specify the expected type as int,
3. String Methods
Two new features have been added to the str object. This feature can sometimes come in handy in the Exploratory Data Analysis process.
One function removes the prefix from a string
Other function removes the suffix from a string
4. Math functions
A modification to an existing math function has been made. In earlier versions, the function to calculate GCD accepts only two numbers. But now, it can be applied to any number of values.
A new function has been added to the math module to calculate the LCM. Like the GCD function, the LCM function also accepts any number of values.
The math.nextafter() function takes two arguments x and y. This Python 3.9 feature is a function that returns the next floating-point value after x going towards y. It takes into account the precision of the floating-point.
4.4 Unit in the Last Place
The next Python 3.9 feature is the function to find the units in the last place which is the spacing between floating-point numbers. Consider a number with 3 digits, 8.14. The nearest larger number to this number is 8.15. These two numbers differ by 1 Unit in the Last Place (ULP). The return value of Math.ulp function is equivalent to this example.
To know in detail about Units in the Last Place(ULP), check this.
5. Consistent Package Import Errors
This is more of a fix than a feature. When a relative import went past its top-level package, earlier versions of Python raised inconsistent import errors.
builtins.__import__() raise ValueError whereas
importlib.__import__() raise ImportError
__import__() now raises ImportError instead of ValueError which makes more sense.
6. Random Bytes Generation
A new method called randbytes() has been introduced in the random module to generate random bytes. Python already enables the generation of random bytes through 3 different functions
but they can’t generate pseudo-random patterns.
The random.Random.randbytes() function can generate random bytes in a controlled manner and can be reproduced from the seed. Although, it can be used only when security doesn’t matter.
7. Support for IANA timezone
A new module zoneinfo has been introduced and added to the standard library to support the IANA time zone.
Consider an example to convert from Indian Standard Time to Madrid’s current time. Before 3.9 we would do this by installing pytz through pip,
With the zoneinfo module, this is pretty candid. You can directly import the ZoneInfo class.
Apart from these, we now also have the new High-performance PEG-based parser, Graphlib module, AsyncIO and multiprocessing improvements, HTTP status codes and a bunch of redundant features has been removed. Learn more. Thank you for reading all the way down here. Let me know in the comment section if you have any concerns, feedback, or criticism. Have a good day!