7 features of Python3.9


** Pantron AI Sharing**

Author | PADHMA

Compile | VK

Source | Analytics Vidhya

Introduction

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.

Update Python

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.

1. Dictionary update

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.

1.1 Consolidated dictionary

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.

1.2 Update dictionary

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

**2 Type hint **

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)

3 String method

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

4 Mathematical functions

4.1 GCD

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.

4.2 LCM

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.

4.3 Nextafter

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.

4.4 ulp

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

5 Consistent package import error

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.

6 Random byte generation

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.

7 Support IANA time zone

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 conclusion

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

7 features of Python3.9
Some new features of Python 3.10
Secrets of the new features of Python 3.8
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Subscripts of tuples in Python
Python analysis of wav files
Analysis of JS of Python crawler
python king of glory wallpaper
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Python version of OpenCV installation
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules
The usage of wheel in python
Summary of logarithm method in Python
Detailed explanation of python backtracking template
Analysis of usage examples of Python yield
Use of Pandas in Python development
Detailed explanation of python sequence types
Implementation of reverse traversal of python list
Magic methods and uses of Python
In-depth understanding of Python variable scope
Python preliminary implementation of word2vec operation
Python handles the 4 wheels of Chinese
Python calculation of information entropy example
Implementation of python selenium operation cookie
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Scrapy simulation login of Python crawler
Analysis of Python conditional control statements
Black hat programming application of Python1
Detailed usage of dictionary in Python
Usage of os package in python
Several common modes of Python functions
A summary of 200 Python standard libraries!
Mongodb and python interaction of python crawler
Implementation of python3 registration global hotkey
Implementation of python student management system
List of new features of Zabbix 5.0 LTS
There are several versions of python
Simple usage of python definition class
Summary of Python calling private attributes
Some examples of python operation redis
​Full analysis of Python module knowledge
Example operation of python access Alipay
The premise of Python string pooling
Implementation of python gradient descent algorithm
Learning path of python crawler development