一、abs()函数——获取绝对值
功能:返回数字的绝对值
语法:abs(x)
例子:
-
循环输出列表中定义的数字的绝对值
tupleabs = [12.45,0,-19.69]
for i in tupleabs:
print(abs(i))
输出结果:
12.45
0
19.69
2.定义函数,用于输出2个数字相乘后的绝对值
def aabs(value1,value2):
return abs(value1*value2)
print(aabs(12.96,8.2))
print(aabs(10,-10))
输出结果:
106.27199999999999
100
二、divmod()函数——获取商和余数的元组
功能:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a//b)(a%b)
语法:divmod(a,b) a:被除数 b:除数
例子:
1.计算从参数N开始,向下递减的每一个数字和参数A的除数及余数的结果值
def getDivmod(A,N):
while N >= 0:
sn = str(N)
sa = str(A)
result = ["divmod({0},{1}):".format(sn,sa),str(divmod(N,A))]
print("".join(result))
N = N - 1
getDivmod(3,10)
输出结果:
divmod(10,3):(3, 1)
divmod(9,3):(3, 0)
divmod(8,3):(2, 2)
divmod(7,3):(2, 1)
divmod(6,3):(2, 0)
divmod(5,3):(1, 2)
divmod(4,3):(1, 1)
divmod(3,3):(1, 0)
divmod(2,3):(0, 2)
divmod(1,3):(0, 1)
divmod(0,3):(0, 0)
2.模拟实现数据分页计算方法
def getSegment(curIndex,getSize):
abc = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
stratGet = (curIndex - 1) * getSize
getData = abc [stratGet :curIndex * getSize ]
totalIndexTuple = divmod(len(abc),getSize)
totalIndex = totalIndexTuple[0] +( 1 if totalIndexTuple[1] > 0 else 0)
return (getData,totalIndex)
result = getSegment(2,5)
print(result[0])
print(result[1])
输出结果:
['f', 'g', 'h', 'i', 'j']
6
三、sum()函数——求和计算
功能:函数对列表元组和集合等序列进行求和计算
语法:sum(iterable,start)
例子:
1.计算0-100之间所有偶数的和
evenNumber = []
i = 0
while i <= 100:
if i %2 == 0:
evenNumber.append(i)
i+=1
print(sum(evenNumber))
输出结果:
2550
2.计算二维序列中每个子序列的平均值以及整个序列的平均值
def avg(seq):
totalLen = 0
everySeq =[]
for i in seq:
Len = len(i)
everySeq.append(sum(i))
print(sum(i) / Len)
totalLen += Len
print(sum(everySeq) / totalLen)
avg([[10,12,13],[12,11,15],[2,5,4,2]])
输出结果:
11.666666666666666
12.666666666666666
3.25
8.6
四、round()函数——四舍五入
功能:返回浮点数四舍五入的值
语法:round(x,n)
例子:
1.实现加减乘除计算器
class myCalc:
def __init__(self,a,b):
self.a=a
self.b=b
def addition(self,Retain):
return round(self.a + self.b ,Retain)
def subtraction(self,Retain):
return round(self.a - self.b,Retain)
def multiplication(self,Retain):
return round(self.a * self.b,Retain)
def division(self,Retain):
return round(self.a / self.b,Retain)
while True:
get_num1 = input("请输入第一个数字:")
opera = input("请输入运算符:")
get_num2 = input("请输入第二个数字:")
get_retain = input("请输入保留小数位数:")
num1 = float(get_num1)
num2 = float(get_num2)
retain = int(get_retain)
result = 0.00
if opera == "+":
result = myCalc(num1,num2).addition(retain)
elif opera == "-":
result = myCalc(num1,num2).subtraction(retain)
elif opera == "*":
result = myCalc(num1,num2).multiplication(retain)
else:
result = myCalc(num1,num2).division(retain)
print("输出结果是:",result)
输出结果:
请输入第一个数字:3
请输入运算符:*
请输入第二个数字:7
请输入保留小数位数:2
输出结果是: 21.0
五、pow()函数——计算任意数n次方的值
功能:返回x的y次方的值
语法:pow( x , y [ , z ] )
例子:
1.编写一个进步累计器,每天进步一点点,计算一年提升的倍数
def progress(val):
num= 1 + val
yearProgress=pow(num,365)
output =""
if yearProgress == num :
output ="原地踏步"
else:
output = str(round(yearProgress,1))
print("一年进步值:",output+"倍")
#接受用户输入,然后调用progress函数
dayVal = input("每天进步值:")
progress(float(dayVal))
输出结果:
每天进步值:0.01
一年进步值: 37.8倍
每天进步值:0
一年进步值: 原地踏步倍
六、min()函数——取出给定参数的最小值
功能:获取指定数值或者指定序列中最小值
语法:min(a,b,c,…)
例子:
1.随机生成10个1到100之间的数字,再取出这些数字中最小值
import random
seq=[]
i=0
while i<10:
seq.append(random.randint(1,100))
i += 1
getMin = min(seq)
print("原列表值:",seq)
print("列表最小值:",getMin)
输出结果:
原列表值: [47, 83, 45, 69, 32, 71, 74, 92, 58, 68]
列表最小值: 32
七、max()函数——取出给定参数的最大值
功能:获取指定数值或者指定序列中最大值
语法:max(a,b,c,…)
例子:
1.定义产品各项参数值,实现按参数名获得该参数值最高的一个产品型号
def searchMax(item):
pro1 = {"product":"iphone xs","screen":5.8,"price":8699,"weight":"177克","depth":7.7}
pro2 = {"product":"iphone xs MAX","screen":6.5,"price":10699,"weight":"208克","depth":7.7}
pro3 = {"product":"iphone xr","screen":6.1,"price":6499,"weight":"194克","depth":8.3}
proList = [pro1,pro2,pro3]
a=max(proList, key=lambda x: x[item])
msg ={"product":"产品","screen":"屏幕尺寸","price":"起价","weight":"重量","depth":"厚度"}
print("您获取的参数值信息是:",msg[item])
return a
#通过接收用户输入的参数名,来获取相关产品
itemName = input("请输入要查找配置最高的参数名称:")
productItem = searchMax(itemName)
print(productItem)