Discover the fundamentals of functions and conditionals in this beginner-friendly programming lesson. This video explains how functions help organize code and how conditional statements control decision-making in programs.
Perfect for beginners, this course breaks down key concepts with clear explanations and practical examples to help you build a strong programming foundation.
š¹ What youāll learn:
What functions are and why they are important How to create and use functions Understanding conditionals (if, else, else if) Writing simple decision-based logic
00:00Earlier in the course we were looking for a mean or an average function that Python
00:11might have to allow us to make the average of the items of the numbers of a list.
00:18And we found out that Python has some more fundamental functions such as len to get the
00:23number of items of a list and sum to make the sum of the numbers of a list but it doesn't
00:29have a mean function.
00:31So we ended up doing this.
00:33So we had this list, we calculated the sum using the sum function, calculated the length
00:38so the number of items using the len function and then did the division between sum and length
00:44to get the mean.
00:45However, the fact that Python doesn't have a mean function doesn't mean we cannot create
00:52our own mean function.
00:54So what we're going to do now is we're going to create our own mean function.
00:59To create functions in Python you use the dev keyword and then pick a name for your function.
01:06The name has to follow the same naming rules as with variables.
01:10So it cannot start with a number, it cannot start with a math operator.
01:14So let's say min, then you use surround parenthesis and inside those and the first line ends with
01:22a colon and inside the parenthesis optionally you can input some parameters of your function.
01:29Parameters, in other words, is the input that your function will get and that will process
01:36that input and that will produce an output.
01:38The input in our case would be a list.
01:41However, here you have to give a name following again the same rules as with variables.
01:47Let's say my list.
01:49Okay.
01:50Then you want to press enter there and you see that automatically Visual Studio Code, so the
01:57editor indented the next line with four spaces.
02:01One, two, three, four, so that's what the syntax requires that after the first line.
02:08So after you have defined what name you want for your function and what parameters you want,
02:14you have to indent the next lines.
02:17So either your editor will do it automatically, but if it doesn't do it, one, two, three, four,
02:24just indent four spaces in there, you can also make it with one space or just one space would
02:31also work, but it's recommended to use four spaces.
02:35Everyone uses four spaces because it's more visible, the indented code, so it will make
02:40your code more readable.
02:42And then you can start to calculate the mean.
02:45Let's say the mean, I'm going to call it like that so I don't confuse it with the function
02:52name. That would be the sum of my list divided by the length of my list.
03:04And next, what we want to do, we want to use the return keyword, and that is followed by
03:11what you want to return.
03:13Well, we want to return the mean.
03:14Now, save that and let me open a terminal.
03:19I want to execute this script now.
03:22So let me show you what will happen.
03:25Nothing will actually happen.
03:28Nothing happens because what Python does is it just reads this definition.
03:33This is like a blueprint.
03:35You have declared what your function should do, but you didn't tell Python to execute your
03:42function.
03:43You want to call the function, pass an input value for that parameter that you defined.
03:55And also you want to print out print, open parentheses there, close parentheses there, save, don't forget,
04:05and execute.
04:06And that's the mean of those three numbers.
04:12So like that, I can easily try this out with many input values.
04:17Just like that, I don't have to rewrite anything in here.
04:22That's the beauty of a function.
04:24So you can see the similarity here.
04:28Just like we use the sum function, we are using the mean function in the same way.
04:35So sum gets this input value, mean gets this input value.
04:40This happens to be a variable, this happens to be a direct value, but it doesn't matter.
04:45If you check the type of mean, type mean and type of sum, what you're going to see is that mean is a function.
04:59So that was printed out.
05:01This was printed out by this.
05:04So mean is a function and sum is, again, it's a function, but it's a built-in function.
05:11So it's built-in in the Python interpreter.
05:14It was designed by the Python core development team.
05:18And this function was designed by me, the programmer.
05:22So that is how you create and call a function in Python.
05:31Something to point out is that when your function doesn't have a return statement, save, execute,
05:40it implicitly returns a none object.
05:45None is a specific object in Python, which means nothing.
05:50It's not an integer, it's not a float, it's not a string, it's just none.
05:54It means nothing.
05:55So Python, what it does is, if it doesn't find the return statement, on the background,
06:00it does this, return none.
06:03So if you execute that, oops, an indentation error.
06:08That's a good thing.
06:09So you see that all this block here has to be the same level of indentation.
06:15If you indent it four lines here, you have to indent four lines in here too.
06:19So indentation has to be consistent.
06:23That looks right.
06:26So we got the same output, none here and none there.
06:32Some people do this mistake.
06:35Instead of returning, they use print.
06:39Instead of using return, they use a print function, the mean, which works half the way.
06:49So it still returns the value.
06:52And it also returns none.
06:55So what's going on here is that when you call the function here, Python will execute
07:00all these lines.
07:02So it calculates the mean, then it goes to the next line.
07:05So line by line, it will print the mean.
07:09So it will print all the mean in here, and then it will look for a return statement.
07:15It doesn't find it, so it will return none.
07:18The problem with this is that if you try to do some other operations with your
07:25mean value, let's say my mean is equal to the mean function 0, 3, and 4, okay.
07:39Now I want to print out my mean plus 10.
07:44So I would expect that this would give me the value of 7 is a sum divided by 3.
07:51So 2 point something plus 10, 12 point something.
07:55But let's see what this will give me.
07:59So this will give me a type error instead.
08:03It tried to add the none type, which is none.
08:07So this is a none type, that's the type of this value.
08:11It's trying to add that to 10.
08:14So my mean is none because it returns none.
08:17And the interpreter will try to add up that to 10.
08:21That's not possible.
08:22So that's why you shouldn't use print.
08:26If I replace that with return instead, remove the parentheses, save, execute, that will give
08:33me 12.3.
08:36That is the average plus 10.
08:38However, this doesn't mean you cannot use print in your functions.
08:42You can actually do that.
08:45Let's say you just want to print some information, start it, function started and execute that.
08:55So you get the output of print in here and then Python returns the min.
09:04So you get the min, you store it in here, the output of your function, but the value of
09:09the function is always what you return in here.
09:14So if you see that.
09:19So always use return in your functions, unless you have a very special reason to do so.
09:30We defined this function in the previous lectures, which is able to calculate the min of a list.
09:37However, what happens if instead of passing a list as input, we pass a dictionary.
09:47So student grades is list dictionary here.
09:50Let's see what's going to happen.
09:53Well, it says an unsupported operant type.
09:59So an error will occur with the sum function because it will try to add up strings and integers.
10:06And this function is not designed to process dictionaries.
10:11It's designed to process lists.
10:13So how can we make this function to process both lists and dictionaries?
10:18We can do that using conditionals.
10:21Let's see how to do that in the next videos.
10:24This function returns the min value of a list, but we want to make it more intelligent.
10:35We want the function to return the min even if the input value is a dictionary.
10:41So when the input value is a list, it will going to apply a specific algorithm and return the specific value,
10:48which is the min of a list when it's a dictionary.
10:50It's going to check if it's a dictionary and apply the corresponding algorithm that calculates the min value of a dictionary.
10:58So we know that some Monday temperatures divided by length of Monday temperatures gives you the min of the list.
11:10Similarly, student grades, which is a dictionary, its values, the sum of its values divided by the length of student grades dictionary will give you the min.
11:25So we want our function to check and make a decision.
11:29This is a parameter.
11:32Let me change it to value.
11:34value is a more meaningful name since what we are processing will not only be a list, but it can also be a dictionary.
11:46And then under the function, everything has to be indented.
11:51You can see the whole body of the function is indented.
11:55So the conditional block that I'm going to write here is also going to be indented.
12:01It starts with if.
12:03If you remember, we use a type function earlier in the course, which returns
12:11the type of an object.
12:13For example, this is a dictionary, an empty one, but it's still a dictionary.
12:17So what you can do here is you can say if type value double assignment operator, which means equal, it's a comparison operator.
12:28It checks equality.
12:30If that is a column, enter, and it has to be indented with four spaces.
12:38So you see, it's another level of indentation.
12:41It's one here for if, and this is for a function.
12:46So here, the mean will be equal to sum of value dot values divided by the length of value.
13:02So this is a dictionary, dict dot values, and length of the dictionary.
13:08I have to close the sum function call there like that, and enter if you are done with the if block.
13:21So if that's all what you want to check, then you say else.
13:28And we have this code, which has to be indented under else as well, and that's it.
13:37Let me try this.
13:40So with student grade, the dictionary as input, we got this value here.
13:49If I used the list Monday temperatures, it's going to give me the other value, which is the mean of the Monday temperatures.
14:07So that's an if and else conditional, but let me explain it line by line in the next video.
Be the first to comment