1 00:00:00,730 --> 00:00:06,490 Hello and welcome back to an underclass of our course but the complete introduction to Python. 2 00:00:06,700 --> 00:00:08,920 We talked about many things until now. 3 00:00:08,970 --> 00:00:15,120 I think we are gonna learn another thing that will be very very useful into your well in Python programming 4 00:00:16,290 --> 00:00:22,830 so what we are gonna talk about today are functions so basically functions are just a simplified way 5 00:00:22,980 --> 00:00:24,940 to write down your code. 6 00:00:25,140 --> 00:00:29,250 So we will start by learning how we can write functions. 7 00:00:29,520 --> 00:00:35,520 So basically functions will always start with a def so when you write down def this means you are starting 8 00:00:35,610 --> 00:00:36,090 a function. 9 00:00:36,150 --> 00:00:38,950 So you are creating your own function. 10 00:00:39,510 --> 00:00:41,820 Then you write down the name of your function. 11 00:00:41,850 --> 00:00:51,180 So let's say for example your function will say you love something so write down you can name your function 12 00:00:52,050 --> 00:00:53,300 let's say love. 13 00:00:53,580 --> 00:00:59,670 We'll say what exactly you love then open up your disease and inside your parenthesis you will write 14 00:00:59,670 --> 00:01:05,560 down the element that will be used in your function. 15 00:01:05,590 --> 00:01:08,520 So in this case we will say name. 16 00:01:10,210 --> 00:01:13,040 So the permit the parameter will be name. 17 00:01:13,410 --> 00:01:17,960 Then when it's all done you write down two points. 18 00:01:18,720 --> 00:01:29,230 After that you write down what you want to put in your function so let's say you want to print the name. 19 00:01:30,160 --> 00:01:30,610 I love 20 00:01:33,680 --> 00:01:36,730 and then you add name. 21 00:01:36,790 --> 00:01:40,910 So in this case your function is very basic it's something that is very basic. 22 00:01:41,080 --> 00:01:47,970 You have the function which is love you have name which will be the parameter and the parameter used 23 00:01:48,520 --> 00:01:51,570 and your functions output will be print. 24 00:01:51,640 --> 00:01:54,250 I love plus your name. 25 00:01:54,280 --> 00:01:55,560 So in this case name. 26 00:01:55,570 --> 00:01:59,650 So if we run our functions so how do we run functions. 27 00:01:59,860 --> 00:02:01,510 You can't just click here. 28 00:02:01,510 --> 00:02:03,750 It's not gonna work to run your function. 29 00:02:03,790 --> 00:02:07,330 You need to write down your functions name so love. 30 00:02:07,390 --> 00:02:09,970 And in this case your barometer is name. 31 00:02:10,120 --> 00:02:11,750 So you simply have to change it. 32 00:02:11,800 --> 00:02:15,130 In this case a name let's say will be pizza. 33 00:02:17,110 --> 00:02:22,810 So since it's a string you write it down like this and then you can run it. 34 00:02:23,290 --> 00:02:26,400 So you run your function as you can see it's written down. 35 00:02:26,410 --> 00:02:29,640 I love pizza. 36 00:02:30,030 --> 00:02:37,050 So what it does it simply will help you only write down the code once and then you can run your function 37 00:02:37,410 --> 00:02:38,820 as many times as you want. 38 00:02:38,820 --> 00:02:41,200 So right here it's something very very simple. 39 00:02:41,370 --> 00:02:43,560 Once again it's only two lines. 40 00:02:43,560 --> 00:02:45,270 Well it's one line of code. 41 00:02:45,300 --> 00:02:50,010 It's the function plus one line of code so what's happening in the function is a write down print. 42 00:02:50,030 --> 00:02:50,750 I love you. 43 00:02:51,840 --> 00:02:52,050 All right. 44 00:02:52,530 --> 00:02:57,210 So I want that discourse is really to show you how you can use functions and do something very very 45 00:02:57,210 --> 00:02:58,530 cool with them. 46 00:02:58,530 --> 00:03:02,780 So what I propose you today is to write down a monologue between two persons. 47 00:03:02,790 --> 00:03:05,830 So you will have one person that is ordering food. 48 00:03:06,030 --> 00:03:12,420 You will have another person who will tell what type of food this person wants and you will have the 49 00:03:12,420 --> 00:03:15,380 person no one who will answer to this person. 50 00:03:15,390 --> 00:03:15,860 Thank you. 51 00:03:16,890 --> 00:03:21,260 So we'll use only functions to create all this so will create three functions. 52 00:03:21,260 --> 00:03:24,300 In this case we'll have Person 1 and Person 2. 53 00:03:24,720 --> 00:03:27,170 So let's start the first thing we want to create. 54 00:03:27,210 --> 00:03:29,130 Right now we'll be the first function. 55 00:03:29,130 --> 00:03:31,530 So the function for the person number one. 56 00:03:31,590 --> 00:03:34,480 So the first thing we are writing down will be death. 57 00:03:34,500 --> 00:03:38,090 So this is how we start the creation of a function. 58 00:03:38,130 --> 00:03:40,910 So right down there then let's say Person 1. 59 00:03:40,910 --> 00:03:45,040 So in this case per one this will be our person. 60 00:03:45,040 --> 00:03:51,210 So in this case we are one and we write down the name because the only parameter that we will need will 61 00:03:51,210 --> 00:03:54,670 be the name we write down two points. 62 00:03:54,690 --> 00:04:00,780 And what exactly the person one will say the person one will only say well the person one will only 63 00:04:00,780 --> 00:04:02,520 say Hello how can I help you. 64 00:04:03,150 --> 00:04:07,260 So it looks something like a small print will have the name of the person. 65 00:04:07,260 --> 00:04:15,600 So in this case name plus two points because once again it's a monologue and you will write down Hello 66 00:04:16,860 --> 00:04:22,470 how can I help you. 67 00:04:22,500 --> 00:04:23,140 All right. 68 00:04:23,280 --> 00:04:24,240 So we have all this. 69 00:04:24,270 --> 00:04:28,310 So let me just run this small line of code and show you what it looks like. 70 00:04:28,320 --> 00:04:37,800 So we have our first one and this case let's say the name will be cashmere because the person is the 71 00:04:37,800 --> 00:04:40,830 cashier to simply write this down 72 00:04:43,960 --> 00:04:44,300 all right. 73 00:04:44,350 --> 00:04:46,460 When it's already we run the up. 74 00:04:46,480 --> 00:04:50,320 So as you can see the cashier to point said Hello how can I help you. 75 00:04:50,320 --> 00:04:52,030 So that's that's them on the look. 76 00:04:52,030 --> 00:04:57,420 So we have our person no one who is saying Hello how can I help you. 77 00:04:57,430 --> 00:04:57,800 All right. 78 00:04:58,360 --> 00:05:01,700 So you can delete this right now what we want to do. 79 00:05:01,740 --> 00:05:03,740 We will create the person number two. 80 00:05:03,760 --> 00:05:11,690 So the person number two will ask will say what this what she wants or he wants to eat. 81 00:05:12,400 --> 00:05:16,420 So the way I want to make it on I make it a bit more complicated. 82 00:05:16,420 --> 00:05:20,070 So you guys can practice and you will be able to. 83 00:05:20,200 --> 00:05:25,370 Well in this case understand a bit more what you can do with the function. 84 00:05:25,390 --> 00:05:27,470 So I decided to make it more complicated. 85 00:05:27,490 --> 00:05:31,870 We are going to use inputs Swansea and it's not that complicated but you'll see it's going to be more 86 00:05:31,870 --> 00:05:32,360 cool. 87 00:05:32,410 --> 00:05:37,810 So in this case we are going to use input since we are going to ask our user to tell us what he wants 88 00:05:37,810 --> 00:05:40,670 to eat so let's write it down. 89 00:05:40,750 --> 00:05:48,040 So first then we are starting our function which will be the death then we write down the person in 90 00:05:48,040 --> 00:05:54,110 this case to and the first line because the person number two takes only once. 91 00:05:54,130 --> 00:05:54,460 All right. 92 00:05:54,730 --> 00:05:57,380 So what exactly this person will say. 93 00:05:57,400 --> 00:06:11,900 So this person will say the food that he or she wants the drink that dessert and her or his name. 94 00:06:11,900 --> 00:06:12,090 All right. 95 00:06:12,100 --> 00:06:15,430 So those are already parameters that will be used in this function. 96 00:06:15,430 --> 00:06:20,370 So we have four parameters and that's what we will use. 97 00:06:20,380 --> 00:06:21,160 All right. 98 00:06:21,190 --> 00:06:27,940 Next thing we start with the our name since we want the our user to tell us his name. 99 00:06:27,940 --> 00:06:30,040 So the user will tell us what you want. 100 00:06:30,070 --> 00:06:31,260 He wants to eat. 101 00:06:31,510 --> 00:06:36,320 In this case we simply write down name equal and we write down input. 102 00:06:36,320 --> 00:06:42,550 So once again the input function as we saw it before it is a way to ask our user to give us a certain 103 00:06:42,550 --> 00:06:42,820 answer. 104 00:06:42,820 --> 00:06:45,850 So we asked him a question and the user give us an answer. 105 00:06:46,090 --> 00:06:51,970 In this case we write down input and we ask our user what is your name. 106 00:06:53,260 --> 00:06:57,720 So very simple the user answers. 107 00:06:58,030 --> 00:07:01,540 And next thing we want to ask the user what food you want to eat. 108 00:07:01,540 --> 00:07:06,970 So in this case we'll call this variable food because you can see I'm storing variables in my functions 109 00:07:08,100 --> 00:07:09,250 and my functions are. 110 00:07:09,730 --> 00:07:14,990 So I have food in this case will ask our user what type of food you want to eat to input. 111 00:07:15,850 --> 00:07:19,900 And we ask our user what do you want. 112 00:07:20,350 --> 00:07:25,220 What do you want to eat. 113 00:07:25,290 --> 00:07:25,540 All right. 114 00:07:26,710 --> 00:07:28,670 So the user will answer us. 115 00:07:28,750 --> 00:07:32,080 Next thing we'll ask the user what type of drinks he wants. 116 00:07:32,100 --> 00:07:35,200 So we'll name this variable drink. 117 00:07:35,200 --> 00:07:40,690 So as you can see drink when dealing with drink with the drink drink right here would be will be linked 118 00:07:40,690 --> 00:07:48,040 with drink right here and we'll ask our user to write down input and we'll ask our user what exactly 119 00:07:48,040 --> 00:07:48,760 he wants to drink 120 00:07:53,860 --> 00:07:54,400 all right. 121 00:07:56,260 --> 00:08:00,370 What do you want to drink. 122 00:08:00,370 --> 00:08:02,480 Great. 123 00:08:02,620 --> 00:08:03,700 Finally the. 124 00:08:04,000 --> 00:08:08,050 Well the next thing we'll ask our user what type of dessert you want. 125 00:08:08,230 --> 00:08:09,930 So dessert. 126 00:08:10,300 --> 00:08:11,740 And once again we'll do the same thing. 127 00:08:11,860 --> 00:08:12,220 Input 128 00:08:16,030 --> 00:08:17,980 what dessert 129 00:08:23,350 --> 00:08:33,800 do you want. 130 00:08:34,090 --> 00:08:37,110 So right now the person will answer those questions. 131 00:08:37,120 --> 00:08:37,950 What is your name. 132 00:08:37,960 --> 00:08:40,670 What you want to eat what you want to drink. 133 00:08:41,230 --> 00:08:42,910 And finally what desert do you want. 134 00:08:43,480 --> 00:08:45,910 So those are the questions we are asking our user. 135 00:08:45,910 --> 00:08:49,360 The last thing that we want to do in our function is print something. 136 00:08:49,360 --> 00:08:51,820 So what it will be the output of this function. 137 00:08:51,820 --> 00:08:56,440 So when we have all those answers to all this questions. 138 00:08:56,860 --> 00:09:01,420 So right now we'll write down the outputs in this case we'll write down the monologue of this person. 139 00:09:01,480 --> 00:09:03,620 So once again we will start with name. 140 00:09:03,620 --> 00:09:10,330 So since we need the name of the person we'll write down plus and open up the small commas up there 141 00:09:10,990 --> 00:09:14,760 two points since we want to one the name of the person. 142 00:09:14,770 --> 00:09:22,820 Then two points and the person will say I would like the food that the person wants. 143 00:09:22,850 --> 00:09:35,050 So plus in this case food then we continue our sentence. 144 00:09:35,230 --> 00:09:36,220 I want to drink. 145 00:09:36,370 --> 00:09:38,830 So in this case I want to drink 146 00:09:41,520 --> 00:09:43,240 what the person what I drink. 147 00:09:43,240 --> 00:09:47,950 It's the person wants to drink the variable drink next plus 148 00:09:50,890 --> 00:09:54,420 I want. 149 00:09:54,460 --> 00:09:58,420 So in this case the last variable that we will use will be desert. 150 00:09:58,420 --> 00:10:02,370 I want desert the variable desert as a desert. 151 00:10:02,470 --> 00:10:09,890 So we simply open up the parenthesis right here right down as a desert desert. 152 00:10:09,980 --> 00:10:10,340 Here we go. 153 00:10:11,560 --> 00:10:16,280 So what we have now we have our output of our function right here. 154 00:10:16,300 --> 00:10:22,720 What's also with that when you have inputs right there you don't need to write down the well the elements 155 00:10:22,720 --> 00:10:23,400 that are here. 156 00:10:23,410 --> 00:10:27,610 So for example in this case we would need to write down debt. 157 00:10:27,730 --> 00:10:30,350 So let me just show you what it will look like. 158 00:10:30,390 --> 00:10:36,760 So when we print this function so when we print the the function right here we'll need to name our element. 159 00:10:36,790 --> 00:10:45,370 So in this case sorry I don't but one person one and we'll need to print this element right here. 160 00:10:45,370 --> 00:10:47,370 So we'll need to give a name to the name. 161 00:10:47,380 --> 00:10:56,320 So in this case the name can be Jack but in the case of the function right here since we are asking 162 00:10:56,380 --> 00:11:00,440 our user to give us those parameters we don't need to name them. 163 00:11:00,490 --> 00:11:06,400 And even if we name them well what's gonna happen is that python will use the inputs instead of using 164 00:11:06,610 --> 00:11:11,680 our parameters that we wrote where our elements. 165 00:11:11,710 --> 00:11:12,060 All right. 166 00:11:12,160 --> 00:11:15,860 So until now we have our two first monologues. 167 00:11:16,030 --> 00:11:17,620 The last one will be very easy. 168 00:11:17,620 --> 00:11:21,630 So once again it's going to be again the person number two. 169 00:11:21,640 --> 00:11:23,840 So in this case the person number ones are. 170 00:11:24,220 --> 00:11:30,190 In this case we'll write down deaf person number one and the second monologue. 171 00:11:30,190 --> 00:11:31,590 So person number 1 2. 172 00:11:32,900 --> 00:11:40,330 So in this case we just need the name of the person to write down name and the person will say print. 173 00:11:42,760 --> 00:11:48,730 So the name of the person because we want to know who is talking plus we open up our small comments 174 00:11:48,990 --> 00:11:49,990 to points. 175 00:11:49,990 --> 00:11:50,380 Thank you 176 00:11:53,760 --> 00:11:54,550 that's it. 177 00:11:54,630 --> 00:11:54,870 All right. 178 00:11:54,930 --> 00:11:57,350 So right now we have all our functions. 179 00:11:57,510 --> 00:12:03,120 We can cause them so we have created our functions. 180 00:12:03,140 --> 00:12:06,080 The first thing that we want to do is very simple 181 00:12:09,800 --> 00:12:15,950 right now want to write down our code so in this case what we want to do. 182 00:12:15,950 --> 00:12:18,080 As I said we want to write it down. 183 00:12:18,290 --> 00:12:19,900 So how exactly we write it down. 184 00:12:19,960 --> 00:12:23,350 I will simply write down our first person. 185 00:12:23,350 --> 00:12:28,520 So in this case purse two points 1. 186 00:12:29,120 --> 00:12:32,160 So this will be our first person who will talk. 187 00:12:32,170 --> 00:12:39,110 So in this case first one let's say the name of the first one is they said the cashier then 188 00:12:44,310 --> 00:12:46,180 here we go. 189 00:12:49,850 --> 00:12:54,070 Then the second person who will talk will be the person number two. 190 00:12:55,330 --> 00:13:00,160 So in this case person to 1. 191 00:13:00,680 --> 00:13:06,110 And in this case we don't need to name all those variables we can say instead of we can just write the 192 00:13:06,110 --> 00:13:06,860 exact same thing. 193 00:13:07,300 --> 00:13:17,080 So we can just copy and paste this or just write down food then we'll write down the drink 194 00:13:21,720 --> 00:13:27,240 we'll write down dessert in this case dessert 195 00:13:31,740 --> 00:13:32,360 and name. 196 00:13:32,640 --> 00:13:35,240 So those are the four variables that were the four. 197 00:13:35,420 --> 00:13:38,280 Well the four elements that are in our function 198 00:13:44,530 --> 00:13:49,760 and finally we have the person number one and two. 199 00:13:51,550 --> 00:13:54,400 So in this case it's once again the cashier. 200 00:13:54,430 --> 00:13:56,070 Here we have to name our variable. 201 00:13:56,080 --> 00:13:59,190 So yeah sheer all right. 202 00:13:59,820 --> 00:14:02,190 So right now we have everything that we need. 203 00:14:02,190 --> 00:14:09,390 And the only thing that is left to do will be to run our function so we have our cashier who says hello 204 00:14:09,420 --> 00:14:10,170 how can I help you. 205 00:14:10,200 --> 00:14:17,780 So you answer what is your name you say in this case my name is let's say John. 206 00:14:18,060 --> 00:14:19,110 What do you want to eat. 207 00:14:19,110 --> 00:14:21,180 I want to eat pizza. 208 00:14:21,660 --> 00:14:24,210 I want to drink soda. 209 00:14:24,210 --> 00:14:27,990 And what dessert I want I want cake. 210 00:14:28,110 --> 00:14:29,980 So as you can see here we have John. 211 00:14:30,030 --> 00:14:30,900 I would like pizza. 212 00:14:31,050 --> 00:14:32,200 I want to drink soda. 213 00:14:32,220 --> 00:14:33,660 I want a cake as a dessert. 214 00:14:33,660 --> 00:14:35,400 And the cashier says thank you. 215 00:14:35,520 --> 00:14:38,770 As you can see we have our modern monologue that is created. 216 00:14:38,850 --> 00:14:42,010 And as you can see it's well it's very very simple. 217 00:14:42,030 --> 00:14:44,940 We created our functions right here. 218 00:14:44,940 --> 00:14:45,570 All right. 219 00:14:45,570 --> 00:14:52,950 Right now if we change the order so for example if we put the person number to one right here. 220 00:14:53,100 --> 00:14:55,360 Well the person number two starts the monologue. 221 00:14:55,410 --> 00:15:00,150 We will have the person number two that will say we'll have this function at first. 222 00:15:00,150 --> 00:15:01,540 So we'll have our function right here. 223 00:15:01,620 --> 00:15:06,240 If for example we instead take it like this and write it and put it there. 224 00:15:06,240 --> 00:15:13,110 So this would not make sense because in our monologue this is the second thing that the person needs 225 00:15:13,110 --> 00:15:13,410 to say. 226 00:15:13,440 --> 00:15:13,980 So we have. 227 00:15:14,000 --> 00:15:17,110 This is the first thing this is the second thing this is the third thing. 228 00:15:17,160 --> 00:15:19,670 So in our monologue it will not work. 229 00:15:19,760 --> 00:15:24,180 But let's say for example you want to work with let's say you have your functions and you need to start 230 00:15:24,180 --> 00:15:24,960 with this function. 231 00:15:24,960 --> 00:15:29,040 Then this function then this function or this at first and this and this. 232 00:15:29,400 --> 00:15:30,820 Well in this case you can do it. 233 00:15:30,890 --> 00:15:32,160 You can simply change the word. 234 00:15:32,190 --> 00:15:35,500 So the way you will change your order let's say want to start with this one right here. 235 00:15:35,960 --> 00:15:36,700 All right done. 236 00:15:37,330 --> 00:15:43,230 So person number one so the function number three as you can see we start with this once once again 237 00:15:43,230 --> 00:15:50,790 fear we'll say cashier and then we can go to the person number one and then well to the function number 238 00:15:50,790 --> 00:15:57,090 one and the come back the function number two so you decide of the order that you want to use your functions. 239 00:15:57,090 --> 00:16:03,120 So as I explained functions are simply a way for you guys to store not just for information but to store 240 00:16:03,150 --> 00:16:07,890 let's say a certain calculation or whatever you want to store in it what's cool also with functions 241 00:16:07,890 --> 00:16:09,180 that you can store anything you want. 242 00:16:09,300 --> 00:16:11,330 You can have variables in them. 243 00:16:11,400 --> 00:16:12,260 You can have strings. 244 00:16:12,300 --> 00:16:13,260 You can have boolean. 245 00:16:13,260 --> 00:16:18,610 You can have numbers and that you can use them for absolutely everything. 246 00:16:18,630 --> 00:16:23,460 So let's say you want to write down a line of code that you know that you will reuse instead of writing 247 00:16:23,460 --> 00:16:27,530 down a huge code right here and copy it and passed it everywhere. 248 00:16:27,540 --> 00:16:32,320 You can just write down the function and reuse simply this function. 249 00:16:32,340 --> 00:16:38,030 So right now you understand what is the concept of functions and how exactly they can help you. 250 00:16:38,220 --> 00:16:42,610 We'll see later in the course some more cool stuff that we can do with functions. 251 00:16:42,690 --> 00:16:49,410 But until now you know that they exist and they're are just a good way to store information and calculations 252 00:16:49,470 --> 00:16:50,180 in them. 253 00:16:50,190 --> 00:16:51,230 That's it for this class guys. 254 00:16:51,270 --> 00:16:53,100 And see open our next class.