内置函数

1、关键字传值
test(arg1 = x, arg2 = y)
 
2、参数组
def test(x, *args, **kwargs): //**字典,*列表
 
3、test(1,2,3,4,x=5) 同 test(1,*[2,3,4], **{'x':5})
 
4、导入函数
import Hello as hl
from Hello import hello1 as hh1, hello2 as hh2
 
const = __import__('const')
print(const.Utf8)
 
5、引用全局变量关键字 global
 
6、引用上一级变量关键字 nonlocal
 
7、匿名函数
foo = lambda x:x+1
print(foo(10))
 
8、函数式编程(面向编程函数和数学函数)
1)不可变:不用变量保存状态,不修改变量
#非函数式
def test(x):
i = 2 * x
i = i + 1
return i
 
#函数式
def test(x):
return 2 * x + 1
 
2)第一类对象:函数即“变量”
高阶函数:函数名作为:返回值、参数
 
9、内置函数map
map(func, testList) //数据逐个处理,返回迭代器
eg:list(map(lambda x:x**2, [1, 2, 3, 4]))//[1, 4, 9, 16]
 
10、内置函数filter
filter(func,testList)//过滤数据,返回迭代器
eg:li = [1,2,3,4,6,7,8,9,0]
print(list(filter(lambda x:x%2==0, li))) //[2, 4, 6, 8, 0]
 
11、内置函数reduce
reduce(func, testList,initValue) //合并序列
eg:from functools import reduce
li = [1, 2, 3, 4]
print(reduce(lambda x,y:2*x+y,li,0))) //26
 
12、next函数
next(iter) //调用iter.__next__()
 
13、三元表达式
v = resultTrue if a == b else resultFalse
[i+1 for i in list if i== v] //列表
(i+1 for i in list if i== v) //生成器表达式
 
14、返回生成器
def func():
t = yield v
tt = yield y
 
test = func()
test.__next()__
test.send(value) //把value传递给yield
 
15、其他内置函数
abs(num) 绝对值
pow(x , y,z) x 的 y 次方 % z
round(3.5) 保留值将保留到离上一位更近的一端(四舍六入),如果距离两边一样远,会保留到偶数的一边
divmod(num1,num2) 得到(商,余数)
sum(list) 求和
all(list) 序列中的每个元素做Boolean运算,然后再and关联,
eg:print(all([1,2,3,''])) //False
print(all([1,2,3])) //True
print(all("")) //可迭代对象为空,返回True
print(all("1230")) //True
any(list) 序列中的每个元素做Boolean运算,然后再or关联
bin(num) 转二进制
hex(num) 转十六进制
oct(num) 转八进制
bool(v) 布尔运算: ''、'null'、None、0为False
bytes(str, encoding=) 字符串转字节
eg:name = "你好"
print(bytes(name,encoding=const.Utf8)) //b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(bytes(name,encoding=const.Utf8).decode(const.Utf8)) // 你好
chr(num) ASCII转字符
ord(num) 字符转ASCII
dict(map) new 一个字典
help(classobj) 打印解释
enumerate 将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据 //下标,一般用在 for 循环当中返回 enumerate(枚举) 对象。
eg: li = ["chenhaiquan", "wang"]
print(dict(enumerate(li))) //{0: 'chenhaiquan', 1: 'wang'}
eval(str) 1、执行一个字符串表达式,并返回表达式的值 2、提取字符串的数据结构
type(v) 取数据类型
isinstance() 数据类型判断
reversed(testList) 列表反转
zip(iter1,iter2) 拉链,返回元组序列 eg:list(zip((‘a’‘b’‘c’),(1,2,3)))
max(可迭代数据类型) 返回最大值
li = [ 'ac3','ac123','ac32','ac22']
print(max(li)) //ac32

d = {"chq_age":18, "why_age":29, "dda_age":36}
print(max(zip(d.values(), d.keys()))) //(36, 'dda_age')
print(max(d, key=lambda x : d[x])) // 'dda_age'
min 用法与max类似
sorted 用法与max类似
slice 定义切片
eg:
li = [1,2,3,4,5,6,7]
sl = slice(2, 6, 2)
print(li[sl]) //[3, 5]
   
hash() 可hash的数据类型等价于数据类型不可变
id() 返回内存地址
globals() 返回全局变量集合
locals() 返回局部变量集合
vars() 查看对象所有方法,以字典格式返回。如果参数为空,则取locals()
dir(classobj) 打印对象的所有方法
iter() 同  li.__iter__()
v1, v2 = v2, v1 v1,v2交换值
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2019-05-31 13:14  ChenHQ2048  阅读(96)  评论(0编辑  收藏  举报