Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Sequences Sequence Operations Len, Min, and Max

Dat Ngo
Dat Ngo
2,586 Points

about the min and max function

Is that these functions depend on the ascii code of the elements?

3 Answers

Josh Keenan
Josh Keenan
20,315 Points

No, it takes an iterable object and compares the items to each other to find the biggest (max()) or the smallest (min()) item in the iterable.

These two functions break it down to its simplest form of understanding.

def minimum(iterable):      # iterable is any iterable object, like a list
    smallest = iterable[0]  # set to first value of list
    for i in iterable:      # loop to go over the contents of the list
        if i < smallest:    # check if the current value is smaller than the smallest
            smallest = i    # if it is, it becomes the new smallest
    return smallest         # return the smallest item in the array


def maximum(iterable):
    biggest = iterable[0]   
    for i in iterable:      
        if i > biggest:     # this time we need to see if it is bigger
            biggest = i
    return biggest

I came here to post the same question because it does sound like ASCII values are in action here.

Here are some examples:

min("treehouse") #returns e which has a ASCII Value of 101 
min("Treehouse") #returns T  which has a ASCII Value of 84 
min("Treehouse!!!") #returns ! which has a ASCII Value of 33 
max("!!!TreehouseZ~~") #returns ~ which has a ASCII Value of 126 

Upper case letters have lower ASCII values than lower case letters. Numbers have lower ASCII values than Upper case letters. Some special characters have values lower than Numbers and some have values way higher (more than lower case letters).

Yes, I agree. That's how python seems to be evaluating letters. Really, using the Unicode values. Play around with the Unicode table and see how it sorts them. (https://unicode-table.com/en/#02AF)