Yolanda Hui
Yolanda Hui

Reputation: 97

How does this code compare numbers in the list and output the maximum?

myList=[1,2,3]
max=myList[0]

for i in range(1,len(myList)):
    if myList[i]>max:
        max=myList[i]

print(max)

It stores the number in first slot in a temporary variable and compares the 2 other numbers to the temporary variable.

How does it know which one is maximum?

It only stores:

myList[1] = max if myList[1] > max and 
myList[2] = max if myList[2] > max

There's no comparsion between myList[1] and myList[2] in the code.

How does the program know myList[2] is larger than myList[1] in here?

Upvotes: 0

Views: 1037

Answers (5)

noamgot
noamgot

Reputation: 4091

Actually, there is a comparison - if needed... There are 2 possible cases:

  1. If myList[1] > myList[0] then max = myList[1] and in the next iteration it compares myList[2] with max, which is similar to comparing myList[2] with myList[1].
  2. If myList[1] <= myList[0] then max remains equals to myList[0]. In this case we don't need to compare myList[2] with myList[1], because we already know that myList[0] is greater than or equals to myList[1], so we only need to compare myList[2] with myList[0]

This is a rather compact example, but it is true for larger lists...

Hope that it makes sense, comment if you need more clarifications.

Upvotes: 1

Prune
Prune

Reputation: 77875

This works just the way you might handle it with a large list on paper. You don't keep track of the position of the largest element, merely its value.

You start by looking at the first element, and marking that value as the max. At this point, it's also the minimum, and the median, and the mean, and ... :-)

Now, go down the list, one element at a time. Compare the element with the max. If the element is larger, then update the max to be that new, higher value. There is no explicit check of myList[1] vs mylist[2]. What happens is this, assuming (as you did) that myList[1] > myList[0]:

max = myList[0]           # myList[0] and max are now the same value
while ...
   if mylist[1] > max:    # This compares myList[1] > myList[0]
       max = myList[1]    # myList[1] and max are now the same value
   ... # Next loop iteration, i=2
   if myList[2] > max     # THIS is where you compare myList[2] and the value of myList[1]
   ...

Does that clear up the operation? If not, try adding some basic tracing:

for i in range(1, len(myList)):
    print(i, myList[1], max)
    if myList[i] > max:
        max = myList[i]

Upvotes: 0

Ghazaleh Kharadpoor
Ghazaleh Kharadpoor

Reputation: 146

The "max" variable just keep the maximum number that was assigned to it. In every steps of the loop, if the given number from list is bigger than the "max" variable, we assign that number to the "max".

Upvotes: 0

BlooB
BlooB

Reputation: 965

1- your max is set to the first element in the array -> max = 1

2- your comparing max to the elements in the list starting from the second position( in this case number 2)

3- 2 > 1 , so max is updated to number 2

3- you compare the new max(number 2) to number 3

4- 3>2 , so max is updated to number 3

5- you have reached the end of your list

6- max is printed - 3

Upvotes: 0

rahlf23
rahlf23

Reputation: 9019

The variable max here (poor variable name choice, considering max() is a built-in function in Python) is continually updated in the loop. If the current iteration of the for loop yields an element in myList that exceeds max in value, then max is assigned to that value and the loop continues to the next iteration until it has reached the end of myList.

Upvotes: 1

Related Questions