** Pantron AI Sharing**
Author | PADHMA
Compile | VK
Source | Analytics Vidhya
As the famous writer Wayne W. Dell said,
❝Change the way you look at things what you look at will also change
❞
When new versions of Python came out, many people worried about backward compatibility and other issues. But if you like Python, you will be excited about the features released in the new update.
The latest version of Python will be released on October 5, 2020 (Monday). This article provides you with a list of python3.9 features, which you can try now.
Let us first update to the new version of python. If you are not sure which version you are currently using, please use the code below to check the current version.
In cmd, type
![]( http://qiniu.aihubs.net/31497cmd version.jpg)
To update your version, go to the Python download page, get the installation package and start the installation. Note: Make sure to update the path in the environment variable.
Now that we have the latest version, it's time to check what's new.
Dictionaries are one of the most useful and commonly used data structures in Python. The new version optimizes the way of merging and updating dictionaries.
Suppose we have two dictionaries dict1 and dict2,
dict1 contains the name and model of the car, while dict2 contains the engine and weight.
Now we want to merge these two dictionaries because they contain information about the same car. In python3.8 and earlier versions, to merge two dictionaries, we can use
Built-in update method:
Or expression**:
This sometimes causes inconvenience and trouble.
In Python 3.9.0, we use the | union operator to improve the syntax to merge two dicts,
This approach is very clean, concise, and frank. It also improves the readability of the code.
If two dictionaries have a common key, then the value in the second dictionary will be retained.
In order to update the existing dictionary with new key-value pairs in Python3.8 or earlier, we can
Use the update method,
Or update with iterable,
In 3.9, we now have the update operator |= which accomplishes the same job in a simpler way.
Here, |= works like the extended assignment operator.
❝dict1 |=dict2 means dict1=dict1 | dict2
❞
Under normal circumstances, we do not specify data types in Python. But in some cases, we may need a variable to represent a certain type. In this case, Python's flexibility can be annoying. Starting from Python 3.5, we can specify the type, but this update makes things easier.
![]( http://qiniu.aihubs.net/68514type hint1.png)
In this case, the type of value passed to the function is very important. Although there is no error in the code, passing a string will repeat the same string twice.
In the latest version, we can specify the desired type as int through type hinting,
![]( http://qiniu.aihubs.net/36104type hint2.png)
Two new features have been added to the str object. This feature can sometimes be useful during exploratory data analysis.
Remove prefix from function
Remove suffix from string
Modified existing mathematical functions. In earlier versions, the function to calculate GCD only accepted 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 LCM. Like the GCD function, the LCM function accepts any number of values.
This math.nextafter() function accepts two parameters, x and y. This feature of python3.9 is a function. Considering the precision of floating point numbers, it is the next floating point number from x to y.
Suppose we don't have a 64-bit computer. Instead, we only have 3 digits. With these three numbers we can represent numbers like 3.14, but not 3.141. For 3.14, the closest larger number we can represent is 3.15, and the difference between these two numbers is 1 ULP** (unit of the last digit)**, which is 0.1. The return value is equivalent to this example, but the same as the actual accuracy of your computer.
To learn more about ULP, please check: https://matthew-brett.github.io/teaching/floating_error.html
This is not so much a feature as it is a fix. When the previous Python import version is inconsistent, its earlier import version has an inconsistent error.
builtins.__import__()Raise ValueError
importlib.__import__()Raise ImportError
__ import__()
now raises ImportError instead of ValueError, which makes more sense.
A new method called randbytes is introduced in the random module to generate random bytes. Python can already generate random bytes through 3 different functions
But they cannot produce pseudo-random patterns.
The random.random.randbytes function can generate random bytes in a controlled manner, and can copy the result by setting a seed. However, it can only be used when safety is not important.
A new module supporting IANA time zone is introduced in the time zone library zoneinfo.
Consider an example of converting Indian Standard Time to the current time in Delhi. Before 3.9, we will install pytz via pip,
For the zoneinfo module, this is very straightforward. You can directly import the ZoneInfo class.
In addition, we now have a new high-performance PEG-based parser, Graphlib module, asynchronous and multiprocessing improvements, HTTP status codes, and a bunch of redundant features have been removed. Click here to learn more: https://docs.python.org/3.9/whatsnew/3.9.html
Original link: https://www.analyticsvidhya.com/blog/2020/10/7-exciting-python-3-9-feature-to-know/
Recommended Posts