How to find the maximum and minimum values in python? Here are two methods to introduce to you:
The first one
count = int(input('输入数据个数:\n')) a = 1 while a <= count: num = int(input('请输入第{}个数:'.format(a))) #字符串中的方法 if a == 1: #这句一定会执行,而且只执行一次,目的就是让你输入的第一个数作为根据与之后的数比较 max = min = num #第二个及以后的数都会走else, else: #第一次走else时,比较中的min和max都是你第一次输入的数,以后走else就不一定了 if num < min: min = num elif num > max: max = num a += 1 print('最大数据是:', max) print('最小数据是:', min)
Related recommendations: "Python Video Tutorial"
Second type
#思路:将用户输入的每个数据存入一个列表,然后调用min与max函数且以列表为参就可以了 count = int(input('请输入数据个数:\n')) list = [] for i in range(1, count+1): #注意range取值范围 list.append(int(input('请输入第{}个值:'.format(i)))) print(min(list)) print(max(list))
The above is the detailed content of How to find the maximum and minimum values in python. For more information, please follow other related articles on the PHP Chinese website!