Home  >  Article  >  Backend Development  >  How to find the maximum and minimum values ​​in python

How to find the maximum and minimum values ​​in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-25 15:01:3311308browse

How to find the maximum and minimum values ​​in python

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(&#39;请输入第{}个数:&#39;.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(&#39;最大数据是:&#39;, max)
print(&#39;最小数据是:&#39;, min)

Related recommendations: "Python Video Tutorial"

Second type

#思路:将用户输入的每个数据存入一个列表,然后调用min与max函数且以列表为参就可以了
count = int(input(&#39;请输入数据个数:\n&#39;))
list = []
for i in range(1, count+1):   #注意range取值范围
    list.append(int(input(&#39;请输入第{}个值:&#39;.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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn