1 00:00:07,000 --> 00:00:09,680 - Welcome to Lesson Four, Functions. 2 00:00:09,680 --> 00:00:12,350 Up to this point we've used a few built-in functions 3 00:00:12,350 --> 00:00:14,240 from the Python Standard Library, 4 00:00:14,240 --> 00:00:15,750 and here we're going to start talking 5 00:00:15,750 --> 00:00:17,820 about all the different syntax issues 6 00:00:17,820 --> 00:00:20,870 for Creating your own custom functions in Python, 7 00:00:20,870 --> 00:00:22,900 which of course is a key aspect 8 00:00:22,900 --> 00:00:24,520 of Procedural Programming, 9 00:00:24,520 --> 00:00:26,880 and then separately when we eventually do get 10 00:00:26,880 --> 00:00:30,050 to Object Oriented Programming in Part Three, 11 00:00:30,050 --> 00:00:33,010 we'll create functions in the context of classes, 12 00:00:33,010 --> 00:00:34,850 which we would then called Methods, 13 00:00:34,850 --> 00:00:38,600 and the same syntax issues apply there as well. 14 00:00:38,600 --> 00:00:40,520 Now another key aspect of this lesson 15 00:00:40,520 --> 00:00:41,940 is that we're going to start 16 00:00:41,940 --> 00:00:45,550 to Import capabilities from other aspects 17 00:00:45,550 --> 00:00:47,320 of the Python Standard Library. 18 00:00:47,320 --> 00:00:49,420 So we'll learn the Import Statement 19 00:00:49,420 --> 00:00:50,350 for the first time, 20 00:00:50,350 --> 00:00:51,760 and that's going to be similar 21 00:00:51,760 --> 00:00:54,610 to what you do in other C-based languages. 22 00:00:54,610 --> 00:00:57,240 So for example, in C and C++, 23 00:00:57,240 --> 00:00:59,650 we use the Pound Include Directive, 24 00:00:59,650 --> 00:01:01,890 in Java we use the Import Keyword, 25 00:01:01,890 --> 00:01:04,120 and in C sharp we use Using 26 00:01:04,120 --> 00:01:06,130 in the same context. 27 00:01:06,130 --> 00:01:07,900 We'll also demonstrate the concepts 28 00:01:07,900 --> 00:01:10,710 of Passing data between functions in Python, 29 00:01:10,710 --> 00:01:13,730 and because everything is an object in Python, 30 00:01:13,730 --> 00:01:16,260 all you ever pass into a function 31 00:01:16,260 --> 00:01:18,380 is a copy of a reference. 32 00:01:18,380 --> 00:01:19,320 So in effect, 33 00:01:19,320 --> 00:01:21,930 every object in Python is passed 34 00:01:21,930 --> 00:01:24,190 into a function by reference. 35 00:01:24,190 --> 00:01:25,960 And we'll talk about that and demonstrate 36 00:01:25,960 --> 00:01:28,020 that to you as well. 37 00:01:28,020 --> 00:01:31,480 We'll also step into some Simulation techniques 38 00:01:31,480 --> 00:01:32,840 as part of this lesson, 39 00:01:32,840 --> 00:01:35,240 introducing Random number generation 40 00:01:35,240 --> 00:01:36,530 for the first time. 41 00:01:36,530 --> 00:01:39,070 We'll talk as part of that discussion 42 00:01:39,070 --> 00:01:41,510 about Tuples for the first time. 43 00:01:41,510 --> 00:01:44,320 Tuples are another key data structure in Python 44 00:01:44,320 --> 00:01:46,330 for aggregating data, 45 00:01:46,330 --> 00:01:48,890 and one of the key uses of a tuple 46 00:01:48,890 --> 00:01:50,310 is to be able to return 47 00:01:50,310 --> 00:01:52,520 more than one value at a time 48 00:01:52,520 --> 00:01:53,810 from a function. 49 00:01:53,810 --> 00:01:55,550 So by definition, 50 00:01:55,550 --> 00:01:58,490 functions in Python return one value at a time, 51 00:01:58,490 --> 00:02:00,780 but that one value could be an object 52 00:02:00,780 --> 00:02:03,430 and that object could contain multiple things, 53 00:02:03,430 --> 00:02:06,400 and that's what tuples are commonly used for. 54 00:02:06,400 --> 00:02:08,850 Amongst other things in Python. 55 00:02:08,850 --> 00:02:10,530 We'll talk about how to create tuples, 56 00:02:10,530 --> 00:02:12,250 which is called Packing a tuple. 57 00:02:12,250 --> 00:02:15,030 We'll also talk about how to extract the data 58 00:02:15,030 --> 00:02:15,863 in a tuple, 59 00:02:15,863 --> 00:02:17,910 which is called Unpacking the tuple. 60 00:02:17,910 --> 00:02:19,640 And we'll take a look at Scope 61 00:02:19,640 --> 00:02:22,130 in the context of Python functions, 62 00:02:22,130 --> 00:02:24,320 which is actually somewhat different 63 00:02:24,320 --> 00:02:25,800 from how Scope works 64 00:02:25,800 --> 00:02:28,420 in some of the other programming languages 65 00:02:28,420 --> 00:02:30,720 that you're probably familiar with. 66 00:02:30,720 --> 00:02:31,820 Some of the other topics 67 00:02:31,820 --> 00:02:33,500 we'll look at in this lesson, 68 00:02:33,500 --> 00:02:36,420 we'll show you how to do Default parameter values, 69 00:02:36,420 --> 00:02:39,490 which are frequently used in Python. 70 00:02:39,490 --> 00:02:41,120 We'll also take a look at what are known 71 00:02:41,120 --> 00:02:42,640 as Keyword arguments, 72 00:02:42,640 --> 00:02:46,240 which enable you to call a Python function or method 73 00:02:46,240 --> 00:02:48,660 with the parameters named, 74 00:02:48,660 --> 00:02:51,310 and that also means that you can change the order 75 00:02:51,310 --> 00:02:52,440 of the parameters 76 00:02:52,440 --> 00:02:55,240 and the function will still get called correctly 77 00:02:55,240 --> 00:02:59,060 if you have named each of the arguments. 78 00:02:59,060 --> 00:03:00,400 We'll show you how to define 79 00:03:00,400 --> 00:03:02,780 a Variable-length argument list, 80 00:03:02,780 --> 00:03:03,850 and we'll finish up 81 00:03:03,850 --> 00:03:06,040 with a discussion of Methods 82 00:03:06,040 --> 00:03:08,930 by demonstrating how to call functions 83 00:03:08,930 --> 00:03:11,690 on objects of the string class. 84 00:03:11,690 --> 00:03:13,900 So as I've mentioned previously 85 00:03:13,900 --> 00:03:15,200 in the introduction, 86 00:03:15,200 --> 00:03:17,890 it's very commonly the case in Python 87 00:03:17,890 --> 00:03:20,330 that you will create objects of existing types 88 00:03:20,330 --> 00:03:22,780 and simply interact with those objects, 89 00:03:22,780 --> 00:03:23,890 and the primary way 90 00:03:23,890 --> 00:03:26,893 that you'll do that is by calling their methods.