一、 关于数学运算
(一) abs() 计算绝对值
参数必须是数值类型,且每次计算一个。参数是str类时报错。需要对list每一个元素求abs,可使用遍历或者map()。
因为字典是不可遍历的,所以也无法使用map(),只能按需逐个求解。
#abs()求绝对值,参数需要是数值类型,且每次只能计算一个#int
a = 1b= -1
print('abs({}) = {}'.format(a,abs(a))) #abs(1) = 1
print('abs({}) = {}'.format(b,abs(b))) #abs(-1) = 1#布尔值
c =Trueprint('abs({}) = {}'.format(c,abs(c))) #abs(True) = 1
c =Falseprint('abs({}) = {}'.format(c,abs(c))) #abs(False) = 0
## abs()函数参数不能是str类型#c = 'erf'#print('abs({}) = {}'.format(c,abs(c)))## TypeError: bad operand type for abs(): 'str'
#对于列表统一求绝对值,需要遍历或者map()
d = [12,-34,5,6,-3,9.3,-3.3]#map()
abs_d =map(abs,d)print(abs_d) #
print(list(abs_d)) #[12, 34, 5, 6, 3, 9.3, 3.3]#遍历
abs_d =[]for i ind:
abs_d.append(abs(i))print(abs_d) #[12, 34, 5, 6, 3, 9.3, 3.3]
View Code
(二) max()与 min() 求最大最小值
li = [1,3,5.4,3,-3,-9.4]print(max(li),min(li)) #5.4 -9.4#各比较数值直接做参数
print(max(3,2,5)) #5
View Code
(三) sum() 求和
注意,sum()函数的参数应该是一个list。
#print(sum(6,5)) # 报错
print(sum([6,5])) #11
View Code
(四) divmod() 除法
注意:此函数使用时,第一个参数数被除数,第二个参数是除数,结果是一个元组,元组第一个元素是商,第二个元素是余数。除数为0时,报错。在分页时常用此函数。
print(divmod(5,3)) #(1, 2)
print(divmod(5.5,3)) #(1.0, 2.5)#print(divmod(5,0)) # 报错
View Code
(五) pow() 求幂
两个参数时,求幂:pow(x,y) = x**y
三个参数时,求幂后取余:pow(x,y,z) = pow(x,y)%z = x**y%z
#pow(x,y) = x**y
print(pow(2,3)) #8
print(pow(2,-3)) #0.125
print(pow(2,3.5)) #11.313708498984761
print(pow(2,-3.5)) #0.08838834764831845#pow(x,y,z) = pow(x,y)%z = x**y%z#三个参数时,表示求幂后取余数,三个参数需要全部是int
print(pow(2,3,3)) #2
View Code
注意:三个参数时,需要全部为int。
(六) round() 四舍五入
参数可以是数值,也可以是数值表达式。
print(round(3.14)) #3
print(round(3.54)) #4
print(round(4-3*2+4%3)) #-1
View Code
(七)complex() 虚数运算
可直接进行两个虚数的运算,也可以生成一个新虚数。complex(a,b)= a+bj
#complex(a,b)= a+bj
comdata = complex(3,2)print(comdata,type(comdata)) #(3+2j)
comdata = complex(3.5,2.5)print(comdata) #(3.5+2.5j)#只有一个参数,默认虚部为0
comdata = complex(3)print(comdata) #(3+0j)#comdata = complex('a')#print(comdata,type(comdata)) # 报错#计算#complex(a,b)=a+bj
comdata = complex(1+2j,3)print(comdata) #(1+5j)
comdata = complex(1+2j,3+6j)print(comdata) #(-5+5j)
#参数可以是一个数字字符,其他str报错
comdata = complex('1')print(comdata,type(comdata)) #(1+0j)
View Code
二、过滤:对可迭代对象中的每一个元素,应用指定函数。
常与lambda()、三元运算同时使用。
(一) map() 返回一个新的map对象,记录每一个元素应用指定函数后的结果。
map_myList = map(lambda x:x+5,myList)print(map_myList) #
print(list(map_myList)) #[6, 8.4, 9, 3]
x= [1,2,3,4]
y= [4,3,2,1]
ret= map(lambda x,y:x+y,x,y)print(list(ret)) #[5, 5, 5, 5]
View Code
(二) filter() 返回一个新的filter对象,记录应用指定函数后值为True的所有元素。
#与lambda()同时使用
a = [12, 34, 5, 6, 3, 9.3, 3.3]
ret= filter(lambda x:x>4,a)print(ret,type(ret))#
print(list(ret)) #[12, 34, 5, 6, 9.3]#去掉假值
a = ['', 0, [], 6, (), 3, 3.3]
ret=filter(None,a)print(ret) #
print(list(ret)) #[6, 3, 3.3]
View Code
a = [1,2,3,4]
ret= map(lambda x:True if x<3 elseNone,a)print(list(ret))#[True, True, None, None]
a= [1,2,3,4]
ret= filter(lambda x:True if x<3 elseNone,a)print(list(ret))#[1, 2]
View Code
三、 判断真假:[]、{}、()、0为假,其余为真。
(一) all() 判断迭代对象中元素是否全部为真
参数需要是迭代对象,对象内没有元素,返回True;对象内有元素,且元素全部为真时,返回True。
#空元素为假,空格为真
myList = ['3',3,'er',' ']print(all(myList)) #True
myList = ['3',3,'er','']print(all(myList)) #False#0为假
myList = ['3',0,'er',' ']print(all(myList)) #False#空对象为真
myList =()print(all(myList)) #True
myList = ''
print(all(myList)) #True
View Code
(二) any() 判断迭代对象中元素是否有真
参数需要是迭代对象,对象内没有元素,返回False;对象内有元素,且有元素不为假时,返回True。
#有元素为真,返回True
myList = ['3',3,'er',' ']print(any(myList)) #True
myList = ['3',3,'er','']print(any(myList)) #True#没有元素为真,返回False
myList = [0,'',[]]print(any(myList)) #False#空对象为假,返回False
myList =()print(any(myList)) #False
myList = ''
print(any(myList)) #False
View Code
(三) bool() 将一个对象转换成布尔值。
空对象返回False,对象内含有真元素时返回True。
bool()
a=[]print(bool(a)) #False
a = ['',2]print(bool(a)) #True
print(bool()) #False
View Code
四、 将任意对象转化为字符串
(一) repr() 将任意对象转化为python解释器可读的字符串
实际上,repr()与__repr__()对应。运行repr()时,相当于到对象类中寻找__repr__方法,并获取返回值(一个可以表示对象的可打印字符串)。
ASCII()与repr函数类似,当对象元素具有ascii码值时,返回值同repr(),否则就会输出\u、\U、\x等字符表示。
#ascii
s = 'hello'
print(repr(s)) #'hello'
print(ascii(s)) #'hello'
s= '你好'
print(repr(s)) #'你好'
print(ascii(s)) #'\u4f60\u597d'
View Code
(二)str()将任意对象转化为人可读的字符串
a = 'hi,world!'
print(repr(a),type(repr(a))) #'hi,world!'
print(str(a),type(str(a))) #hi,world! #将任意值转成字符串
a = 3+2
print(repr(a),type(repr(a))) #5
print(str(a),type(str(a))) #5
a = 1.0/7.0
print(repr(a),type(repr(a))) #0.14285714285714285
print(str(a),type(str(a))) #0.14285714285714285 #将列表等对象转换成字符串
a = [12, 34, 5, 6, 3, 9.3, 3.3]print(repr(a),type(repr(a))) #[12, 34, 5, 6, 3, 9.3, 3.3]
print(str(a),type(str(a))) #[12, 34, 5, 6, 3, 9.3, 3.3]
a = {'k1':'k1','k2':2}print(repr(a),type(repr(a))) #{'k1': 'k1', 'k2': 2}
print(str(a),type(str(a))) #{'k1': 'k1', 'k2': 2}
View Code
对象应用repr()转化成字符串后,可以用eval()还原。
a = {'k1':1,'k2':2}print(a,type(a)) #{'k1': 1, 'k2': 2}
b = repr(a) #将字典转换成字符串
print(b,type(b)) #{'k1': 1, 'k2': 2}
c = eval(b) #将字符串还原回字典
print(c,type(c)) #{'k1': 1, 'k2': 2}
View Code
五、关于进制转换
(一) bin() 将整数转化成二进制字符串
a = 3435bin_a= bin(a) #取二进制字符串
print(bin_a,type(bin_a)) #0b110101101011
c =eval(bin_a)print(c,type(c)) #3435
View Code
(二) oct() 将整数转换成八进制字符串
hex() 将整数转换成十六进制字符串
a = 3434
print(a,type(a)) #3434
oct_a =oct(a)print(oct_a,type(oct_a)) #0o6552
print(eval(oct_a)) #3434
hex_a =hex(a)print(hex_a,type(hex_a)) #0xd6a
print(eval(hex_a)) #3434
View Code
注意:只能转化整数,float类型做参数时会报错。数值字符串可以用eval()还原为int。
六、 enumerate() 将一个可迭代对象转换成一个索引序列
通过enumerate得到的索引序列,可以同时使用索引和值,常用在for循环中,可以指定索引开始值。
a = ['name','age','sex','num']#得到一个索引序列,默认序号从0开始
ret =enumerate(a)print(ret,type(ret))##应用在for循环中,打印
for index,item inret:print(index,':',item)#得到一个索引序列,序号从1开始
ret = enumerate(a,1)for index,item inret:print(index,':',item)
View Code