赞
踩
abs(-2),abs(-2.3),abs(-3+4j)
# 输出:2, 2.3, 5.0
a = [] # 空返回True
b = [1,2]
c = [1,2,''] # 含有空字符串,False
d = [1,2,None] # 含有None,False
e = [0,1,2] # 含有0,False
f = [1,2,False] # 含有False,False
all(a),all(b),all(c),all(d),all(e),all(f)
# 输出(True, True, False, False, False, False)
a = [] # 空返回False
b = [1,2] # 1,2都是Ture
c = [False,'',0,None] # 没有一个是True
e = [1,False,'',0,None] # 有一个是就返回True
any(a),any(b),any(c),any(e)
# 输出(False, True, False, True)
a = None
b = ''
c = False
d = 0
e = ['',0] # 就算列表元素全是Flase,但是e不是一个空列表,所以返回True
bool(a),bool(b),bool(c),bool(d),bool(e)
# 输出False, False, False, False, True
callable(print),callable(callable),callable(np.array),callable(2)
# 输出 True, True, True, False
# 直接通过输入若干组关键字, key=value来创建字典
my_dict = dict(name = 'zhangsan', age=17)
# 通过传入若干组双值序列来创建字典,每个序列第一个元素为key,第二个为其对应的value
n1 = ('name','zhangsan')
n2 = ('age',17)
my_dict=dict((n1,n2))
# 通过映射函数zip方式来构造字典
keys = ['name','age']
values = ['zhangsan',17]
my_dict=dict(zip(keys,values))
divmod(11,3) # 输出(3,2)
list1 = ['a','b','c']
for i,v in enumerate(list1):
print(i,v)
# 输出: 0 a
1 b
2 c
def filter_function(x):
'过滤一个序列中比2小的元素'
if x >= 2:
return x
my_list = [0,1,2,3,4]
list(filter(filter_function,my_list)) # 输出为[2, 3, 4]
float(1),float('1.2') # 输出 1.0, 1.2
age = input('please input your age:') # 键盘上输入12
print(age) # 输出为字符串 '12'
iter_1 = iter([1,2,3])
for i in iter_1:
print(i)
1
2
3
def number_plus_1(original_number):
return original_number + 1
my_list = [1, 2, 3]
list(map(number_plus_1,my_list)) # 输出 [2, 3, 4]
def number_plus(num1,num2):
return num1+num2
# 函数有两个参数,需要传递两个序列。每个序列相同index的元素作为一组参数输入
my_list_1 = [1, 2, 3]
my_list_2 = [1,1,1]
list(map(number_plus,my_list_1,my_list_2)) # 输出[2, 3, 4]
max(1,2,10)
max([1,2,10])
open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None,closefd=True, opener=None)
pow(3,2) # 输出9,即 3的2次方
pow(3,2,2) # 输出1,即3的2次方为9,9再对2取余为1
list(reversed([1,2,3])) # 输出[3,2,1]
round(2.111) # 输出 2
round(2.111,1) # 输出 2.1
sorted([1,3,2]) # 返回[1,2,3]
sorted(['d','ee','ccc']) # 返回 ['ccc', 'd', 'ee']
sorted(['d','ee','ccc'],key=len) # 返回 ['d', 'ee', 'ccc']
class X:
a = 1
Y = type('X', (object,), dict(a=1))
Y # 输出__main__.X
a = [1,2,3]
b = [4,5,6]
c = [7,8,9,10]
list(zip(a,b)) # 返回[(1, 4), (2, 5), (3, 6)]
list(zip(a,c)) # a只有三个元素,所以只有三个元祖,返回[(1, 7), (2, 8), (3, 9)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。