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

爱喝马黛茶的安东尼
Release: 2019-06-25 15:01:33
Original
11297 people have browsed it

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)
Copy after login

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))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template