1 00:00:00,000 --> 00:00:04,622 [MUSIC] 2 00:00:04,622 --> 00:00:07,410 We've used some predefined functions throughout this course. 3 00:00:07,410 --> 00:00:09,200 And they've proven to be super handy. 4 00:00:09,200 --> 00:00:13,820 You know functions like print and input, we also used len. 5 00:00:13,820 --> 00:00:15,820 Functions are great, they provide a way for 6 00:00:15,820 --> 00:00:18,700 you to group multiple statements together to use later. 7 00:00:18,700 --> 00:00:21,140 They help you avoid duplicating your code. 8 00:00:21,140 --> 00:00:24,970 Now if you ever find yourself writing the same exact code more than once, 9 00:00:24,970 --> 00:00:27,600 it's a symptom that something is likely wrong. 10 00:00:27,600 --> 00:00:31,170 Now we refer to these symptoms as code smell, like hmm, 11 00:00:31,170 --> 00:00:33,660 something about this code smells. 12 00:00:33,660 --> 00:00:35,790 Here, let me help you write some smelly code and 13 00:00:35,790 --> 00:00:38,560 then we'll clean it up with the power of functions. 14 00:00:38,560 --> 00:00:42,490 Getting someone's attention these days is so hard. 15 00:00:42,490 --> 00:00:46,764 In application development, you like have to bounce an icon or make a noise, 16 00:00:46,764 --> 00:00:48,426 [SOUND] to alert them, right? 17 00:00:48,426 --> 00:00:53,414 So one solution that we could do in our textual environment here is to yell in 18 00:00:53,414 --> 00:00:54,532 all upper case. 19 00:00:54,532 --> 00:00:56,764 And I feel like the more exclamation points, 20 00:00:56,764 --> 00:00:58,710 the more likely they're gonna see it. 21 00:00:58,710 --> 00:01:01,495 Let's do that, let's create some text and 22 00:01:01,495 --> 00:01:05,201 then convert it to an upper cased and exclamatory phrase. 23 00:01:05,201 --> 00:01:10,996 So I'm gonna make a new file actually, so let's go here, we'll go to File, 24 00:01:10,996 --> 00:01:15,737 and then New File, and I'm gonna name it notifications.py. 25 00:01:17,724 --> 00:01:21,774 So the first thing that I would love to shout from the rooftops 26 00:01:21,774 --> 00:01:24,000 is some praise for you. 27 00:01:24,000 --> 00:01:27,420 So, I'll assign it to the variable praise and 28 00:01:27,420 --> 00:01:32,750 well say, "You are doing great". 29 00:01:32,750 --> 00:01:34,681 So, we want to uppercase this, right? 30 00:01:34,681 --> 00:01:38,986 And what we'll do is we'll just reassign it, so 31 00:01:38,986 --> 00:01:42,353 we'll say praise = praise.upper. 32 00:01:42,353 --> 00:01:45,900 And that's because strings are immutable and they can't be changed. 33 00:01:45,900 --> 00:01:47,570 So it returns a brand new string, 34 00:01:47,570 --> 00:01:51,270 which we're just going to take our label off the old one and put it on the new one. 35 00:01:51,270 --> 00:01:54,440 Now I think there should be at least as many exclamation marks 36 00:01:54,440 --> 00:01:59,220 as there are characters in the string, to really grab your attention. 37 00:01:59,220 --> 00:02:03,636 So to get the numbers of characters in the string I can use the len function. 38 00:02:03,636 --> 00:02:07,901 The len function expects an object, so we'll pass in our variable. 39 00:02:07,901 --> 00:02:16,120 And then I'm gonna store that in a new variable called number_of_characters. 40 00:02:16,120 --> 00:02:22,370 And I'm gonna call the len function, I'm gonna pass in our string, here we go. 41 00:02:22,370 --> 00:02:27,100 So we wanna create a new value that is a combination of this praise and 42 00:02:27,100 --> 00:02:28,870 the exclamation points. 43 00:02:28,870 --> 00:02:32,090 So let's create a new variable called result. 44 00:02:33,230 --> 00:02:38,470 And we'll make that praise and we'll concatenate the exclamation point. 45 00:02:38,470 --> 00:02:43,238 And we're gonna rely on order of operations here so we'll say, 46 00:02:43,238 --> 00:02:47,752 the exclamation point times the number_of_characters. 47 00:02:47,752 --> 00:02:50,321 And then finally, we'll print that out. 48 00:02:55,293 --> 00:02:59,900 And we'll run python notifications, cuz it's a new file. 49 00:03:02,120 --> 00:03:04,430 There it is, YOU ARE DOING GREAT. 50 00:03:04,430 --> 00:03:05,106 And you're doing great. 51 00:03:05,106 --> 00:03:08,753 That's is some good looking code, and fine smelling too. 52 00:03:08,753 --> 00:03:12,985 You know what, I also like to remind you that you should remember to ask for 53 00:03:12,985 --> 00:03:14,290 help when you need it. 54 00:03:15,290 --> 00:03:17,330 So can we yell that too, please? 55 00:03:17,330 --> 00:03:19,157 I don't see why not, right? 56 00:03:19,157 --> 00:03:24,233 So let's make a new variable, we'll call that advice and 57 00:03:24,233 --> 00:03:28,179 that is, "Don't forget to ask for help". 58 00:03:28,179 --> 00:03:36,490 And then, well I guess I would just copy this code, right? 59 00:03:36,490 --> 00:03:41,650 Copy this code here, and paste it down here. 60 00:03:41,650 --> 00:03:44,660 Guess I would change, I need to change this praise and make that advice. 61 00:03:46,060 --> 00:03:48,121 And I make this advice. 62 00:03:50,641 --> 00:03:52,057 Cuz it's the length of device, 63 00:03:52,057 --> 00:03:55,200 not the length of praise because that's a different variable. 64 00:03:55,200 --> 00:04:00,097 And then we do this, so basically I just copied and pasted that code and 65 00:04:00,097 --> 00:04:02,177 changed this variable name. 66 00:04:02,177 --> 00:04:08,138 That style of coding is called copy pasta, and it's not a good habit to fall into. 67 00:04:08,138 --> 00:04:12,399 And it's really a bad habit, especially if you don't understand what the code 68 00:04:12,399 --> 00:04:15,460 you've copied [LAUGH] is doing, it happens all the time. 69 00:04:16,930 --> 00:04:19,450 So here we go, lets keep going. 70 00:04:19,450 --> 00:04:22,280 You know what, I've got a another piece of advice for 71 00:04:22,280 --> 00:04:24,362 you that I would love you to yell for me. 72 00:04:24,362 --> 00:04:32,280 In coding, there's an acronym that we call dry or D-R-Y, don't repeat yourself. 73 00:04:32,280 --> 00:04:34,012 Let's yell that. 74 00:04:34,012 --> 00:04:35,805 So, I guess that's some more advice, right? 75 00:04:35,805 --> 00:04:40,452 So let's call that advice2, and 76 00:04:40,452 --> 00:04:46,067 we'll say, "Don't Repeat Yourself. 77 00:04:46,067 --> 00:04:49,560 Keep things DRY". 78 00:04:49,560 --> 00:04:53,272 Okay so, I guess I'll copy and pasta this. 79 00:04:53,272 --> 00:04:56,994 We'll put this here and I'll go ahead and do advice2. 80 00:04:58,914 --> 00:05:03,972 Advice2, the length of advice2, 81 00:05:03,972 --> 00:05:08,056 and then we'll do advice2. 82 00:05:08,056 --> 00:05:11,320 And then let's just make sure that this is working. 83 00:05:11,320 --> 00:05:18,070 Okay, It is, but what if I told you that we had like ten more of these to do? 84 00:05:18,070 --> 00:05:20,182 Would you be excited about that? 85 00:05:20,182 --> 00:05:23,482 You know what, actually after seeing this in action, I'd like to make it so 86 00:05:23,482 --> 00:05:24,731 that there are half as many. 87 00:05:24,731 --> 00:05:27,010 This is [LAUGH], this is ridiculous. 88 00:05:27,010 --> 00:05:28,890 That's way too many exclamation points. 89 00:05:28,890 --> 00:05:32,360 So I wanna make only half of those as there are characters, right. 90 00:05:32,360 --> 00:05:35,720 So half of the amount of characters we should do so. 91 00:05:35,720 --> 00:05:39,150 One thing that I can think of doing is if we look we can make it so 92 00:05:39,150 --> 00:05:40,810 that we use the floor division. 93 00:05:40,810 --> 00:05:43,340 That was that the double division where we get an integer. 94 00:05:43,340 --> 00:05:48,790 So we can, if we divide the number of characters by 2 and keep it an integer. 95 00:05:48,790 --> 00:05:49,355 That should be that, and 96 00:05:49,355 --> 00:05:52,393 I'm gonna put parens around here because I want the order of operations to work 97 00:05:52,393 --> 00:05:52,980 correctly. 98 00:05:52,980 --> 00:05:55,387 So let's go ahead, let's run that. 99 00:05:55,387 --> 00:06:00,034 The first one looks great, but the other two don't seem to be. 100 00:06:00,034 --> 00:06:03,920 Rats, I forgot to update those too. 101 00:06:03,920 --> 00:06:09,510 Because my code is duplicated, it means any change that I need to make 102 00:06:09,510 --> 00:06:13,801 I have to change every single place I copied and pasta-ed this code. 103 00:06:14,940 --> 00:06:17,140 Well, that's really smelly right? 104 00:06:17,140 --> 00:06:20,928 So let's clean up the smell, anybody got some Febreze? 105 00:06:20,928 --> 00:06:24,786 [LAUGH] Totally missed product placement opportunity right there. 106 00:06:24,786 --> 00:06:28,400 [LAUGH] I guess functions are definitely an air freshener for 107 00:06:28,400 --> 00:06:30,910 these type of code smells. 108 00:06:30,910 --> 00:06:33,440 So let's create one, shall we? 109 00:06:33,440 --> 00:06:37,760 So the way that you create a function, let's get up here. 110 00:06:37,760 --> 00:06:42,270 The way that you create a function is with the keyword named def, 111 00:06:42,270 --> 00:06:44,520 which is short for define. 112 00:06:44,520 --> 00:06:48,061 And then you give your function a name, let's name it yell. 113 00:06:48,061 --> 00:06:51,136 That seems like exactly what we're doing, right, so we'll say yell. 114 00:06:51,136 --> 00:06:53,652 And then when you're defining a function, 115 00:06:53,652 --> 00:06:57,640 you define what parameters are expected when it's called. 116 00:06:57,640 --> 00:07:02,020 So in this case, we want to accept the text that should be yelled. 117 00:07:02,020 --> 00:07:04,240 Let's go ahead, and we'll call that text. 118 00:07:04,240 --> 00:07:08,220 And then we add a colon, cuz we're gonna start the body of the function. 119 00:07:08,220 --> 00:07:13,650 And you press Enter, and see how it's indented automatically here? 120 00:07:13,650 --> 00:07:17,460 And now, we are in what is called the function body. 121 00:07:18,460 --> 00:07:22,730 This is the code that is run when the yell function is called. 122 00:07:22,730 --> 00:07:26,409 So we basically want to do exactly what we did in our previous code that we were 123 00:07:26,409 --> 00:07:27,911 copying and pasting, right? 124 00:07:27,911 --> 00:07:32,108 So let's just do that one more time, we're gonna copy this code here. 125 00:07:32,108 --> 00:07:35,880 I'm gonna cut it out, when I come in here I'm gonna paste it. 126 00:07:35,880 --> 00:07:37,260 And if you come in front of these lines and 127 00:07:37,260 --> 00:07:40,550 press Tab, you can see that they tab into our body. 128 00:07:40,550 --> 00:07:44,900 So instead of praise, this is now text. 129 00:07:44,900 --> 00:07:47,847 I'm using Shift and the arrow key to do that highlighting. 130 00:07:50,176 --> 00:07:52,505 You get better at those and you forget to say. 131 00:07:52,505 --> 00:07:55,273 So that's how I'm doing that, so text. 132 00:07:56,855 --> 00:07:58,137 That looks good, and 133 00:07:58,137 --> 00:08:03,206 it's really good style to leave at least one space after your function definition. 134 00:08:03,206 --> 00:08:05,380 So that it's clear that it's a function where it ends. 135 00:08:06,710 --> 00:08:10,550 So now, we can go ahead and call that. 136 00:08:10,550 --> 00:08:13,058 So let's do this, we'll get rid of this. 137 00:08:13,058 --> 00:08:17,577 We won't assign it anymore, we'll say yell("You are doing great"). 138 00:08:19,456 --> 00:08:21,829 And then we don't need this other code too, let's clean this up. 139 00:08:21,829 --> 00:08:25,132 So we'll say yell("Don't forget to ask for help"). 140 00:08:27,963 --> 00:08:31,558 And then finally, let's go call this last one here. 141 00:08:31,558 --> 00:08:34,588 We'll say, yell("Don't Repeat Yourself. 142 00:08:34,588 --> 00:08:36,399 Keep things DRY"), we'll get rid of these. 143 00:08:38,160 --> 00:08:40,541 That looks a lot better, right? 144 00:08:40,541 --> 00:08:42,433 Let's go ahead, let's run it and make sure things are working. 145 00:08:45,025 --> 00:08:48,990 There we go, these lines are even shorter now, great. 146 00:08:48,990 --> 00:08:51,160 So let's review what we've got. 147 00:08:51,160 --> 00:08:57,000 So, we used the def keyword to define a new function named yell. 148 00:08:58,050 --> 00:08:59,950 Yell declares a parameter which is named text. 149 00:09:01,530 --> 00:09:03,470 This is the body of the function. 150 00:09:03,470 --> 00:09:07,090 It's all this code that's indented four spaces by our style definition. 151 00:09:07,090 --> 00:09:11,880 Now, the function body has code that upper cases and concatenates half-ish 152 00:09:11,880 --> 00:09:14,600 the amount of text in the exclamation marks, and then it prints. 153 00:09:16,350 --> 00:09:19,350 We've got a blank line after our function definition, 154 00:09:19,350 --> 00:09:21,590 because we're adhering to good coding style. 155 00:09:21,590 --> 00:09:22,960 It's important, right? 156 00:09:22,960 --> 00:09:25,650 I want you to keep in mind that just defining this function 157 00:09:25,650 --> 00:09:27,210 doesn't run the code. 158 00:09:27,210 --> 00:09:32,220 What it does is it creates a new name of yell that we can call later, 159 00:09:32,220 --> 00:09:34,330 and we can do it multiple times. 160 00:09:34,330 --> 00:09:38,440 So here we're calling the yell method that we just created, and we're passing in 161 00:09:38,440 --> 00:09:42,870 a brand new string that we just created that says "You are doing great". 162 00:09:42,870 --> 00:09:45,080 So that's a brand new string. 163 00:09:45,080 --> 00:09:49,270 And then the values that you push into a function are called arguments. 164 00:09:49,270 --> 00:09:52,400 And since this is the first argument, right, there's only one. 165 00:09:53,410 --> 00:09:57,280 It's the first argument here, and it's called text here. 166 00:09:57,280 --> 00:10:01,790 So basically what this is saying, is text equals you are doing great. 167 00:10:01,790 --> 00:10:03,060 That's what I want you to imagine. 168 00:10:03,060 --> 00:10:04,860 So text equals you are doing great, and 169 00:10:04,860 --> 00:10:07,920 then the rest of the code runs on you are doing great. 170 00:10:09,410 --> 00:10:12,380 It runs through each one of these texts you are doing great, you are doing great. 171 00:10:13,700 --> 00:10:18,050 Prints the result, and then it pops back out to whatever the next line is. 172 00:10:18,050 --> 00:10:20,860 In this case, is another call to the yell function. 173 00:10:20,860 --> 00:10:23,840 And we create a new string that says, don't forget to ask for help. 174 00:10:23,840 --> 00:10:27,380 And imagine again, text equals, don't forget to ask for help. 175 00:10:27,380 --> 00:10:30,030 And then the rest of the function runs, it pops out. 176 00:10:30,030 --> 00:10:34,250 We get to the next one, yell, don't repeat yourself. 177 00:10:34,250 --> 00:10:37,260 So on and so forth, I feel like I'm repeating myself. 178 00:10:37,260 --> 00:10:41,260 Now also if we wanted to change the way that this yell function worked, 179 00:10:41,260 --> 00:10:44,240 we only need to change it in one place. 180 00:10:44,240 --> 00:10:48,430 Like for instance, I think even this too is a little long. 181 00:10:48,430 --> 00:10:50,820 Why don't we do it to a fourth of that? 182 00:10:50,820 --> 00:10:52,020 So if I come up in here, 183 00:10:52,020 --> 00:10:57,370 I can just change this in one place now and all of those functions run. 184 00:10:57,370 --> 00:10:59,290 Functions are pretty handy aren't they? 185 00:11:00,860 --> 00:11:04,920 We've just scratched the surface of how powerful these functions can be. 186 00:11:04,920 --> 00:11:07,860 But, they definitely help you to create nice reusable code. 187 00:11:08,880 --> 00:11:12,530 And try to stay on the lookout for lines of code that are repeated. 188 00:11:12,530 --> 00:11:14,020 Recognize the code smell. 189 00:11:14,020 --> 00:11:16,980 Don't repeat yourself is a great mantra to live by. 190 00:11:16,980 --> 00:11:20,560 It will lead you to write clean, understandable, and maintainable code. 191 00:11:21,660 --> 00:11:25,050 Everyone can agree that repeating what you're saying over and 192 00:11:25,050 --> 00:11:26,510 over is super annoying. 193 00:11:27,530 --> 00:11:30,510 Everyone can agree that repeating what you're saying over and 194 00:11:30,510 --> 00:11:32,630 over is super annoying. 195 00:11:32,630 --> 00:11:35,860 Everyone can agree that repeating what, right. 196 00:11:35,860 --> 00:11:36,710 Keep things DRY everybody.