Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Get important information about your Python sequences with functions `len`, `min`, and `max`.
Lexicographical ordering is very similar to alphabetical ordering, but it considers additional characters besides the letters of the alphabet.
Some of the basic rules in Python are that:
1) Uppercase letters come earlier than lowercase letters. This means that A < Z < a < z.
2) Numbers come earlier than letters. This means that 0 < 9 < A < a.
3) Space characters come before all printable characters.

0:00
In this video, we're gonna discuss some more Python functions that are common to

0:03
all sequences, len, min, and max.

0:06
These three functions are builtin Python functions that all receive one argument,

0:10
an iterable.

0:11
Len, for length, will return the length of the passed sequence.

0:14
Min will return the smallest element in the sequence, and

0:17
max will return the largest.

0:19
Min and max works for all sequences, even strings, but

0:22
only if every element in the sequence is of the same type.

0:25
If you try to determine the max element in a sequence that contains both strings and

0:29
integers, you'll get an error.

0:31
We'll start with len.

0:32
Len is a very useful function you'll probably use quite often.

0:35
I'm gonna copy and paste in a simple sequence,

0:37
a tuple where the elements are the numbers 1 through 10.

0:42
To get the length of nums, that is the number of elements inside nums,

0:48
the code is len(nums).

0:51
Great, it returned 10, just like we knew already.

0:55
To obtain the max or the largest element in the sequence, we use the max function.

1:03
Also 10, it's the largest integer in the sequence.

1:06
And then, min.

1:10
It returns 1, the smallest integer in the sequence.

1:13
Now, these functions don't just work with lists and tuples.

1:16
They're common to all Python sequences.

1:18
Let's take a look at how they work with strings.

1:26
How many characters are in our string?

1:28
The len function will tell us.

1:33
Looks like there are 9.

1:35
Max and min for strings works by finding the characters closest to the end and

1:39
the beginning of the alphabet, respectively.

1:46
In this example, max returns u because it's closest to z,

1:49
or the end of the alphabet.

1:51
What do you think min will return?

1:53
If you guessed e, you'd be right.

2:00
But what happens if you have a string that also contains numbers,

2:03
like this, for example?

2:13
The 2019 in this string is not an integer, it's part of the string.

2:17
But it's also probably not one you think of when you think of the alphabet.

2:20
So how does the comparison work?

2:22
Well, when comparing strings in all types of sequences,

2:25
Python uses something called lexicographical ordering.

2:28
See the teacher's notes for more information on this.

2:30
But in this example, it's relevant to know that Python considers

2:33
strings that are numbers to be smaller than strings that are letters.

2:37
The max of this string will still be u, but the min will be different.

2:51
As you can see, min returned the string 0.

2:54
All right, now that you've tackled len, min and

2:56
max, join me in the next video to learn about membership testing.

3:00
Which is just a fancy term for finding out whether a sequence contains a given item.
You need to sign up for Treehouse in order to download course files.
Sign up