## Finding maximum, minimum and average in Python

Liu Rihui 2021-04-06 18:35:31
finding maximum minimum average python

# One 、 subject

Answer the following questions in a loop ：

1、 From the outside world 10 It's worth , Output Max 、 minimum value 、 Average ;
2、 From the outside world 10 It's worth , Index that outputs the maximum value 、 Index of minimum value .

Ideas ： Make clear the knowledge needed by the topic first , Put it together again .

## 2.1 Getting data from the outside

input() function

There is no data type specified here , So we can take input（） The returned string is converted to integer or floating point type , Such as ： int(input(' Please enter an integer ：\n'))  float(input(' Please enter a floating point number ：\n'))

The data we get can be saved in a list . If the list is empty when it is created , You can add data continuously . such as ：

a=[]

a.append(int(input()))

If the list wasn't empty , You can replace the elements in the list by the list name and index ：

a=[1,2,3,4,5,6,7,8,9,10]

a=int(input())

Because there are many elements , So we can do it in a circular way , such as ：

i=0;

while i<10:

a.append(int(input())) # If the list is empty at first

a[i]=int(input())# If the list starts with data

i=i+1

## 2.2 Get maximum

We can define a maximum value by （ The initial value is a number in the list ）, Then compare the maximum value with all the elements in the list , If you find that there is a value greater than the maximum value , Let the maximum value be equal to the new maximum value / minimum value .

If you don't have to cycle , This is a ：

theMax=a

if a>theMax:

theMax=a

if a>theMax:

theMax=a

... In this way ：

if(a>theMax):

theMax=a

If you use a loop , We just need to make a change j Instead of the index of the list ：

j=0;

while j<len(a):

if j>theMax:

theMax=j

j++

Also, change the minimum value above to ：

theMin=a

if a<theMin:

theMin=a

if a<theMin:

theMin=a

... In this way ：

if(a<theMin):

theMin=a

If you use a loop , We just need to make a change j Instead of the index of the list ：

j=0;

while j<len(a):

if j<theMin:

theMin=j

j++

## 2.3 averaging

To find the average, first find the sum , The sum can be accumulated ：

sum=a+a+a+a+a+a+a+a+a+a

Or in a circular way ：

sum=0

for z in a:

sum=sum+z

Then divide by the number of elements len(a) You can get the average .

theAvg=sum/len(a)

## 2.4 Find the best index

Declare two variables , The index used to store the maximum and minimum values , The default is zero （ The maximum value you set 、 Where does the minimum start , Which is the starting value of the corresponding maximum index ）：

indexMax=0

indexMin=0

If it is to use for in : Cyclic , Get... With a temporary value ：

``````indexMax=0
indexMin=0
for j in a:
if j>theMax:# If you find a value that's better than theMax Also big
theMax=j
indexMax=a.index(j)# Get index through function
if theMin>j:# If you find a value that's better than theMin Still small
theMin=j
indexMin=a.index(j)# Get index through function
theSum=theSum+j``````

It can also be used. a.index() Get the index in the same way ：

``````q=0# Index of records
for j in a:
if j>theMax:# If you find a value that's better than theMax Also big
theMax=j
indexMax=q
if theMin>j:# If you find a value that's better than theMin Still small
theMin=j
indexMin = q
theSum=theSum+j
q = q + 1``````

Or by while Way to obtain ：

``````indexMax=0
indexMin=0
i=0# Index of records
while i<len(a):
if a[i]>theMax:
theMax=a[i]
indexMax=i
if theMin>a[i]:
theMin=a[i]
indexMin=i
theSum=theSum+a[i]
i=i+1``````

# 3、 ... and 、 Code Or use functions , But the original idea of this problem is to use circulation ： Be careful , although a.index(max(a)) You can get the index that appears , But this only returns the first occurrence of the maximum value

https://pythonmana.com/2021/04/20210406183354820G.html