1 00:00:00,180 --> 00:00:03,580 In this video, we're gonna discuss some more Python functions that are common to 2 00:00:03,580 --> 00:00:06,850 all sequences, len, min, and max. 3 00:00:06,850 --> 00:00:10,208 These three functions are built-in Python functions that all receive one argument, 4 00:00:10,208 --> 00:00:11,440 an iterable. 5 00:00:11,440 --> 00:00:14,590 Len, for length, will return the length of the passed sequence. 6 00:00:14,590 --> 00:00:17,250 Min will return the smallest element in the sequence, and 7 00:00:17,250 --> 00:00:19,290 max will return the largest. 8 00:00:19,290 --> 00:00:22,120 Min and max works for all sequences, even strings, but 9 00:00:22,120 --> 00:00:25,290 only if every element in the sequence is of the same type. 10 00:00:25,290 --> 00:00:29,140 If you try to determine the max element in a sequence that contains both strings and 11 00:00:29,140 --> 00:00:31,500 integers, you'll get an error. 12 00:00:31,500 --> 00:00:32,790 We'll start with len. 13 00:00:32,790 --> 00:00:35,510 Len is a very useful function you'll probably use quite often. 14 00:00:35,510 --> 00:00:37,834 I'm gonna copy and paste in a simple sequence, 15 00:00:37,834 --> 00:00:40,750 a tuple where the elements are the numbers 1 through 10. 16 00:00:42,760 --> 00:00:48,813 To get the length of nums, that is the number of elements inside nums, 17 00:00:48,813 --> 00:00:51,078 the code is len(nums). 18 00:00:51,078 --> 00:00:55,300 Great, it returned 10, just like we knew already. 19 00:00:55,300 --> 00:00:59,696 To obtain the max or the largest element in the sequence, we use the max function. 20 00:01:03,120 --> 00:01:06,290 Also 10, it's the largest integer in the sequence. 21 00:01:06,290 --> 00:01:06,882 And then, min. 22 00:01:10,115 --> 00:01:13,220 It returns 1, the smallest integer in the sequence. 23 00:01:13,220 --> 00:01:16,080 Now, these functions don't just work with lists and tuples. 24 00:01:16,080 --> 00:01:18,230 They're common to all Python sequences. 25 00:01:18,230 --> 00:01:20,266 Let's take a look at how they work with strings. 26 00:01:26,573 --> 00:01:28,200 How many characters are in our string? 27 00:01:28,200 --> 00:01:29,609 The len function will tell us. 28 00:01:33,752 --> 00:01:35,180 Looks like there are 9. 29 00:01:35,180 --> 00:01:39,090 Max and min for strings works by finding the characters closest to the end and 30 00:01:39,090 --> 00:01:41,612 the beginning of the alphabet, respectively. 31 00:01:46,488 --> 00:01:49,970 In this example, max returns u because it's closest to z, 32 00:01:49,970 --> 00:01:51,581 or the end of the alphabet. 33 00:01:51,581 --> 00:01:53,940 What do you think min will return? 34 00:01:53,940 --> 00:01:55,588 If you guessed e, you'd be right. 35 00:02:00,723 --> 00:02:03,968 But what happens if you have a string that also contains numbers, 36 00:02:03,968 --> 00:02:05,271 like this, for example? 37 00:02:13,675 --> 00:02:17,380 The 2019 in this string is not an integer, it's part of the string. 38 00:02:17,380 --> 00:02:20,850 But it's also probably not one you think of when you think of the alphabet. 39 00:02:20,850 --> 00:02:22,690 So how does the comparison work? 40 00:02:22,690 --> 00:02:25,590 Well, when comparing strings in all types of sequences, 41 00:02:25,590 --> 00:02:28,320 Python uses something called lexicographical ordering. 42 00:02:28,320 --> 00:02:30,620 See the teacher's notes for more information on this. 43 00:02:30,620 --> 00:02:33,400 But in this example, it's relevant to know that Python considers 44 00:02:33,400 --> 00:02:36,620 strings that are numbers to be smaller than strings that are letters. 45 00:02:37,680 --> 00:02:40,941 The max of this string will still be u, but the min will be different. 46 00:02:51,579 --> 00:02:54,420 As you can see, min returned the string 0. 47 00:02:54,420 --> 00:02:56,950 All right, now that you've tackled len, min and 48 00:02:56,950 --> 00:03:00,280 max, join me in the next video to learn about membership testing. 49 00:03:00,280 --> 00:03:04,160 Which is just a fancy term for finding out whether a sequence contains a given item.