Python入门之内置函数
python 3 内置函数
python中的一些内置函数,能大大提高软件开发及运维的工作效率;以下是python 3中常用函数介绍:
1、abs():绝对值函数
a = 10 b = -110 print(abs(a),abs(b)) 执行结果: 10 110
2、all():传入序列参数的元素都是真,all()才是真;空字符串、空list/tuple/dict、None、0为假,其余为真
a = "chengd" b = range(1,10) c = [1,2,3,0] print(all(a)) print(all(b)) print(all(c)) 执行结果: True True False #如果传入参数不是序列,则会报错;可自己试验
3、any():传入序列参数的元素只要有一个为真,any()就为真
a = (0,0) b = range(0,10) c = [1,2,3,""] print(any(a)) print(any(b)) print(any(c)) 执行结果: False True True
4、bin(int):10进制转2进制
a = 255 b = 1 print(bin(a)) print(bin(b)) 执行结果: 0b11111111 0b1 #不满8位时,第一个1前面的0可以省略,即相当于0b0000001
5、bool():布尔类型;
空字符串、空list/tuple/dict、None、0为假,其余为真
6、callable(func):判断函数func是否可执行
def functions(): pass print(callable(functions)) #functions函数可以被调用执行 print(callable(functions())) #functions()已经被调用执行,返回的事函数结果,而函数结果不可以执行,所以false 执行结果: True False
7、chr():ascii转换字符串
a = 80 b = 33 c = 119 print(chr(a)) print(chr(b)) print(chr(c)) 执行结果: P ! w
8、ord():字符串转换ascii
a = "$" b = "B" c = "~" print(ord(a)) print(ord(b)) print(ord(c)) 执行结果: 36 66 126
9、divmod(int1,int2):divmod(a,b)方法返回的是a//b(除法取整)的结果和a对b的余数组成的一个元组
a = 11 b = 2 print(divmod(a,b)) print(type(divmod(a,b))) 执行结果: (5, 1) <class 'tuple'>
10、enumerate(seq,num):为序列生成序号,可以指定起始序号
shop = ['电脑','鲜花','吹风机'] for i,items in enumerate(shop,0): print(i,items) 执行结果: 0 电脑 1 鲜花 2 吹风机
11、filter(func,seq):过滤;循环后面seq序列,每次循环值被前面函数过滤后,如果为True生成新序列并加入此元素
li = [11,22,33,44,100,1111] def func(x): if x > 33: return True else: return False new_li = filter(func,li) print(new_li) print(type(new_li)) print(tuple(new_li)) 执行结果: <filter object at 0x0000000000D22438> <class 'filter'> (44, 100, 1111) #list、tuple函数调用filter类型的数据,可以生成对应的序列
12:、map(func,seq):映射;循环后面seq序列,每次循环值被前面函数处理生成新序列并加入元素
t = (1,2,3,4) n_t = map(lambda x : x + 100,t) print(n_t) print(type(n_t)) print(tuple(n_t)) 执行结果: <map object at 0x0000000000A6B9B0> <class 'map'> (101, 102, 103, 104) #list、tuple函数调用map类型的数据,可以生成对应的序列
13、float():浮点型
14、format():字符格式化函数
- 使用{}占位符或使用{0},{1}形式占位符
>>> "{} {} welcome to you!".format("python","world") 'python world welcome to you!' >>> "{0} {1} welcome to you!".format("python","world") 'python world welcome to you!' >>> "{1} {0} welcome to you!".format("world","python") 'python world welcome to you!'
- 使用关键字参数
>>> "NAME:{name} AGE:{age}".format(name = "chengd",age = "99") 'NAME:chengd AGE:99'
- 使用tuple,list,dict的下标
>>> list1 = ["chengd",99] >>> list1 ['chengd', 99] >>> "Name:{0[0]} Age:{0[1]}".format(list1) 'Name:chengd Age:99'
-
填充与对齐:^、<、>分别是居中、左对齐、右对齐,后面带宽度;:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
>>> name = "chengd" >>> name 'chengd' >>> ":>20".format(name) ':>20' >>> "{:>20}".format(name) ' chengd' >>> "{:*>20}".format(name) '**************chengd' >>> "{:<20}".format(name) 'chengd ' >>> "{:*<20}".format(name) 'chengd**************' >>> "{:^20}".format(name) ' chengd ' >>> "{:*^20}".format(name) '*******chengd*******'
15、fronzenset():不能修改集合
16、globals():全局可用变量;locals():局部变量
参考:http://blog.csdn.net/sxingming/article/details/52061630
17、len():返回参数长度函数
a = "chengd" b = [1,2,3,4,5,6] c = range(0,100) print(len(a)) print(len(b)) print(len(c)) 执行函数: 6 6 100
18、max():返回最大值
19、min():返回最小值
20、range():生成序列函数,可以指定起始、结尾、步长
l1 = list(range(10)) l2 = list(range(1,10)) l3 = list(range(1,10,2)) print(l1) print(l2) print(l3) 执行结果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 3, 5, 7, 9]
21、reversed():返回序列seq的反向访问的迭代子。参数可以是列表,元组,字符串,不改变原对象。
l = [1,2,3,4,5] l1 = [] for i in reversed(l): l1.append(i) print(l) print(l1) 执行结果: [1, 2, 3, 4, 5] [5, 4, 3, 2, 1]
22、sortd():排序函数
l = [1,3,4,2,18,3] s = "chengd" print(sorted(l)) print(sorted(s)) 执行结果: [1, 2, 3, 3, 4, 18] ['c', 'd', 'e', 'g', 'h', 'n']
23、round():四舍五入,小数点后数字小于等于5的舍去并且整数不变;小数点后数字大于5的舍去但整数加1。
n = 2.6 a = 2.5 x = 2.1 m = round(n) b = round(a) y = round(x) print(m) print(b) print(y)