1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Explain Min and Max in Python

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to write code to find the min and max values in an array. I came across an implementation where the first element (at index 0) is directly assigned to the max, but also mistakenly to the min. This creates a problem when the first element happens to be large, but the minimum lies elsewhere in the array.


    arr = [1, 345, 234, 21, 56789]

    max = float('-inf')
    min = float('inf')

    for i in range(len(arr)):
    if arr> max:
    max = arr
    elif arr<min:
    min = arr

    print(f'max element is {max} and min element is {min}')



    If the first element is large (e.g., arr1 = [1, 345, 234, 21, 56789]), the min is incorrectly initialized to the first element without proper comparison logic.

    Continue reading...

Compartilhe esta Página