**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Start a free Courses 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.

In this video, we're gonna discuss some more Python functions that are common to 0:00 all sequences, len, min, and max. 0:03 These three functions are built-in Python functions that all receive one argument, 0:06 an iterable. 0:10 Len, for length, will return the length of the passed sequence. 0:11 Min will return the smallest element in the sequence, and 0:14 max will return the largest. 0:17 Min and max works for all sequences, even strings, but 0:19 only if every element in the sequence is of the same type. 0:22 If you try to determine the max element in a sequence that contains both strings and 0:25 integers, you'll get an error. 0:29 We'll start with len. 0:31 Len is a very useful function you'll probably use quite often. 0:32 I'm gonna copy and paste in a simple sequence, 0:35 a tuple where the elements are the numbers 1 through 10. 0:37 To get the length of nums, that is the number of elements inside nums, 0:42 the code is len(nums). 0:48 Great, it returned 10, just like we knew already. 0:51 To obtain the max or the largest element in the sequence, we use the max function. 0:55 Also 10, it's the largest integer in the sequence. 1:03 And then, min. 1:06 It returns 1, the smallest integer in the sequence. 1:10 Now, these functions don't just work with lists and tuples. 1:13 They're common to all Python sequences. 1:16 Let's take a look at how they work with strings. 1:18 How many characters are in our string? 1:26 The len function will tell us. 1:28 Looks like there are 9. 1:33 Max and min for strings works by finding the characters closest to the end and 1:35 the beginning of the alphabet, respectively. 1:39 In this example, max returns u because it's closest to z, 1:46 or the end of the alphabet. 1:49 What do you think min will return? 1:51 If you guessed e, you'd be right. 1:53 But what happens if you have a string that also contains numbers, 2:00 like this, for example? 2:03 The 2019 in this string is not an integer, it's part of the string. 2:13 But it's also probably not one you think of when you think of the alphabet. 2:17 So how does the comparison work? 2:20 Well, when comparing strings in all types of sequences, 2:22 Python uses something called lexicographical ordering. 2:25 See the teacher's notes for more information on this. 2:28 But in this example, it's relevant to know that Python considers 2:30 strings that are numbers to be smaller than strings that are letters. 2:33 The max of this string will still be u, but the min will be different. 2:37 As you can see, min returned the string 0. 2:51 All right, now that you've tackled len, min and 2:54 max, join me in the next video to learn about membership testing. 2:56 Which is just a fancy term for finding out whether a sequence contains a given item. 3:00

You need to sign up for Treehouse in order to download course files.

Sign up