0

Can any explain me what is happening in this python program?

numbers = [76, 44 , 57, 88, 8] max = numbers[0] for num in numbers: if num > max: max = num print(num)

28th Aug 2019, 6:36 PM
Omkar Chavan
Omkar Chavan - avatar
2 Respostas
+ 3
It's finding the max value in an array. max is set to the first element in the array, but it could be set to any. Something this code could have done is start the loop at the 2nd element to make it more efficient, but isn't needed for a 5 element array. Then it loops through each element and compares the element (num) to the max. If the current element is greater than what is in max, you want to save that value right? So it is stored back into max, so that the next element can be compared to the new max. Finally the max number is printed.
28th Aug 2019, 6:58 PM
Zeke Williams
Zeke Williams - avatar
0
Can u explain it with the iteration format
28th Aug 2019, 7:04 PM
Omkar Chavan
Omkar Chavan - avatar