Python学习记录(一):常用操作
range函数
range(start, stop[, step]) [start, stop) 区间左闭右开,同切片操作
>>> for i in range(5):
... print(i)
>>> 1 2 3 4 5
字典的元素个数
len(dict) 返回字典中的元素(键值对)个数
Python字符串包含

assert 断言
表达式为False时触发异常AssertionError。不满足条件时直接返回错误,而不是等程序奔溃后退出。

字符串大小写
转为小写lower(), 转为大写upper()

Ref. https://www.cnblogs.com/cmnz/p/6956984.html
Python之filter函数

说明:7个一组(0-6)、(7-13), 取出每一组中间的那个值,即3, 10, 17等等
Python关键字
Note: 代码中的变量不能与内置的关键字相同,并且不同版本Python的内置关键字有可能是不同的
>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', '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']
执行命令行指令
import os
os.system(cmd)
Python计时
import time
start_t = time.time()
# type your code here
print("Runtime: {} seconds".format(time.time() - start_t))
any/all函数
mylist = [False, True, False]
x = any(mylist) # True
y = all(mylist) # False

浙公网安备 33010602011771号