做韦恩图网站,软件开发管理软件,网络文化经营许可证有什么用,苏州网站推广电话文章目录 formatted字符串字面值formatted字符串支持 字符串新方法变量类型标注二进制表示中数字为1的数量统计字典的三个方法新增mapping属性函数zip()新增strict参数dataclass字典合并match 语法 formatted字符串字面值
formatted字符串是带有’f’字符前缀的字符串#xf… 文章目录 formatted字符串字面值formatted字符串支持 字符串新方法变量类型标注二进制表示中数字为1的数量统计字典的三个方法新增mapping属性函数zip()新增strict参数dataclass字典合并match 语法 formatted字符串字面值
formatted字符串是带有’f’字符前缀的字符串可以很方便的格式化字符串
#旧版本
name 张三
print(姓名是%s%name)
print(姓名是{}.format(name))
#新版本
print(f{name}欢迎您加入)
works [数据分析,财务报表]
print(f工作内容:{works[0]},{works[1]}等)
#格式化字符串字面值formatted stringliteral可以在字符串常量内使用嵌入的Python表达式。
a 5
b 10
print(f表达式运算的结果{2*(ab)})输出结果
姓名是张三
姓名是张三
张三欢迎您加入
工作内容:数据分析,财务报表等
表达式运算的结果30formatted字符串支持 user zhangsan
info f{user}
print(info) # userzhangsan
#指定格式输出
name manager
#旧版本
print({:*^20}.format(name))
#新版本
print(f{name:*^20})
#数值类型的格式化输出
num 10
print(f{num:.2f}) # num10.00 :.2f代表控制输出2个小数
pct 0.834
#旧版本
print({:.2f}%.format(pct*100))
#新版本
print(f{pct*100:.2f}%)输出结果
userzhangsan
******manager*******
******manager*******
num10.00
83.40%
83.40%字符串新方法
方法名功能描述str.removeprefix()如果str以它开头的话将会返回一个修改过前缀的新字符串否则它将返回原始字符串。str.removesuffix()如果str以其结尾则返回带有修改过后缀的新字符串否则它将返回原始字符串。
info sutpc
print(info.removeprefix(sut))
print(info.removesuffix(pc))
输出结果
pc
sut变量类型标注
变量类型注解是用来对变量和函数的参数返回值类型做注解暗示帮助开发者写出更加严谨的代码让调用方减少类型方面的错误也可以提高代码的可读性和易用性。但是变量类型注解语法传入的类型表述能力有限不能说明复杂的类型组成情况因此引入了 typing 模块来实现复杂的类型表达。
TypeDescriptionTypeDescriptionint整型integerUnion联合类型float浮点数字Optional参数可以为空或已经声明的类型bool布尔int的子类Mapping映射是collections.abc.Mapping的泛型str字符(unicode)MutableMappingMapping对象的子类可变bytes8位字符Generator生成器类型,Generator[YieldType、SendType、ReturnType]object任意对象公共基类NoReturn函数没有返回结果List[str]字符组成的列表Set集合set的泛型,推荐用于注解返回类型Tuple[int, int]两个int对象的元组AbstractSetcollections.abc.Set的泛型推荐用于注解参数Tuple[int, …]任意数量的int对象的元组Sequencecollections.abc.Sequence的泛型list、tuple等的泛化类型Dict[str, int]键是str值是int的字典TypeVar自定义兼容特定类型的变量Iterable[int]包含int的可迭代对象Generic自定义泛型类型Sequence[bool]布尔值序列只读NewType声明一些具有特殊含义的类型Mapping[str, int]从str键到int值的映射只读Callable可调用类型,Callable[[参数类型],返回类型]Any具有任意类型的动态类型值NoReturn没法返回值
二进制表示中数字为1的数量统计
通过调用bit_count函数来统计二进制中数字“1”的个数
# bit_count()函数
#旧版本
value 5
print(bin(value).count(1))
#新版本
print(value.bit_count())字典的三个方法新增mapping属性
mydict {一:1,二:2,三:3}
#旧版本
print(mydict.keys(),mydict.values(),mydict.items())
#新版本
keys mydict.keys()
values mydict.values()
items mydict.items()
print(keys.mapping,values.mapping,items.mapping)输出结果
dict_keys([一, 二, 三]) dict_values([1, 2, 3]) dict_items([(一, 1), (二, 2), (三, 3)])
{一: 1, 二: 2, 三: 3} {一: 1, 二: 2, 三: 3} {一: 1, 二: 2, 三: 3}在Python3.10中针对于字典的三个方法itemskeys和values都增加了一mapping属性通过上诉的程序可以发现对三个方法调用mapping属性后都会返回原字典数据。
函数zip()新增strict参数
keys [one,two,three,four]
values [1,2,3,4,5]
#旧版本
print(dict(zip(keys,values)))
#新版本
print(dict(zip(keys,values,strictTrue)))对于zip函数加了strict参数顾名思义strict参数就是要严格的通过参数长度的匹配原则在以上代码中keys和values列表的长度并不一致。旧版本的zip函数会根据长度最短的参数创建字典。新版本的zip函数中当设定strict参数为True则要求zip的输入数必须要长度一致否则报错。
dataclass
为了支持数据修改, 默认值, 比较等功能。更加好一些的方法是使用自定义类来实现数据类。 dataclass 可以认为是提供了一个简写__init__ 方法的语法类型注释是必填项 (不限制数据类型时, 添加typing.Any为类型注释), 默认值的传递方式和__init__ 方法的参数格式一致。
from dataclasses import dataclass
from typing import ClassVar
dataclass
class Player:name:strnumber:intpostion:strage:int
p1 Player(zhangsan,18,PG,26)
print(p1)
输出结果
Player(namezhangsan, number18, postionPG, age26)字典合并
字典添加两个新的运算符[|]和[|]。[|]运算符用于合并字典。[|]用于更新字典。
dict1 {name:zhangsan}
dict2 {type:manager}
#旧版本
dict1.update(dict2)
print(dict1)
#新版本
dict3 dict1 | dict2
print(dict3) # {name: zhangsan, type:mananger}
#[|]
dict1 | dict2 # 等价于dict1 dict1 | dict2
print(dict1)
输出结果
{name: zhangsan, type: manager}
{name: zhangsan, type: manager}
{name: zhangsan, type: manager}match 语法
match...case结构化模式匹配可以匹配字典、类以及其他更复杂的结构。match...case的匹配模式匹配于Java或C中的switch的使用很相似。
# 语句结构match subject:case pattern_1:action_1case pattern_2:action_2case pattern_3:action_3case _:action_wildcard
status 200
match status:case 200:print(访问成功)case 404:print(页面丢了)case _:print(不能识别)#模式匹配
person1 (James,23,male)
person2 (Lili,33,female)
person3 (Mary,34,female)
def func(person):match person:case (name,_,female):print(f{name} is woman)case (name,_,male):print(f{name} is man)case (name,age,gender):print(f{name} is {age} old)
#调用函数
func(person1)
func(person2)
func(person3)输出结果
James is man
Lili is woman
Mary is woman 文章转载自: http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn