python3.6新功能_python3 9的新功能

python3.6新功能

Python 3.9rc1 was released on September 14, 2020. This release has some good new features, improvements, etc. In this blog, we are going to discuss 7 of the new features

Python 3.9rc1于2020年9月14日发布。此版本具有一些不错的新功能,改进等。在此博客中,我们将讨论其中的7个新功能。

1.词典合并和更新运算符: (1. Dictionary merge and update operator:)

merge (|): This operator is used for merging two dictionaries and returning a new dictionary as an output. If any key is duplicated then the value of the RHS is updated in the final dictionary.

merge(|) :此运算符用于合并两个字典并返回新字典作为输出。 如果有任何密钥重复,那么RHS的值将在最终字典中更新。

Sample code:

样例代码

a = {“a”: “a1”, “b”: “b1”}b = {“c”: “c2”, “d”: “d2”}c = a | bprint(f’{a=}’)print(f’{b=}’)print(f’{c=}’)

Output:

输出

a={‘a’: ‘a1’, ‘b’: ‘b1’}b={‘c’: ‘c2’, ‘d’: ‘d2’}c={‘a’: ‘a1’, ‘b’: ‘b1’, ‘c’: ‘c2’, ‘d’: ‘d2’}

Sample code:

样例代码

a = {“a”: “a1”, “b”: “b1”}b = {“a”: “a2”, “d”: “d2”}c = a | bprint(f’{a=}’)print(f’{b=}’)print(f’{c=}’)

Output:

输出

a={‘a’: ‘a1’, ‘b’: ‘b1’}b={‘a’: ‘a2’, ‘d’: ‘d2’}c={‘a’: ‘a2’, ‘b’: ‘b1’, ‘d’: ‘d2’}

update(|=): This operator is doing the same operation as the merge operator but here instead of creating a new dictionary LHS is getting updated. In case of key duplication the value of RHS gets updated.

update(| =):此运算符执行的操作与merge运算符相同,但此处不是创建新字典,而是更新LHS。 在重复密钥的情况下,RHS的值会更新。

Sample code:

样例代码

a = {“a”: “a1”, “b”: “b1”}b = {“c”: “c2”, “d”: “d2”}a |= bprint(f’{a=}’)print(f’{b=}’)

Output:

输出

a={‘a’: ‘a1’, ‘b’: ‘b1’, ‘c’: ‘c2’, ‘d’: ‘d2’}b={‘c’: ‘c2’, ‘d’: ‘d2’}

2. removeprefix()和removesuffix()方法 (2. removeprefix() and removesuffix() methods)

removeprefix(): This method returns a text after removing the given prefix

removeprefix():此方法在删除给定前缀后返回文本

Sample code:

样例代码:

text = “prefixtext”prefix_removed_text = text.removeprefix(“prefix”)print(prefix_removed_text)

Output:

输出:

text

Sample code:

样例代码:

text = “abcabctext”print(text.removeprefix(“abc”))

Output:

输出:

abctext

removesuffix(): This method return a text after removing the given suffix

removesuffix():此方法删除给定的后缀后返回文本

Sample code:

样例代码:

text = “textsuffix”suffix_removed_text = text.removesuffix(“suffix”)print(suffix_removed_text)

Output:

输出:

text

Sample code:

样例代码:

text = “textabcabc”print(text.removesuffix(‘abc’))

Output:

输出:

textabc

3.新的解析器 (3. New parser)

Python 3.9 uses a new parser based on PEG instead of LL(1). The performance of the new parser is roughly comparable with the old parser. But this new parser increases the flexibility of Python to accommodate new features in the future.

Python 3.9使用基于PEG而不是LL(1)的新解析器。 新解析器的性能与旧解析器大致相当。 但是,这个新的解析器增加了Python的灵活性,以适应将来的新功能。

In Python 3.10 the old parser is completely removed. In Python 3.9 we can switch to the old parser by setting an environment variable PYTHONOLDPARSER=1

在Python 3.10中,旧的解析器已完全删除。 在Python 3.9中,我们可以通过设置环境变量PYTHONOLDPARSER = 1切换到旧的解析器

4.新模块zoneinfo(4. New module zoneinfo)

Added new module zoneinfo which contains the IANA timezone database to our standard library

在我们的标准库中添加了包含IANA时区数据库的新模块zoneinfo

Sample code:

样例代码:

from datetime import timedeltadt = datetime(2020, 1, 1, 12, tzinfo=ZoneInfo(‘Asia/Kolkata’))print(dt)print(dt.tzname())

Output:

输出:

datetime.datetime(2020, 1, 1, 12, 0, tzinfo=zoneinfo.ZoneInfo(key=’Asia/Kolkata’))‘IST’

5.为multiprocessing.SimpleQueue添加了close方法 (5. Added close method for multiprocessing.SimpleQueue)

Added a close method for the queue. We can explicitly close the queue, methods like get(), put(), empty() no longer work after this. If we tried to access after close it will raise an OSError exception.

为队列添加了关闭方法。 我们可以显式关闭队列,之后诸如get(),put(),empty()之类的方法将不再起作用。 如果我们在关闭后尝试访问,它将引发OSError异常。

Sample code:

样例代码:

a = SimpleQueue()a.put(“test”)a.put(“test”)a.close()a.put(“test”)

Output:

输出:

OSError: handle is closed

6.数学模块中的LCM (6. LCM in math module)

Added lcm in math module. It returns the lcm of the operand.

在数学模块中添加了lcm 。 它返回操作数的lcm。

Sample code:

样例代码:

import mathprint(math.lcm(100, 150))print(math.lcm(7, 8, 2, 56))

Output:

输出:

300
56

7.内置泛型类型 (7. Builtin Generic Types)

We can now use built-in collection types such as list and dict as generic types instead of importing from the typing module.

现在,我们可以使用内置的收集类型(例如list和dict)作为泛型类型,而不是从键入模块中导入。

Python 3.9:

Python 3.9:

 def greet_all(names: list[str]) -> None:     for name in names:         print(“Hello”, name)

Before Python 3.9:

在Python 3.9之前:

from typing import Listdef greet_all(names: List[str]) -> None:    for name in names:        print(“Hello”, name)

There are so many other changes in Python 3.9 . You can refer the changes by clicking here.

Python 3.9还有许多其他更改。 您可以通过单击此处参考更改。

翻译自: https://medium.com/swlh/what-is-new-in-python3-9-21bfac42382a

python3.6新功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值