Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features of Python 3.9

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what are the new features of Python 3.9". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of Python 3.9.

Dictionary merging

A very elegant feature, when we want to merge two dictionaries, we only need to use the operator "|":

A = {1:'a', 2: 'baked, 3:' c'}

B = {4: 'dwells, 5:' e'}

C = a | b

Print (c)

Output result:

[Out]: {1: 'asides, 2:' baked, 3: 'crested, 4:' dumped, 5:'e'}

Not only that, we can also update the original dictionary directly using the merge update operator "| =":

A = {1:'a', 2: 'baked, 3:' c'}

B = {4: 'dwells, 5:' e'}

A | = b

Print (a)

Output result:

[Out]: {1: 'asides, 2:' baked, 3: 'crested, 4:' dumped, 5:'e'}

It should be noted that if both dictionaries contain the same Key, the result of the operation will directly adopt the key-value pair of the second dictionary:

A = {1: 'ajar, 2:' baked, 3: 'crested, 6:' in both'}

B = {4: 'daddy, 5:' eBay, 6: 'but different'}

Print (a | b)

Output result:

[Out]: {1: 'asides, 2:' baked, 3: 'but different', 6:' but different', 4: 'dudes, 5:' e'}

Dictionary update of iterable objects

The "| =" operator also has another great feature, which is to update the dictionary with the key-value pair of an iterable object:

A = {'averse:' one', 'baked:' two'}

B = (I, iTunes 2) for i in range (3))

A | = b

Print (a)

Output result:

[Out]: {'one',' baked: 'two', 0: 0, 1: 1, 2: 4}

It is important to note that if you use the standard merge operator "|" instead of the "| =" operation server above, this will directly lead to TypeError.

TypeError:unsupported opprand type (s) for |: 'dict' and' generator'

Type hint

Python is a dynamically typed programming language, which means that we do not need to specify a data type for a variable in use. However, while this can be done, it often makes us confused when maintaining the code, and flexibility becomes a disadvantage.

After version 3.5, although we can specify the data type, it is still very troublesome to use. This is updated in this new version: there is no type prompt vs has a type prompt

In the figure above, we want to add two identical numbers together through the add_int function, but the compiler does not understand our intention very well, because two strings can also be concatenated with +, so there is no warning here.

Now when we can specify the parameter type of the function, the compiler can immediately recognize the above problem when the parameter type is int.

New string function

Although the string function is not as powerful as other new features, string is the most frequently used data type in development, and its changes need to be mentioned here. Two string functions to remove prefixes and suffixes have been added in the new version:

"Hello world" .removeprefix ("He")

Hello world ".removesuffix (" ld ")

Output result:

[Out]: "llo world"

[Out]: "Hello wor"

New parser

As an ordinary developer, the change in the syntax parser may not be detected, but it can be an important turning point in the evolution of Python.

As far as we know, Python currently mainly uses a syntax based on LL (1), which can be parsed through the LL (1) parser, which parses the code from top to bottom and from left to right, and only needs to pull a token from the lexical analyzer to parse correctly.

However, there are some problems in LL (1):

Python contains non-LL (1) syntax, which is why the current grammar adopts some curvilinear methods to save the nation, which brings a lot of unnecessary complexity.

LL (1) places a lot of restrictions on Python syntax. A related topic mentions that the following code cannot be parsed with the current parser (causing SyntaxError).

With (open ("a_really_long_foo") as foo

Open ("a_really_long_bar") as bar):

At this point, I believe you have a deeper understanding of "what are the new features of Python 3.9?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report