PYTHON FUNCTIONS (Beginner's Guide to Python Lesson 6)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video I'm going to show you how to use functions in Python. Okay so let's load up our atom editor with our project that we had from our last lesson. We're going to create a new file here and let's call it functions.py okay so this is our new blank functions file now a function in Python is just a way of defining a block of code that you can repeat elsewhere in the... in your source code. The difference between a function and a while loop or something like that is that a function you can pass in various variables to customize the variables in the code that you execute so I'm going to demonstrate how you do this and then you'll understand how it works and how they can be useful in your code. Ok so let's pretend we're writing a piece of code for a store and let's stick with the same theme that we've had in the previous two videos we're going to be selling apples in our store and let's say we want to write a simple script that greets the customer when they come in so we want to greet them by their name and we want to tell them how many apples we have in stock. Ok so let's start by just writing these lines out individually so let's print "Hello, Mark" and under that print "We have 6 apples in stock". Ok so I'm gonna save that file and let's copy and paste these two lines and paste them two more so we have this 3 kind of blocks a code here and let's change the name so let's say we have Brooke and then we have Greg coming to the store. Alright so save the file and then load up the terminal or command prompt window and let's run our function script by typing python functions.py. Okay so you can see that it outputs on the screen the name of each person and how many apples we have in stock. Alright so you can see that this code is a bit repetitive we've repeated the same thing three times we literally had to copy and paste and all we've done is changed the text in here and there's a lot of repeating text that we've entered more times than we need to. So for example "hello" here this is the same for all of them. And "we have" and "apples in stock"... well this actual string is the same for all of them but let's pretend that this may change so we're basically...You can see that it's repeating the code here. We're also repeating the print functions and there's a lot of repetitiveness going on. Good source code has limited repetitiveness. There's a concept called DRY which is do not repeat yourself and that means don't repeat unnecessary blocks of code in your application because then if we ever wanted to change this text we would have to change it in all of these three places as opposed to just changing it in one. So I'm going to show you how you can use functions to reduce code duplication. Alright so before we actually reduce the code duplication I'm just going to show you how to make a simple function in Python. Alright so the way you define a function in Python is you use the DEF - it's short for define and then the name of the function that you want to define. So let's create a function called greet mark so I'm going to define def and I'm going to create a function called greet_mark and each function that you define needs to end in two brackets I'll explain why a bit later in this video. Alright so you write def, the name of the function, open and close brackets and then a colon. If you hit enter after the codon you can see that Atom automatically indents this here because as with the while loops that we learnt in the previous lesson the function code is grouped by the indentation so any code we want to include in this function needs to be indented by one tab underneath this function. Okay so that's our first function let's create one for each of these blocks so I'm gonna copy and paste this here, copy and paste this here then I change this to Brooke and I'm going to change this to Greg and then I'm gonna indent these two here, save that. The best practice is to use two spaces in between these functions when you're doing it in a single Python file like this. So now we've defined our function if we go to our command prompt and we run the code you actually see that it doesn't output anything now and that's because defining the function just isn't enough you actually need to call the function after you defined it. So underneath these functions we're going to call the function so make sure that we're indented back to the base level of the file and the way you call the function is you simply provide the function name and the brackets without the def because we've already defined it. We just want to call it now so if I write greet mark and then again I'm gonna copy and paste that I'm gonna put greet Brooke greet Greg and then save the file and if you load up command prompt push up and do Python functions you can see their outputs exactly the same as before and it may seem backwards because we've actually created more lines of code instead of reducing the lines of code which was the goal but I'm just demonstrating how you define functions now I'm going to demonstrate how you can use functions to save...reduce...or sorry to reduce the lines of code in the application. Okay so one of the things that you can do with functions is you can pass in arguments to the function, so you can define various arguments that you can provide when you call the function and then the function can use those arguments in the source code. Alright so if we delete these two here because they're pretty much the same, and we can also delete the calls to them here as well and let's rename our function instead of greet mark which is very specific and doesn't help with... it doesn't help with reducing duplication let's just call the function greet customer. And then in between the brackets here we're going to define a variable and we're going to call or it's actually an argument, we're gonna define an argument for our function and we're going to call the argument name. Okay so if we call the argument name and then we hit save and now when we call our function....actually before we do that we're going to change this print here and instead of printing mark which we've hard-coded here we're going to update this to include our variable. So print hello and we'll leave the string there before the end of the print the last bracket in the print use the plus symbol and this will concatenate our name variable with this string. So what this is now doing is its greeting a customer and it's allowing us to provide the name of the customer and it will include that name in the code that it prints out. Okay so below this let's write again greet_customer and then we need to provide the name of the customer we want to greet. So you define the variables when you call the function, you define the variables in the same order that you put them in the greet customer arguments and since we only have one we can just type it here. We'll just put the name mark and then we'll copy and paste this and we'll call it Brooke...and we'll call it Greg. Save that. Now load up the command prompt and let's run our file again and now you can see it outputs the same again except this time we have a lot a lot less lines of code than we had before. Okay so let's say we wanted to update this function and we want to actually be able to change the number of apples that we have in stock because if Mark comes into the store then after he's left the store there may be less apples in there when Brooke comes in. Okay so you can actually define multiple arguments for a function here and you do that just by comma separating the list of argument names in the function argument bracket. So let's just call a new argument called num_apples just short for number of apples and then similar to what we did here where we added the value of this to our string I'm going to delete this and then add the quotes here so we now have two strings and do +. And we actually need to convert this number type into a string so that we can add it to our string because you can't just add numbers into strings using the plus symbols so we'll just type STR and then num_apples so what this STR does is it converts this whatever variable this is into the string type so it basically converts our number into a string so instead of it being the number six it will just be a string saying six. Okay so now we need to add this argument to the call when we call our function so let's add... let's start with seven and then let's say there's six and then let's say when Greg comes in there's two left. Save that and then go back to our command prompt, hit up and run. Now you can see that it actually changes the number of apples that outputs based on the arguments that we provide. Okay so there's one more thing I'm going to show you about these function arguments and that's that you can actually set a default for the function arguments. Okay so a default means you can provide a default value for this so you don't even need to provide it here. So let's just say there's a default of seven apples, we always start with seven. The first time you call it you can actually remove that and you can run it again and you see it still says seven we changed the default to ten. Save that and run it. Now you can see it says ten. Now the only thing to remember about setting defaults is you can only set defaults on the last or multiple last items in a function argument list. So another way of putting it is you cannot have a non default function after you...or sorry a none default argument after you defined an argument with a default. So we wouldn't be able to provide name equals mark as a default and then put a non default argument here. If you try that it will give you an error telling you a non-default argument follows a default argument. So basically once you've defined your first default argument you need to either move it to the end of the list like this... oops copy that you even need to move it to the end of the list or you need to set a default for the items in front and then you would need to reorder when you call the function reorder the...you would need to put the number of apples first in this case because we've changed it around. Okay so let's just move that back just so we have a finished working version. And yeah you can set a default for any type of argument as long as you don't put any non default ones after the default. Okay so that's how you create functions in Python. Thank you so much for watching. If you have any questions at all please leave them in the comments below and I'll try and answer them or someone else might answer them for you. So thank you very much for watching and I'll see you in the next video.
Info
Channel: London App Developer
Views: 32,587
Rating: undefined out of 5
Keywords: Python, python programming, python for beginners, learn how to code, programming, software development, programming tutorial, functions in python, functions in software
Id: _ypAw_pCOt8
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Wed Nov 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.