Python3 数字
描述
max() 方法返回给定参数的最大值,参数可以为序列。
语法
以下是 max() 方法的语法:
max( x, y, z, .... )
参数
返回值
返回给定参数的最大值。
实例
以下展示了使用max() 方法的实例:
#!/usr/bin/python3 print ("max(80, 100, 1000) : ", max(80, 100, 1000)) print ("max(-20, 100, 400) : ", max(-20, 100, 400)) print ("max(-80, -20, -10) : ", max(-80, -20, -10)) print ("max(0, 100, -400) : ", max(0, 100, -400))
以上实例运行后输出结果为:
max(80, 100, 1000) : 1000 max(-20, 100, 400) : 400 max(-80, -20, -10) : -10 max(0, 100, -400) : 100
笔记
max(x, y[, z...]):Number|Sequence 入参类型不能混入(要么全Number(int|float|complex|bool),要么全序列)。
入参是序列的话: 单序列入参,返回序列中最大的一个数值多序列入参, 按索引顺序,逐一对比各序列的当前索引位的“值”,直到遇见最大值立即停止对比,并返回最大值所在的序列(也就是说,多序列入参,返回值依旧是一个序列,而不是数值)
>>> import sys; sys.version '3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]' >>> max(0, True) #布尔喔 True >>> max([1,2,3]) 3 >>> max(1,2,4) 4 >>> max(-1, -0.5, 0) 0 >>> max((1,2,3)) 3 >>> max([2,4], [3,6]) [3, 6] >>> max([2,4], [1,5]) [2, 4] >>> max([2,4], [1,5], [3,1], [2,5],[0,7]) [3, 1] >>> max((1,2,3), (3,3,0)) (3, 3, 0) >>> max((1,-1,0), (True,False,0)) #布尔喔 (True, False, 0) >>> max((1,-1,0), (True,False,2,0),(1, 0, 0, 2)) (True, False, 2, 0) >>> max((1,-1,0), (True,),(1,)) (1, -1, 0) >>> max((-1,-1,0), (True,),(1,)) (True,) >>> max([1,3,2],3,4) #非法入参 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'int' and 'list' >>> max((1,2,3), [2,4,1]) #非法入参 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'list' and 'tuple'