函数和基础的补充
1.查看Python中所有的关键字(33个)
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>> [x for x in dir(dict) if not x.startswith('_')]
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefa
ult', 'update', 'values']
>>> [x for x in dir(list) if not x.startswith('_')]
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove
', 'reverse', 'sort']
2.如何查看一个函数是否有返回值,以及返回值的类型
>>> help(list.pop)
Help on method_descriptor:
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
>>> help(list.remove)
Help on method_descriptor:
remove(...)
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
# pop有返回值,remove没有返回值
>>> help(str.upper)
Help on method_descriptor:
upper(...)
S.upper() -> str
Return a copy of S converted to uppercase.
# 字符串的upper是返回一个新的字符串
3.zip()函数的用法
用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,如果各个迭代器的元素个数不一致,则按照长度最短的返回。这样做的好处是节约了不少的内存。
lst1 = [1, 2, 3]
lst2 = ['a', 'b', 'c', 'd']
lst3 = (11, 12, 13, 14, 15)
for i in zip(lst1, lst2, lst3):
print(i)
结果:
(1, 'a', 11)
(2, 'b', 12)
(3, 'c', 13)
4.双端队列和列表的区别
主要是效率和底层的数据结构上
如果从中间插入或者删除比较频繁的时候用双端队列
如果只是单纯的append和pop再加上查询比较多,这个时候就用列表
5.表达式和语句的区别
python代码是由表达式和语句组成,并由python解释器负责执行。它们的主要区别是:
“表达式”是一个值,它的结果一定是一个python对象。当python解释器计算它时,其结果可以是任意对象。例如42、1+2、int('123')、range(10)等。
结果不是对象的代码则称为“语句”。例如if或者print语句,for和while循环等。它们执行一个动作,而不是返回或生成一个值。
6.文件操作
以字符方式操作数据 | 以字节方式操作数据 | 光标初识位置 |
---|---|---|
r,rt | rb | 文件头 |
r+t,rt+ | r+b | 文件头 |
w,wt | wb | 清空文件 |
w+t,wt+ | w+b,wb+ | 清空文件 |
a,at | ab | 文件尾 |
a+,a+t,at+ | a+b,ab+ | 文件尾 |