python 内置方法

 

all方法

1 print(all([1, 2, -3]))
2 print(all([0, 1, 2]))
3 
4 #如果可迭代对象都为真,返回真,否则返回假
5 
6 输出结果
7 True
8 False
View Code

 

any方法

1 print(any([0, 0, 0]))
2 print(any([0, 1, 2]))
3 
4 #一个是真就为真,否则为假
5 
6 输出结果:
7 False
8 True
View Code

 

bin方法

1 a = bin(10)
2 print(a,type(a))
3 
4 输出结果:
5 0b1010 <class 'str'>
View Code

 

bytearray方法

1 a = bytearray("abcdefg", encoding= "utf-8")
2 a[0] = 64
3 print(a)
4 
5 #可以直接修改a
6 输出结果:
7 bytearray(b'@bcdefg')
View Code

 

byte方法

1 a = bytes("abcdefg", encoding= "utf-8")
2 print(a)
3 
4 #a无法修改
5 
6 输出结果
7 b'abcdefg'
View Code

 

chr方法

1 print(chr(98))
2 
3 #将数字转为字符
4 
5 输出结果:
6 b
View Code

 

ord方法

1 print(ord('b'))
2 
3 输出结果:
4 98
View Code

 

divmod方法

1 print(divmod(5,2))
2 
3 #求出除数和余数
4 输出结果:
5 (2, 1)
View Code

 

eval方法

1 x = 1
2 print(eval("x+1"))
3 
4 #对比较简单的字符串进行数学运算
5 输出结果:
6 2
View Code

 

filter方法

 1 res = filter(lambda x:x>5, range(10))
 2 type(res)
 3 for i in res:
 4     print(i)
 5 
 6 #通过表达式,将数据进行过滤
 7 
 8 输出结果:
 9 6
10 7
11 8
12 9
View Code

 

map方法

 1 res = map(lambda x:x*x, range(5))
 2 type(res)
 3 for i in res:
 4     print(i)
 5 
 6 
 7 输出结果:
 8 0
 9 1
10 4
11 9
12 16
View Code

 

reduces方法

1 import functools
2 
3 res = functools.reduce(lambda x,y:x+y, range(10))
4 print(res)
5 
6 #将前面的值一直相加
7 
8 输出结果:
9 45
View Code

 

globals方法

 1 value1 = "abc"
 2 
 3 def func():
 4     value2 = "efg"
 5 
 6 
 7 func()
 8 
 9 print(globals().get("value1"))
10 print(globals().get("value2"))
11 
12 
13 输出结果:
14 abc
15 None
View Code

 

locals方法

 1 value1 = "abc"
 2 
 3 def func():
 4     value2 = "efg"
 5 
 6     print(locals().get("value1"))
 7     print(locals().get("value2"))
 8 
 9 
10 func()
11 
12 
13 输出结果:
14 None
15 efg
View Code

 

round方法

1 print(round(3.15))
2 print(round(3.6))
3 print(round(3.225, 2))
4 
5 输出结果:
6 3
7 4
8 3.23
View Code

 

sorted方法

 1 a = {'a':2, 'k':4, 'c':1}
 2 print(sorted(a.items()))
 3 
 4 #按照key排序
 5 print(sorted(a.items(), key = lambda x:x[1]))
 6 
 7 
 8 输出结果:
 9 [('a', 2), ('c', 1), ('k', 4)]
10 [('c', 1), ('a', 2), ('k', 4)]
View Code

 

zip方法

 1 a = [1, 2, 3]
 2 b = ["a", "b", "c", "d"]
 3 
 4 for i in zip(a, b):
 5     print(i)
 6 
 7 
 8 输出结果:
 9 (1, 'a')
10 (2, 'b')
11 (3, 'c')
View Code

 

posted @ 2018-09-25 11:15  gogomoumou  阅读(142)  评论(0编辑  收藏  举报