Python基础之八(拆包、高阶函数)

"""
一、拆包
1、result = test01(*list_test)
list_test = [1, 3, 4, 6, 8]


def test(*args):
    return args


print(test(1, 3, 4, 6, 8))
result = test(*list_test)  # 等价于上一行写法,该写法就是对列表list_test进行拆包
print(result)

  输出结果:

(1, 3, 4, 6, 8)
(1, 3, 4, 6, 8)
2、result = test(**dict_test)
dict_test = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}


def test(**kwargs):
    return kwargs


print(test(key1='val1', key2='val2', key3='val3'))
result = test(**dict_test)  # 等价于上一行写法,该写法就是对字典dict_test进行拆包
print(result)

  输出结果:

{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
3、tuple1,tuple2,tuple3 = (1,2,3)
二、内置函数
import builtins
print(dir(builtins))
print()
input()
type()
len()
list()、tuple()、dict()、str()、set()、bool()、int()、float()
sum()
id()
range()

三、高阶函数【了解】

文件操作
模块导入
"""

"""
open函数
一、读文件【掌握】
语法:
file = open(file='test.txt',mode='r')
test=file.read()
print(test)
file.close()
使用
1、file.read() 读取整个文件内容(全部读取出来)
2、file.readline() 读取文件的第一行数据,遇到\n就停止
3、file.readlines() 读取文件逐行读取,会读取换行符,返回所有行组成的list

二、写文件【了解】
1、file.write("test") 覆盖写入
1.1、不会自动换行,需要换行必须加\n
1.2、文件保存:
自动保存:先写进缓存区,等程序运行结束之后,再一次性写进文件
手动保存:flush()
2、file.flush()

三、操作模式【掌握】
1、r;读
2、w:覆盖写入
3、a:追加写入
4、+(表示同时可读写)
r+:可读可写,覆盖写入
w+:可读可写,覆盖写入
a+:可读可写,追加写入
5、二进制(打开图片、上传图片)
1、rb:二进制的方式读
2、wb:二进制的方式写
3、ab:二进制的方式追加写

四、光标操作【重要,自动化用不着】
file.seek(offset,whence)
1、offset:开始的偏移量,表示从哪里开始读取
2、whence:
0:从文件开头开始读
1:表示从当前光标位置开始读
2:表示从文件末尾开始
"""
posted @ 2021-07-16 00:14  阿炳~  阅读(79)  评论(0)    收藏  举报