Python3.8
nums = list(range(10))
# 作用:把等号右面表达式的值 赋值给 这个符号 前面的变量
if (length := len(nums)) > 50:
print(length)
else:
print(length + 1)
name = 'huahua'
age = 18
print(f'{name=}, {age=}')
# name='huahua', age=18
def fun(a, b, /, c, d, *, e, f):
print(f'{a=}, {b=}, {c=}, {d=}, {e=}, {f=}')
Python3.9
d1 = {'a': 1, 'b': 2}
d2 = {'c': 3, 'd': 4, 'b': 5}
new_d = d1 | d2
print(new_d)
# {'a': 1, 'b': 5, 'c': 3, 'd': 4}
d1 = {'a': 1, 'b': 2}
d2 = {'c': 3, 'd': 4, 'b': 5}
d1 |= d2
print(d1)
# {'a': 1, 'b': 5, 'c': 3, 'd': 4}
s = 'Hello World everyone'
# 删除前缀
s.removeprefix('Hello ')
print(s)
# 删除后缀
s.removesuffix(' everyone')
print(s)
Python3.10
status = 100
match status:
case 400:
print('11')
case 500:
print('22')
case _:
print('default')
print(isinstance(9, int | str))
# True
from typing import TypeAlias
str_list: TypeAlias = list[str]
L1 = str_list(['1', '2'])
L2 = str_list([1, 2])
#执行静态类型检测器mypy test.py,结果:
#test.py:7: error: List item 0 has incompatible type "int"; expected "str"
#test.py:7: error: List item 1 has incompatible type "int"; expected "str"
#Found 2 errors in 1 file (checked 1 source file)
网友评论