Alright guys, this is the last video in the subsection for functions and we’re going to be talking about return values from functions. So, again this is not new to some of you, to some of you it is, so if I mean it’s a pretty basic concept basically in the past we’ve created functions that do something in which case each time the function was called it printed something to the screen because inside that function that’s what we’ve done, we’ve told it to print. But, what if we want to return a value from the function and don’t want to print it out and do something else with it? So let’s go ahead and define a function, call it “do_math” and define two variables “(num1, num2)” It’s going to be a very simple function here, and we’re going to “return num1 + num2” Now, if we call “do_math” and pass in “(5, 7)” it’s not going to do anything because we’re not printing anything out, right. So what we could do is actually let’s create a variable called “sum1” or rather “math1” and this variable is going to include the sum of the two numbers that we pass in. So we’re going to call it a second time as well, “math2 = do_math(11, 34)” and we’re going to save it. Now when I run this function, or when I run this script, again nothing happened because we haven’t done anything with it yet. So what we’re going to do now is print out both results in one print statements. So we are going to say “print(“First sum is”, math1 “and the second sum is”, math2)” So we save this and hit the run button you’ll see that the first sun is 12 and the second sum is 45. So let’s go over this again so you guys, I’m not sure if I’ve explained it well enough so I’m going to do that again. We’re defining a function, we’re calling it do math, and we’re passing in number 1 and number 2, so there’s two arguments going into this function. Inside this block of code we are only returning the sum of num1 and num2, so in the case of the first sum it’s going to be 12. So we are returning to whatever part in our code we’re at, so this is going to become 12. So math1 is equal to 12 and then math2 we’re going to call that function again we’re going to pass 11 and 34 in and again it’s going to return to the spot right here with the value that we tell it to return with. So this line of code right here is going to become 45, so math2 equals 45. Then what we do we just print it out and math1 knows that it’s 45 because that’s what the result of that function is, and so this is going to be useful for a lot of reasons later on in programming. So in the next video we’re going to create a calculator using Python and it’s going to be a command-line tool, so we’ll be able to run it and perform different math operations.
Alright guys let’s go ahead and talk about an infinite number of arguments being passed into a function. So what we’re going to do is define a function called “print_people” and here’s where we’re going to pass in the arguments for a function but we don’t know how many were going to get, we may get 3, we may get a 100. So what we’re going to do is start off we’re going to only pass one argument into this function and that’s going to begin with an “asterisk” and this “*” tells this argument that it’s going to be an array of all of the arguments that are passed into the function, so you’re going to see more how this works in a moment. Now with an array we need some way to loop over it and so we’re going to be using a for statement and we’re going to go more into this in the future, but for right now all you need to know is you’re going to write “for person in people:” and you’re going to notice that when i drop down to the new line after putting the colon here it further indented my code which means what I’m writing here is going to be a different block, it’s going to be one level deeper than right here so, and that’s not sure why that’s underlying red maybe because it’s expecting me to type something here. So what I’m going to do is “for person in people:” I’m going to “print(“This person is” , person)” there we go. So that’s our function, we’re done with that. Basically how a for loop works is this is a list or an array as we’re defining right here. This is going to take in all of the values we’re passing to this function and create a list called people. Now you can iterate over the people list by using a for statement, so for person in people and this person can be any you could write this for item in people, but where we are using the variable name person that is going to be available in this block of code as the next person in the list and so you guys are going to see how this works. Again if you’re familiar with other programming languages this is not a new concept, it’s just a slightly different way of doing it Let’s go ahead and call the function “print people” and let’s pass it some names so “(“Nick” , “Dan” , “Jack” , “King” , “Smiley”)” I’m not even sure if this is a real persons name but we’re going to pass it into this function anyway. So what we’re doing is we’re passing in 1, 2, 3, 4, 5 arguments. Now if we knew we were always going to expect five arguments we could accept each individual argument as its own variable, however we don’t so we’re capturing all these, we’re going to create an array out of them, and the array is going to be stored in the variable called people. So we’re going to run this and you’re going to see that for each person it prints out this person is and then the name of the person. So that’s how to include an infinite number, or a flexible, it’s not really infinite eventually if you were going to pass in an infinite amount of parameters you would never be done with your program, you could pass it on to your descendants they would never be done, and so on. This is truly a flexible number of arguments. So, with that being said we’re all done with that. In the next video we’re going to be discussing return values and then we’re going to build a calculator.
Alright guys so let’s talk about keyword arguments. In the last video we found out how we can pass only one parameter into a function if we have the other one set up with the default value. So, this will allow us to pass the first argument only there’s going to be no way to omit this and pass the age, so let’s do this. Let’s run it and it’s going to say my name is 27 and my age unknown. Now there is another keyword in Python called “none” just like this, this would be a Boolean I guess or just a keyword in general, basically this is the equivalent to null in other languages. So we’re going to save this and run this again, and it’s going to say my name is none and my age 27. There’s literally no way to only pass in an argument that is not the first argument unless you use keyword arguments, and that is defining what variable this is supposed to be. So if we do this and hit run we’re going to see that my name is someone, because we didn’t define a name, and my age is 27. Now we can also use this capacity and parameters in different orders. So I can say name “name=Nick” there let’s save that and run it and you’ll see that the order we’re passing it in it should if we weren’t using keyword arguments would say my name is 27, and my age is Nick. So, by using keyword arguments we can specify which value is supposed to go into which variable, when it goes into the function. So that was actually pretty quick. That’s keyword arguments and how to use them. I kind of like the sister concept of default arguments. So in the next video we’re going to be talking about an infinite number of arguments.
Alright guys, in this video we’re going to be talking about arguments a little further, we were going to move onto keyword arguments but before we do that it’s going to make more sense to cover default arguments first. So, let’s go ahead I’ve cleared out my script here I suggest you guys do the same just for repetition. I mean you could remodel your current function to work with this, however repetition is the key to mastering something. So let’s go ahead drop down two lines, define a new function, we’re going to name it “print_something” and it’s going to take two parameters, one is going to be “(name, age)” let’s open that with a “:” and print out one string which we’re going to concatenate with name and age. So “My name is name ” + name + ” and my age is ” + age” Now we’re going to go down here and call this function “print_something(“Nick” , 27)” I’m sure you guys already see a problem with this, so I’ll give you props for that because this is not going to work, that’s exactly why I’m showing you. We’re going to run it amd it’s going to say that it “Can’t convert ‘int’ object to a str implicity” Now there’s a few different resolutions to this, one I’ve already shown you, you can just wrap this in a string. Now you can run it and it will work. Now, this is why I mentioned this earlier because you can definitely do this, however what we’re going to do is instead of concatenating the string we’re going to remove all these extra characters, we’re going to separate things by a comma, going to remove the extra spaces around the text as well because we don’t need them and you’ll see why. Now this will work without converting this integer to a string, and the reason is because when we use commas it just says print out these four things one after another. We don’t need to concatenate different pieces of data, it’s actually printing out a different number of things. So, it’s going to print this out as a string, it’s going to print this out as a string because we’re passing it in as a string. This is going to be printed out as a string and this is going to be printed out as an integer, however when I save it here and run it you won’t know the difference. See it says my name is Nick and my age 27. You don’t notice that this is an int so this is a perfectly acceptable way to do that. Now, what if I only wanted to pass it one argument? What we can do there is we can create default arguments. Actually, let’s pass in “Nick” So, let’s go ahead and change these to accommodate only passing in some of the values. So, what we’re going to do is set a default value and how you do that is inside the parameter list of the function you’re going to assign values to these variables. These are just variables that you’re creating right here, so you can create a variable and assign a default name, and then same with age. You’re going to assign a default age which we’re going to say is “Unknown” Now we’re going to save it. Now when I run this what we have here is my name is Nick and my age is unknown because I’m only passing in this. So what happens is you think well, shouldn’t name equal someone? It would if I didn’t pass anything in so we can do that as well. We’re going to save this and run it and it’s going to say my name is someone and my age is unknown because we’re not passing in anything. However, we’re going to pass in the first argument here and this is going to take priority over this value here. This
basically means this variable is going to be equal to someone, if no value is passed in. Age is going to be equal to unknown, if no value is passed in for it. So, that’s how to use default arguments in functions. In the next video we’re going to be talking about passing in specific parameters by using keyword arguments.
Alright guys, welcome back. In this video we’re going to expand on this function here and we’re actually going to implement arguments into the function. So what this means that this is very static right now, nothing would change this even if we wanted to change it. I could call this function let’s say 10 times and each time it’s going to print out very static strings. So how we can change this to make it more dynamic is by using arguments and the arguments again go in the brackets here at the end of the function name, and what you’re doing is creating a variable so we don’t even need to know the value that’s being passed in we just let’s decide to use two arguments here in this function, and each one is going to be used in its own print statement. So we’re going to name it “str1, and str2” there and this basically we’ve abbreviated string 1 and string 2. It also tells us right now it’s not being used. You’re going to find PyCharm pretty smart. So what we’re going to do is change this line of code to print out string 1, we’re going to use this print statement to print out string 2. Let’s go ahead and save it and run it. We’re going to get an error and the reason is because we’re calling this function without passing anything into it, and it actually tells us right here parameter string 1 unfilled. So it knows that this function is looking for two values here separated by a comma, one of them is string one and one of them is string two, we haven’t passed anything into it so it’s calling this function and immediately the function is freaking out saying I can’t find this information. So what we need to do is pass in two strings here and how we do that is similar to how it looks in here except each one is going to be the value and not the variable name. So, the first one is going to be called “This is argument 1”, “This is the second argument which is also a string.”) Now if we save, go-ahead run, you’re going to see that this is now printing out string 1 as defined right here, and string 2 as defined right here. So, why would we need to do this? Well, let’s say we’re always going to want to print out two things. Let’s go ahead and call the function again, but this time we’re going to pass in different strings. Let’s fall back to the good ole hello world. Now we’re going to save this and what’s gonna happen is it’s going to print out four lines. It’s going to print out “This is argument 1”, “This is the second argument which is also a string.” being called from the first call of that function, and then “Stringy” , “Hello World” from the second call that function and so that’s how to use arguments. Now in the next video we’re going to be discussing keyword arguments and what those are.
Alright guys, so let’s get into functions. Now every programming language basically has functions, I mean they’re going to be different depending on which programming language we’re discussing, even COBOL has functions but they’re radically different because that’s just an entirely different language. However, basically if you have any experience with PHP, JavaScript, functions are going to look a lot, very well very similar. So, we are now past the need to use an interpreter and what we’re going to be doing from here on out is using our IDE that we have installed. Again I want you guys to follow along maybe even do some extra repetitions of this just to get used to it. So, the first lesson here in the subsection of functions is going to be building a function, so pretty simple, we’re going to build a function, we’re going to call it, and we’re going to run the script. So let’s go ahead and drop down a couple lines, and also we’re gonna go over some pep guidelines throughout this course. We’re probably going to have a module specifically for pep, but as I go along and I see or think of anything that’s addressed in the pep guidelines, I’ll let you know. So basically what pep is is Python’s style guide, so it’s like how to write certain things and certain you know we always drop down two lines between any texts or at the top of the script. So, we would have our imports up there, and then drop down two lines, and then start coding. So we drop down two lines we’re gonna instead of typing function we’re gonna “def” and what this is telling Python is that it’s going to define a function. So we write def and then the name of the function. Now regarding naming functions there’s a specific way to do it according to the pep guidelines. This isn’t new, this specific instance, it’s using snake case for function names. So if you know camel case that’s basically like this that’s that looks like, the first letter of each word is capitalized, that’s used in Python for class names but it’s not used for functions. When you define a function in Python you’re encouraged to use snake case which is separating words with underscores, so that’s what we’re going to do. So I’m going to create a function here called my function. So basically so far we’ve got “def” which says we’re about to define a function, we’ve got the name of our function, and then we’ve got brackets right here and these brackets are going to be used for parameters which we’re going to be going over in one of the next videos, they’re also called called arguments. Now after we do this here’s the point when in most programming languages you’d open your curly braces and start to code, but instead with Python where we don’t use those curly braces we just put a colon here. Now this is going to be smart, I mean Python’s smart because it’s going to automatically determine when this function is complete based on indentation, so based on that you can probably assume how we’re supposed to use this, but we can’t enter you’ll notice that it automatically indents four spaces in. This is because anything four spaces in from the left hand side as of right now is going to be the top level of this function and then you can further intense stuff, and we’re gonna explore that as needed as we go on. So this is going to be just a very basic function that prints something out, so let’s go ahead and print out this is my function, and then we come down here, and we removed the indentation telling the function that we’re done. So now anything we write here is not considered inside that function. Now we could, we could additionally put more in this function simply by doing that and writing more indented to the fourth space in, and now also with Python this isn’t something that I’ve mentioned yet i don’t think but you don’t need to end statements with a semicolon liking in a lot of other programming languages, so just keep that in mind. Now we can drop down out of that, out of that function and now we can call that function. So to call a function you’re just gonna type the name of the function, and then any parameters that you’re going to pass in which would go in these brackets. In this case we’re not going to put anything in these brackets because we’re not passing in arguments, we’re going to be over that in the next video. Now actually you can see PEP 8: no newline at end of file which means we need a new line at the end file. You’ll see that the issue has been resolved. So PyCharm will kind of let you know if you’re ignoring or just not using any pep guidelines in your code. So let’s go ahead and save this and let’s go ahead and run, and as you’ll see down here in the console it first printed out this is my function, and then it printed out a second string. So if we look up here at the code again we’re going to go over the flow of this one more time. We’re defining a function, we’re function my function so that we can call it by that name, and we’re putting brackets here which will contain arguments at some point, you don’t have to, If your function doesn’t take arguments, it doesn’t need outside information, just don’t put anything in there, and then a colon which starts the indented block of code on the next lines which is indented four spaces. Now, I mentioned further indentation like in a function like eight spaces, 16 spaces, that’s for each block of code, and that’s not really something I can explain further, you guys are going to see it as we go on. So anyway this is the function because they’re both invented to four lines of Python knows that’s inside the function, and then down here outside the function we’re calling the function making it printed out. So, in the next video we’re going to talk about arguments and how to use those inside your functions.
Alright guys, we are going to talk about some built-in functions before we move on, and then we’re going to get onto defining our own functions, and the different parts of a function. So, let’s go ahead and open up a terminal here. I want you guys to loosely follow what I’m doing here so that you guys can get used to using these functions which I think we’re going to be using some of them quite a bit and you’re going to be using some of them quite a bit once you, once you’re done this course. So these are some basic, very basic, functions that we need to learn about, we’re not going to go through any modules like the math module or anything like that, we’re just going to go through a few basic functions. So let’s jump into a Python console here and we’ve already learned about a few so they’re just going to get a quick mention here. You can print out on the screen in the terminal, whatever you you want to basically use the print command, you can convert things to a string using the str function, and you can do this for both integers and floating points, and actually Booleans, and then for each of these types you can also convert them to their own types if they are a string. So I can use int here, and it’s going to convert that to an integer. I could use int that…oh sorry “flo(“5.6″)” I can use float to convert 5.6 to an actual number, and then you can perform mathematical operations on if you want. Now, let’s say we have a string like true, and we want to convert that to a Boolean to actually be able to test things with it. What we would do is type “bool(“True”)” and there we go that’s going to convert that to a Boolean operator. Now let’s also talk about a command called “len” Basically, what this is is it’s going to determine the length of something so it can work with arrays and strings. So let’s go ahead and try and figure out how long “len(“Hello”)” Hello is which means how many characters are in this, and there are 5. Now this command can also be used on an array. So “len([1, 2, 6, 3, 4])” just a few random numbers in there and it’s 5 digits long. Now also if you were put strings in here each with different a number of characters, you would see that this is still going to be equal to the number of items and not the number of characters within each item. So that’s how to calculate the length of an array. Now another thing you can do with an array that’s pretty neat is sort the array. So let’s go ahead and create an array which is let’s say we’re going to put some random numbers in here in you know no specific order there “[16, 3, 8, 6, 9, 133, 435, 21, 823, 45]” Now as you can see this is incredibly random but let’s say that we wanted to sort the array into lowest to highest, or you know first to last. What we would do is actually wrap it in a function called “sorted” and this will shift around the array until all numbers are in ascending order. Now let’s do the same with an array with strings instead. So let’s create some letters here, “sorted([“B”, “R”, “a”, “N”])” couple issues with that line of code there. So it sorts it but as you’ll notice the “a” is at the end. Well, why did that happen? If we were to capitalize it the “A” would that be at the beginning. So, again if we turn some of these letters into lower case which obviously come before other letters here, and one more, you can kind of see how it’s working. The capital letters are parsed first because that’s just the way it works, and then lower case letters, what if we threw a number in there? You’ll see that it can’t do that, so what you would have to do is wrapping quotes and let’s throw another number right here, and a floating-point wrapped in quotations, and you’re going to see that the way this sorted is it put numbers first regardless of if it’s a floating-point or integer type. They have to be wrapped in quotations in order to be sorted with other strings in an array, and then it parses the capital alphabet and the lowercase alphabet, so that’s kind of a priority order there. Just so you guys are aware of that. So what we’re going to do next is actually explore functions, this was just a few basic building commands in Python that we are going to use through the rest of the course, and we’re probably going to, well we definitely are going to, find other built-in functions as we move along, as we need them. So let’s get into functions.
Welcome back everyone, let’s talk about variables now. Now you can probably guess by the name a variable is something that has variable meaning that the name the value of it is not always going to be constant. So by explicitly stating strings they’re not reusable as well. If I wanted to you know say let’s jump into a Python console, alright. If I wanted to create a string not using variables, “This is a string” I could create that. However, there’s no way now for me to reuse it to do something else so I’d have to do “This is a string” + “Hello” and then there’s no way for me to reuse that one and see you can see the problem. Now that’s a one reason why we use variables is so we can reuse the variables, now another reason is because when you pass values into functions it’s going to take on the form of variable through the parameter list of the function, we will get more functions later. Now the difference between Python and some other programming languages is some programming languages are typed, and you might say well hey aren’t all programming languages typed? Yes, you type them with your fingers on the keyboard, however a typed language basically means that you need to define the type of variable. So as we learned in the previous videos the different types are integers, strings, Booleans and stuff like that. So if you’re setting a string variable you would have to explicitly tell the program that it’s a string but with Python you don’t need to do that. All you need to do is give your variable a name and a value. So, let’s go ahead and let’s create a variable called greeting and it’s going to equal a string that’s going to say “greeting = “hello world”” Now you’ll notice when I hit enter it didn’t print anything on the screen and it didn’t do anything noticeable and it’s because setting a variable doesn’t have any type of response, so it just creates variable and it’s there. Now we can use it by using greeting. So I could type “print(greeting)” and it’s going to print “hello world” Now also now that I’ve got it in a variable we can manipulate that variable in a number of ways. So I’m going to type “greeting = greeting.split(” “)[0]” So basically what we’re doing here, and I can’t use double quotation or double equal sign there, we’re setting greeting to a new value which is going to be greeting which we’re gonna split at the space, so we’re going to split it right here, and we’re going to get the first item in there. So it’s going to turn greeting into just Hello. So let’s hit enter here. We’ve assigned it a new value so if we now go ahead and “print(greeting)” you’re going to see that it says Hello. So now what we can do is now concatenate this with something else. So, we can do that in a number of ways we can reuse the previous example where we just let’s just print it out first, we can “print(greeting + “Nick”” and it’s going to say Hello Nick. Now if we wanted to store that in a variable we could do the same thing that we did before, or you can continue using this variable for different people. So I could say “print(greeting + “someone else”” and so that’s how that works. Now if i did want to make the greeting equal to concatenate a string all you have to do is redefine it as “greeting = greeting + “Nick” Now when we print out greeting you’ll see that it says Hello Nick. So that’s a variable and the useful things that you can do with it, now you can also assign values of other types. So we can say number is going to equal 1 and then second number is going to equal 2. So now we’ve got number and second number stored in variables. Let’s do something with that. So, what we’re just going to do is add these two numbers together in a print statement. So, “print(number * secondnumber + secondnumber * number)” and this is going to print out 4. So essentially what the print statement is doing is before it prints it out it’s calculating this because we’ve stored these as integers, and so it’s going 1*2+2*1 So it’s essentially saying 1*2 + 1*2 and it’s coming up with this, so. And as well you can set “check = True” So, that’s how to set variables, it’s pretty simple and we’re going to be doing a lot of this so this is a concept that you’re going to have to be very familiar with, and get used to using it. In the next video, we’re going to be talking about some built-in functions, and we’ve already actually talked about some of them.
So let’s get into dictionaries and that’s gonna end the sub-series I guess of types, this is going to be the last type that we’re going to talk about. So let’s go ahead and open up a terminal here and jump into Python. Now the previous video when we created a list we used square brackets and we entered comma-separated values in there which became our list, however in order to create a dictionary we’re going to open up sand close curly brackets here. Now in a previous video I told you guys that we don’t use curly brackets in Python and that was a lie, and just so you know there’s going to be a few of those throughout this course, so keep your eyes open for them. What I meant to say, and just to clarify, you don’t wrap code blocks in curly brackets like you do in PHP and JavaScript. Instead the only use that Python has for curly brackets is to define a dictionary. So, rather than entering comma-separated values we’re going to enter comma for separated values but each one of those is going to be a key and a value, so let’s go ahead and create one. Let’s create a person so the name is going to be {“name”: “Nick”, “age”: is 27, “hobby”: “code”} and that becomes our dictionary. Now, again, you can…you can access, I had tried to do that the JavaScript way here which you put a dot and then the name of the key that you’re looking for, in Python you do it like this kind of like when you define an index number here. If you were to do this it wouldn’t be able to find it because it’s not a list but with an array we would have done that, in here we just type the name of the value we want. So name is going to print out “Nick” if we put in age that’s going to print out “27” and if we enter hobby that’s going to print out “code” and so that’s how to define a dictionary and how to retrieve certain values from the dictionary. This is also going to become a very useful later on once we get to, well once we get JSON itself because we are going to do a video on JSON, but also once we start once we start working with a lot of data some of it’s, some of it we’re going to explicitly convert into JSON data just so we can parse it like this because this is one of the easiest ways to parse data is using this form. So thanks for watching this video and I hope you guys enjoyed it. This ends the sub section of types and in the next video we’re going to be learning about variables.
Alright, so let’s discuss lists and what they are in Python. So, let’s open up terminal again and launch a Python 3 console. So, if you do have experience with programming in other languages, let’s say PHP, you know how to create an array and what an array is. For anybody else it’s a way to to keep data organized and within one construct. So we’re going to go ahead and define an array and how we do that Python is just open up and close square brackets, and then in them we’re going to have some items and those items are going to each have their own index number within the array. So, let’s say we’re making a list of things that I like. I’m going to type “[“Movies”, “Games”, “Python”]” Now what this becomes is a list that has three indexes and we saw earlier when we split a string apart that created one of these and so you guys already know how to access items within this array, however if you forgot we’re going to go ahead and do that. So, basically I can put an index of 0 “[0]” and it’s going to print out movies. Now if we put that in a print statement, we’re going to concatenate it, “print(“I Like” +” Alright, we’re gonna get there so…let’s go ahead and print this out it says “I like movies” if we change that index number [1] it’s going to say “I like games” and if I change that to [2], oh not 3, so “I like Python” as well. Now what happens if one of these are a number? Let’s say I replace this with the number 16, let’s say “I like 16” go ahead and change this index to “[1]” then hit enter. It can’t convert the integer to string. So what we would do is if we were trying to print, and this specifically printing out an array and then choosing an index that time of recreation or list, that’s impractical and we’re never going to do things that way. So, what we just saw this error message doesn’t really matter. Basically again if you’re trying to concatenate strings with an integer it’s gonna freak out because it doesn’t know how to do that. So that is what a list is, it’s just a way to to create a collection under one variable name and when we discuss variables we will revisit this so you guys can see what I mean. In the next video we’re going to be talking about dictionaries, and basically these are like in JavaScript you can create JSON objects and that’s basically what they are. So, let’s get into that.