【合集】python 的一些妙用,推导式、三元表达式、with as 等,


自己常用的内置函数

函数如下:

dir len str list tuple zip map reduce(现在并入了functools中)



常用的进制转换

lambda 表达式

推导式生成

三元表达式

妙用固定参数、可变参数、默认参数

def func(name,age,sex = '男',*args,**kwds)

with as 一种上下文管理器,如打开文件

一般的是:

file = open("/log.txt")
data = file.read()
file.close()

存在的问题:

file = open("/log.txt")
try:
    data = file.read()
finally:
    file.close()

使用with as后:

with open("/tmp/foo.txt") as file:
    data = file.read()

for else 相当于执行完for后执行else







后面将更新,常用函数的具体用法,未完待续~~~


相关内容

    暂无相关文章

评论关闭