1 00:00:01,350 --> 00:00:03,600 - [Narrator] In this video we're going to continue 2 00:00:03,600 --> 00:00:06,350 our discussion of custom function definitions, 3 00:00:06,350 --> 00:00:09,870 this time with a function that has multiple parameters. 4 00:00:09,870 --> 00:00:12,770 Now to save a little typing time here 5 00:00:12,770 --> 00:00:16,600 I'm going to go ahead and paste in the function definition. 6 00:00:16,600 --> 00:00:20,410 We're going to define a function here called maximum 7 00:00:20,410 --> 00:00:23,020 which receives three parameters 8 00:00:23,020 --> 00:00:27,620 because it wants to figure out the largest of three values. 9 00:00:27,620 --> 00:00:30,430 So as you can see I started with the def keyword 10 00:00:30,430 --> 00:00:33,740 followed by the name of my function, a parameter list, 11 00:00:33,740 --> 00:00:36,950 this time with a comma separated list of names 12 00:00:36,950 --> 00:00:41,950 which become the parameters and the colon then introduces 13 00:00:41,960 --> 00:00:45,780 the body of the function which again is called a block. 14 00:00:45,780 --> 00:00:49,250 Now the first thing inside of any function definition 15 00:00:49,250 --> 00:00:53,020 should be a docstring so we've provided one here 16 00:00:53,020 --> 00:00:55,230 which is saying that this function 17 00:00:55,230 --> 00:00:57,920 will return the maximum of three values 18 00:00:57,920 --> 00:01:01,780 and as we did back in the control statements chapter 19 00:01:01,780 --> 00:01:05,610 we're going to make a little simplifying assumption here 20 00:01:05,610 --> 00:01:10,610 which is to say that value1 is the maximum value so far. 21 00:01:11,380 --> 00:01:14,840 So max_value is assigned value1. 22 00:01:14,840 --> 00:01:17,870 Then we're going to use a couple of if statements 23 00:01:17,870 --> 00:01:21,830 to figure out whether value2 is greater than max_value 24 00:01:21,830 --> 00:01:25,740 and if so assign value2 back to max_value 25 00:01:25,740 --> 00:01:27,840 and we're going to check if value3 26 00:01:27,840 --> 00:01:31,290 is then greater than max_value and only if so 27 00:01:31,290 --> 00:01:33,800 assign value3 to max_value. 28 00:01:33,800 --> 00:01:36,160 Now you'll notice we're violating a little bit 29 00:01:36,160 --> 00:01:38,960 of the Python style guide at the moment 30 00:01:38,960 --> 00:01:42,960 by not putting a blank line above and below 31 00:01:42,960 --> 00:01:45,010 each of these control statements 32 00:01:45,010 --> 00:01:48,160 and there's a reason that we did that. 33 00:01:48,160 --> 00:01:51,500 When you're inputting a function definition like this 34 00:01:51,500 --> 00:01:52,733 directly in IPython, 35 00:01:53,670 --> 00:01:58,670 if you press enter you get a blank line as you would expect, 36 00:01:58,890 --> 00:02:01,770 but if you press enter again saying let's say 37 00:02:01,770 --> 00:02:04,320 you wanted to start a new control statement, 38 00:02:04,320 --> 00:02:07,040 it's going to assume when you press enter twice 39 00:02:07,040 --> 00:02:09,560 that you're done with the definition 40 00:02:09,560 --> 00:02:12,460 of whatever it is you were just creating 41 00:02:12,460 --> 00:02:16,270 and it's going to then evaluate the snippet of code. 42 00:02:16,270 --> 00:02:21,080 So if we were to have gone ahead up here and pressed enter 43 00:02:21,080 --> 00:02:23,970 to drop to the line that currently starts with if 44 00:02:23,970 --> 00:02:26,500 and then pressed enter again so that we could start 45 00:02:26,500 --> 00:02:29,550 the if statement one line further down, 46 00:02:29,550 --> 00:02:32,790 we actually would not have been able to complete 47 00:02:32,790 --> 00:02:36,170 the whole function definition in that case. 48 00:02:36,170 --> 00:02:39,230 So that's just something to keep in mind 49 00:02:39,230 --> 00:02:41,530 when you are working in the context 50 00:02:41,530 --> 00:02:44,220 of the interactive interpreter. 51 00:02:44,220 --> 00:02:46,970 Now once we get past the two if statements 52 00:02:46,970 --> 00:02:50,540 max_value contains the largest value so far 53 00:02:50,540 --> 00:02:52,680 so we're going to use the return statement 54 00:02:52,680 --> 00:02:55,670 at that point to give back the largest value 55 00:02:55,670 --> 00:02:58,710 to whoever calls this function. 56 00:02:58,710 --> 00:03:01,010 So let's go ahead and execute that 57 00:03:01,010 --> 00:03:03,010 which defines the actual function. 58 00:03:03,010 --> 00:03:05,070 When you execute a function definition 59 00:03:05,070 --> 00:03:07,160 it creates the function in memory. 60 00:03:07,160 --> 00:03:11,450 The function itself by the way is actually an object 61 00:03:11,450 --> 00:03:15,430 and the docstring happens to be a piece of data 62 00:03:15,430 --> 00:03:19,270 within the object that represents that function 63 00:03:19,270 --> 00:03:21,050 and when you use the help system 64 00:03:21,050 --> 00:03:22,990 it goes and looks at that object 65 00:03:22,990 --> 00:03:26,560 and accesses the docstring piece of data 66 00:03:26,560 --> 00:03:30,163 to get the string that you defined as the docstring. 67 00:03:31,290 --> 00:03:34,280 So let's go ahead and call this function that we defined. 68 00:03:34,280 --> 00:03:36,970 So we named it maximum so we'll start with that. 69 00:03:36,970 --> 00:03:39,500 Remember when you call a function you need parentheses 70 00:03:39,500 --> 00:03:42,370 and typically are going to need arguments 71 00:03:42,370 --> 00:03:45,100 so usually when you're trying to figure out 72 00:03:45,100 --> 00:03:47,260 the largest of three values, 73 00:03:47,260 --> 00:03:50,610 it's typically going to be three items of the same type 74 00:03:50,610 --> 00:03:53,340 so let's use three integers here 75 00:03:53,340 --> 00:03:54,850 and you can see that the result 76 00:03:54,850 --> 00:03:57,500 is the largest of those three integers. 77 00:03:57,500 --> 00:04:00,990 Let's try it again with some floating point numbers 78 00:04:00,990 --> 00:04:05,990 so let's do 12.3 and we'll do 45.6 and 9.7 79 00:04:06,570 --> 00:04:10,320 so this time the largest value is in the middle position 80 00:04:10,320 --> 00:04:12,090 rather than the third position, 81 00:04:12,090 --> 00:04:14,320 but we just wanna make sure that indeed 82 00:04:15,160 --> 00:04:18,310 it gives us back the largest value each time. 83 00:04:18,310 --> 00:04:21,750 Now before I continue, one thing that's interesting 84 00:04:21,750 --> 00:04:25,100 about the maximum function and about Python in general 85 00:04:25,100 --> 00:04:28,200 is that again we have not specified 86 00:04:28,200 --> 00:04:33,140 what the types of value1, value2 and value3 are. 87 00:04:33,140 --> 00:04:37,360 Python figures that stuff out dynamically for you. 88 00:04:37,360 --> 00:04:42,350 So as long as you can assign whatever type of value 89 00:04:42,350 --> 00:04:44,670 you're dealing with and as long as you can compare 90 00:04:44,670 --> 00:04:46,260 with the greater than sign 91 00:04:46,260 --> 00:04:48,960 whatever types of values you're dealing with, 92 00:04:48,960 --> 00:04:52,500 those values will work with the maximum function. 93 00:04:52,500 --> 00:04:57,470 So it turns out for example that the maximum function has... 94 00:04:58,580 --> 00:05:00,400 I'm sorry, not the maximum function. 95 00:05:00,400 --> 00:05:05,400 The string type has the greater than operator defined 96 00:05:05,690 --> 00:05:08,680 for comparing strings of information 97 00:05:08,680 --> 00:05:12,880 so for example I can go ahead and take the maximum 98 00:05:12,880 --> 00:05:17,880 of let's say yellow, red and orange. 99 00:05:21,120 --> 00:05:24,740 Now I'm using all lowercase letters on purpose at the moment 100 00:05:24,740 --> 00:05:28,430 because when you start dealing with case sensitivity, 101 00:05:28,430 --> 00:05:31,680 strings are not considered to be the same 102 00:05:32,760 --> 00:05:34,040 so you have to be careful with that 103 00:05:34,040 --> 00:05:36,680 and we'll talk more about that subsequently, 104 00:05:36,680 --> 00:05:39,090 but right now I have these three strings 105 00:05:39,090 --> 00:05:42,360 and if you're thinking about strings alphabetically, 106 00:05:42,360 --> 00:05:46,150 O comes before R and R comes before yellow 107 00:05:46,150 --> 00:05:51,150 so yellow is the largest if you will of the string values 108 00:05:51,790 --> 00:05:55,420 and orange is the smallest of the string values 109 00:05:55,420 --> 00:05:59,620 based on alphabetic order in the alphabet. 110 00:05:59,620 --> 00:06:00,970 So if I go ahead and run that 111 00:06:00,970 --> 00:06:05,220 you can see that indeed it gives me back yellow in this case 112 00:06:05,220 --> 00:06:06,053 and I'll talk more 113 00:06:06,053 --> 00:06:09,780 about the case sensitivity issues in subsequent lessons. 114 00:06:09,780 --> 00:06:12,810 That's beyond the scope of what I'm doing at the moment. 115 00:06:12,810 --> 00:06:14,800 The main thing I wanted to point out here 116 00:06:14,800 --> 00:06:18,700 is that you can compare strings using greater than, 117 00:06:18,700 --> 00:06:21,550 greater than or equal, less than, less than or equal, 118 00:06:21,550 --> 00:06:25,350 double equals and not equals as well. 119 00:06:25,350 --> 00:06:29,160 So that just demonstrates to you 120 00:06:29,160 --> 00:06:33,237 the fact that we can go ahead and compare 121 00:06:34,230 --> 00:06:36,060 lots of different types of values, 122 00:06:36,060 --> 00:06:40,100 provided that they support the greater than operator 123 00:06:40,100 --> 00:06:43,460 that we're using in the context of the max, 124 00:06:43,460 --> 00:06:46,500 maximum, excuse me, function in this case. 125 00:06:46,500 --> 00:06:50,940 Now you can call maximum with values of different types, 126 00:06:50,940 --> 00:06:55,010 provided that those types are compatible with one another 127 00:06:55,010 --> 00:07:00,010 so if I do for example 13.5 minus three and seven, 128 00:07:00,560 --> 00:07:03,730 we've got one floating point number and two integers 129 00:07:03,730 --> 00:07:05,730 and floating point numbers and integers 130 00:07:05,730 --> 00:07:07,580 are compatible with one another 131 00:07:07,580 --> 00:07:12,010 so I can go and figure out that 13.5 is indeed 132 00:07:12,010 --> 00:07:14,120 the largest of those three values. 133 00:07:14,120 --> 00:07:16,160 On the other hand if I were to use 134 00:07:16,160 --> 00:07:19,760 decimal objects and floating point numbers, 135 00:07:19,760 --> 00:07:22,740 that would not work because decimal objects 136 00:07:22,740 --> 00:07:25,650 and floating point numbers are actually not considered 137 00:07:25,650 --> 00:07:28,870 to be compatible with one another. 138 00:07:28,870 --> 00:07:32,730 So you can mix types in certain scenarios 139 00:07:32,730 --> 00:07:35,890 which is also part of the flexibility 140 00:07:35,890 --> 00:07:37,450 of the Python language. 141 00:07:37,450 --> 00:07:41,800 Now once again the capability that I'm showing you here 142 00:07:41,800 --> 00:07:44,980 is something that's actually built into Python 143 00:07:44,980 --> 00:07:48,490 which is why it's so important that you spend time 144 00:07:48,490 --> 00:07:50,670 looking at the online documentation 145 00:07:50,670 --> 00:07:52,940 so that you can learn about the features 146 00:07:52,940 --> 00:07:55,870 that already belong to the language 147 00:07:55,870 --> 00:07:59,470 and to the Python libraries and even do some research 148 00:07:59,470 --> 00:08:02,340 into other open source libraries as well 149 00:08:02,340 --> 00:08:06,670 because chances are in the Python community 150 00:08:06,670 --> 00:08:09,460 someone has developed a library 151 00:08:09,460 --> 00:08:13,660 that does some of the capabilities that you need 152 00:08:13,660 --> 00:08:15,670 or that implements some of the capabilities 153 00:08:15,670 --> 00:08:18,430 that you need in your own applications. 154 00:08:18,430 --> 00:08:21,220 So for example as I mentioned previously 155 00:08:21,220 --> 00:08:23,987 the max function is built in so I can do things 156 00:08:23,987 --> 00:08:28,470 like use the max function to compare some strings here 157 00:08:28,470 --> 00:08:32,980 so let's try yellow, red, orange 158 00:08:32,980 --> 00:08:35,230 and we'll add in a couple more here. 159 00:08:35,230 --> 00:08:39,330 Let's do blue and green just to show you 160 00:08:39,330 --> 00:08:41,350 that it can receive any number of arguments 161 00:08:41,350 --> 00:08:43,300 so we did five in this case. 162 00:08:43,300 --> 00:08:46,660 And yellow is still the largest of those three values 163 00:08:46,660 --> 00:08:48,850 and similarly there's the min function 164 00:08:48,850 --> 00:08:50,760 which takes any number of arguments 165 00:08:50,760 --> 00:08:53,260 so let's do some integers. 166 00:08:53,260 --> 00:08:58,260 Maybe we do nine and 27 and 14 so four integers in this case 167 00:08:59,130 --> 00:09:02,380 and you can see that it happily figures out the minimum 168 00:09:02,380 --> 00:09:05,570 of the values that we supplied. 169 00:09:05,570 --> 00:09:08,750 So again you're going to want to take a look 170 00:09:08,750 --> 00:09:13,300 at that online Python documentation at python.org 171 00:09:13,300 --> 00:09:16,730 to familiarize yourself with all those built in functions 172 00:09:16,730 --> 00:09:19,260 because there's a lot of convenience functions 173 00:09:19,260 --> 00:09:20,980 already available to you 174 00:09:20,980 --> 00:09:22,810 and you're going to want to take a look 175 00:09:22,810 --> 00:09:26,270 at the top level of all those different libraries 176 00:09:26,270 --> 00:09:28,630 that are part of Python so that you can start 177 00:09:28,630 --> 00:09:31,960 getting a sense of the full set of features 178 00:09:31,960 --> 00:09:33,693 that are available to you.