1 00:00:11,850 --> 00:00:12,660 Hi, everybody. 2 00:00:13,530 --> 00:00:20,160 All right, so now that we are past theri level basics regarding methods and we have seen in the previous 3 00:00:20,160 --> 00:00:28,440 video how to create simple methods and also how to create methods that receive variables from the execution 4 00:00:28,440 --> 00:00:36,990 level, I think it's time to get over to the mid level theory and see some more cool tricks that methods 5 00:00:36,990 --> 00:00:39,340 can apply on. 6 00:00:40,110 --> 00:00:46,410 Also, you can see here behaviors, some of them, those are especially only in Python as Python is 7 00:00:46,410 --> 00:00:48,000 a very flexible language. 8 00:00:48,000 --> 00:00:54,040 And it can see really, really cool tricks here that other programming languages cannot do. 9 00:00:54,990 --> 00:00:55,380 All right. 10 00:00:55,650 --> 00:01:00,800 So let's start off with the first one and let's just go over it. 11 00:01:00,810 --> 00:01:01,130 All right. 12 00:01:01,770 --> 00:01:08,970 So we have seen that we can pass variables into methods, but what if we're not sure that each time 13 00:01:08,970 --> 00:01:13,250 that a certain method would be called out would be executed? 14 00:01:13,740 --> 00:01:18,190 We're not sure that each time we will have a variable to pass through it. 15 00:01:18,660 --> 00:01:26,640 OK, so in this case, we'll have simply an error because like, if a certain method should receive 16 00:01:26,640 --> 00:01:35,370 a variable and we can see it in the declaration level with the line that contains the def keyword and 17 00:01:35,370 --> 00:01:41,720 it should get a variable BOBERT from the module level, we did not pass a variable. 18 00:01:42,030 --> 00:01:44,700 So this will cause all program to to fail. 19 00:01:45,540 --> 00:01:48,150 But for that we have a solution in Python. 20 00:01:48,450 --> 00:01:48,830 All right. 21 00:01:49,170 --> 00:01:56,220 So in this case that we are not sure if certain variable variable will be passed to a method. 22 00:01:56,550 --> 00:01:58,010 You can see the example right here. 23 00:01:58,980 --> 00:02:00,360 We can do the following thing. 24 00:02:00,370 --> 00:02:08,620 We can set a default value to the method, meaning right here we can see that we have created my function. 25 00:02:08,790 --> 00:02:09,210 All right. 26 00:02:09,240 --> 00:02:10,110 This is the method. 27 00:02:10,860 --> 00:02:17,570 And inside the round brackets, we have set a value country equals Canada. 28 00:02:18,030 --> 00:02:18,540 All right. 29 00:02:19,530 --> 00:02:21,150 So this is the declaration. 30 00:02:21,480 --> 00:02:29,940 And from here, from the middle part here, we're going to execute the execute the the method and let's 31 00:02:29,940 --> 00:02:30,660 see what happens. 32 00:02:30,930 --> 00:02:38,480 So if we take the method and execute it for for the first one right here, we're seeing my function. 33 00:02:38,850 --> 00:02:41,060 And then inside the brackets we see Eataly. 34 00:02:41,490 --> 00:02:45,110 So the printout inside the method would be, I'm from Italy. 35 00:02:45,270 --> 00:02:45,960 That's easy. 36 00:02:46,660 --> 00:02:52,860 But from the green one right here, we can see that we haven't passed any variable to this method at 37 00:02:52,860 --> 00:02:53,160 all. 38 00:02:53,220 --> 00:02:53,550 All right. 39 00:02:53,560 --> 00:02:54,320 So it's empty. 40 00:02:54,330 --> 00:02:59,070 We have some kind of problem here because the print here is waiting for a variable. 41 00:02:59,070 --> 00:03:01,860 And we did not pass any variable to this method. 42 00:03:03,030 --> 00:03:08,760 But we have overcame this of failure with the following solution. 43 00:03:08,940 --> 00:03:15,870 We have set a default value for this method right here in the in the declaration line. 44 00:03:16,170 --> 00:03:23,010 So you can see contre equals Canada, meaning that once we will not have any value set up here in the 45 00:03:23,010 --> 00:03:28,480 execution level, the different value, the default value that would be printed out here would be Canada. 46 00:03:29,250 --> 00:03:37,050 So this is how we can now overcome an exception or an error in the side, our code execution by setting 47 00:03:37,050 --> 00:03:38,760 up a default value. 48 00:03:41,420 --> 00:03:48,560 All right, let's go straight ahead to the next topic that we're going to learn regarding methods, 49 00:03:49,490 --> 00:03:50,990 and it goes as follows. 50 00:03:51,020 --> 00:03:58,070 OK, so we already familiar that methods can receive variables, all collections into them in the execution 51 00:03:58,070 --> 00:03:58,310 line. 52 00:03:58,610 --> 00:04:06,710 But a method can also return a value back to the module level, meaning that if we would like to pass 53 00:04:07,580 --> 00:04:14,780 some variable out of the method into the module level, we can achieve it. 54 00:04:14,810 --> 00:04:20,960 OK, so here we have again, we have a declaration of a module. 55 00:04:21,230 --> 00:04:28,580 OK, my function and you can see here the new keyword that we're not familiar with up until this point 56 00:04:28,580 --> 00:04:32,360 return can see it marked in pink. 57 00:04:32,720 --> 00:04:41,150 I saw the return key word basically returns, whatever comes after it into the execution level that 58 00:04:41,150 --> 00:04:44,340 we can see right here with a pink and recombined. 59 00:04:44,720 --> 00:04:54,050 OK, so first of all, we execute the we execute the method by stating its name right here, my function, 60 00:04:54,290 --> 00:04:57,110 and we're passing the variable three into it. 61 00:04:57,300 --> 00:05:06,170 OK, so from here it goes up to the top and X equals three, meaning it goes inside and inside we can 62 00:05:06,170 --> 00:05:10,780 see a return five equals X, right. 63 00:05:10,820 --> 00:05:13,750 And in this case, X is equals three. 64 00:05:13,910 --> 00:05:19,640 So basically a return will be would be returned the value of 15 in our case. 65 00:05:20,360 --> 00:05:28,880 So what am I doing here is once I executed the method right here, I put an equal sign to the left and 66 00:05:28,880 --> 00:05:30,010 to the left some more. 67 00:05:30,020 --> 00:05:38,390 I've created a new variable called number, and as this method right here returns a value. 68 00:05:38,960 --> 00:05:47,660 OK, so this number variable would get this value that was extracted out of the method to the module 69 00:05:47,660 --> 00:05:51,110 level right here outside of the method. 70 00:05:51,410 --> 00:05:56,350 This is the module level and here with the intention is the method level. 71 00:05:56,510 --> 00:06:02,240 So right here in the module level, we would get the value that was returned from inside the method 72 00:06:02,240 --> 00:06:06,140 to out here and then we can do whatever we want with it. 73 00:06:06,680 --> 00:06:14,910 In our case, the value that was set inside the method is three three three multiplied by five is 15. 74 00:06:15,140 --> 00:06:20,780 So once we print this number variable, we would get the output 15, correct. 75 00:06:22,100 --> 00:06:26,030 I would like to do the following thing, which is going to copy all of this code. 76 00:06:26,330 --> 00:06:26,810 OK. 77 00:06:28,370 --> 00:06:33,440 And show it to you live on by charm, I think is the best way to understand things like that. 78 00:06:35,450 --> 00:06:35,960 All right. 79 00:06:37,460 --> 00:06:40,470 OK, so here's the method and here's the return. 80 00:06:40,500 --> 00:06:45,520 So basically from here, the three goes up in here to the X, OK? 81 00:06:46,280 --> 00:06:49,760 And then this X goes inside the method right here. 82 00:06:49,970 --> 00:06:52,550 And we have the return, as you can see. 83 00:06:52,550 --> 00:06:58,250 Also, the return is is with orange color, which is a special keyword. 84 00:06:58,520 --> 00:07:03,500 And then after the return, we have five multiplied by three in our case. 85 00:07:03,860 --> 00:07:10,680 OK, and then the number of variable right here gets whatever comes out of this method. 86 00:07:10,700 --> 00:07:12,700 This is like we have a return from here. 87 00:07:12,920 --> 00:07:16,380 So everything that is returned from here goes straight to this number. 88 00:07:16,880 --> 00:07:25,680 So as you can see, when we run it, we get the output 15 in once we delete it. 89 00:07:25,700 --> 00:07:27,130 For example, let's put it in. 90 00:07:27,130 --> 00:07:32,570 The comments on this line does not exist and let's put base here so we won't have any error. 91 00:07:32,930 --> 00:07:35,720 And now once we will run it, we will get nothing. 92 00:07:35,750 --> 00:07:36,080 All right. 93 00:07:36,080 --> 00:07:43,310 We have none because number was never set up with anything, because here we didn't have any return 94 00:07:43,460 --> 00:07:47,040 action made out from inside of the method. 95 00:07:47,070 --> 00:07:48,190 So we have none here. 96 00:07:48,440 --> 00:07:56,180 But when we have this value, the value that comes after the return is being returned to the execution 97 00:07:56,180 --> 00:08:00,710 level and we can straight set up a variable and print it out. 98 00:08:00,980 --> 00:08:03,010 And we also can do the following thing. 99 00:08:03,020 --> 00:08:11,180 We can just take this thing, which equals 15 and also put this one inside here inside the print, because 100 00:08:11,210 --> 00:08:16,280 this whole term equals to 15 once we print it. 101 00:08:16,940 --> 00:08:18,300 OK, so there you go. 102 00:08:18,530 --> 00:08:20,510 So we don't even need the number available. 103 00:08:20,510 --> 00:08:22,400 Just easier to understand with. 104 00:08:22,400 --> 00:08:22,800 No. 105 00:08:23,840 --> 00:08:28,740 So, OK, in this case, the number gets the value for whatever comes from here. 106 00:08:29,450 --> 00:08:29,840 All right. 107 00:08:31,160 --> 00:08:35,030 And for the first example right here, I also want to show it. 108 00:08:35,280 --> 00:08:39,960 Let me just copy it to them, to the picture and let's look at it together. 109 00:08:40,370 --> 00:08:44,240 OK, so right here we have this thing. 110 00:08:44,240 --> 00:08:47,360 I'm just going to delete the output, OK? 111 00:08:47,660 --> 00:08:55,430 And just going to leave the EU one with a variable passed to the method and one without with an empty, 112 00:08:55,850 --> 00:08:56,500 empty one. 113 00:08:56,510 --> 00:08:56,840 All right. 114 00:08:57,170 --> 00:09:04,340 And once we execute it, so you see it runs perfectly because in the second case, once we did not pass 115 00:09:04,340 --> 00:09:05,660 that variable to the method. 116 00:09:05,990 --> 00:09:12,770 OK, so the default value was executed and this country equals Canada was set up in here and we have 117 00:09:12,770 --> 00:09:14,260 it inside the printout. 118 00:09:14,690 --> 00:09:17,030 What if we will remove this one? 119 00:09:18,140 --> 00:09:18,580 All right. 120 00:09:18,590 --> 00:09:20,350 Just just leave country here. 121 00:09:20,690 --> 00:09:22,460 You see that right here. 122 00:09:22,460 --> 00:09:26,510 If we hover above above it, parameter country on field. 123 00:09:26,640 --> 00:09:29,570 OK, so we have a certain problem here. 124 00:09:30,260 --> 00:09:32,480 And you see we have an exception. 125 00:09:32,480 --> 00:09:38,690 It's a fail, by the way, just reminding that we are going to learn all of these type errors and exceptions 126 00:09:38,690 --> 00:09:39,830 in a later stage. 127 00:09:40,130 --> 00:09:48,200 But for now, we're just seeing that we have a problem here regarding this execution right here, because 128 00:09:48,530 --> 00:09:52,760 at certain value is expected of the method to be inserted in here. 129 00:09:53,450 --> 00:09:54,560 But nothing comes in. 130 00:09:54,830 --> 00:09:59,700 And then we have a problem because the the code does not know what to run here. 131 00:10:01,010 --> 00:10:06,050 So if we have the default value, we do not have any issues with this thing. 132 00:10:06,110 --> 00:10:06,490 All right. 133 00:10:07,220 --> 00:10:08,120 That's about it. 134 00:10:08,120 --> 00:10:16,250 Regarding Python mid-level lecture, regarding methods, I think we're done with this one. 135 00:10:16,460 --> 00:10:19,400 I'll see you in the next lecture where where we discuss some. 136 00:10:19,430 --> 00:10:24,070 Furthermore, regarding methods, this will be the advanced lecture. 137 00:10:24,080 --> 00:10:25,220 Thank you and bye bye.