QUESTION: How do you perform basic arithmetic in MATLAB?
So in this matlab tutorial we’re going to go through some basic arithmetic in matlab. First I’m going to talk about the usual operations you see in programming which are plus, minus, multiply, and divide, and also the use of parentheses. So I’m going to first create some matrices for us to use, OK. So suppose I want to add two matrices, that’s very simple A + B. I want to subtract two matrices A – B. I want to multiply two matrices that’s A * B. So one thing to note about multiplication is this is pure matrix multiplication which means the inner dimensions of the matrices of A and B have to match. So, for example, if I create another matrix which is, if I create another matrix which doesn’t have the same number of rows as it has columns it’s not going to work. So, the size(B2) is 3 by 2 and the size(A) is two by two, so if I try to multiply A * B2 I’m going to get an error that says inner matrix dimensions must agree. So this does mean though I can do this, right? So, I transpose B2 it’s 2 by 3 and so a two by two matrix times a 2 by 3 matrix will give me a 2 by 3 matrix. So you can also divide matrices with the forward slash, but if you remember from linear algebra there isn’t really a matrix division like that. So, if you want to know what it means you can look it up on the internet, there’s a forward slash division for matrices and there is also a backslash division which we are going to cover in a later tutorial. By the way, if you want to divide two numbers that’s very simple we use the / for that. So the next thing we’re going to talk about is order of operations. It’s generally what you’d expect, anything in parentheses happens first, and then multiplication and division, and then addition and subtraction. So if I have A + B * C I would get that. So if I did B times C and I assign it to a variable D and I add that to A I get the same thing. So you can confirm that the multiplication is happening first. If I wanted the addition to happen first I would put parentheses around them, so we can confirm that what happened in the parentheses came first. So another thing you might want to do when you’re implementing an algorithm or writing some code is element by element operations. So we’ve looked at matrix multiplication which kind of does you
know if you’re looking at row I column J of the output that actually does a dot product between row I of the first matrix and column J of the second matrix. Something we want to do often when we’re coding is element by element multiplication, and so that lets just look at what we have for A & B again. So that’s pretty simple in matlab you just put a .* and so it multiplies each element by each corresponding element in the other matrix. This also works for division so a ./ So if you remember from your linear algebra classes there some different types of multiplication that you can have when it comes to matrices and vectors. First one were going to talk about is the inner product. I have x = (1, 2, 3) y = (4, 5, 6) and I want to do an inner product between these two vectors. One way to do that is simply by using what we already know. So x is a one by three vector and y is a one by three vector and I want the result to be one by one which is a scalar that means I can transpose y and multiply x by y as matrices, right, and so that would be 1 times 4, plus 2 times 5, plus 3 times 6. Another way you can do the dot product in MATLAB is by using the dot function. So we will cover functions more in a later lecture but I will introduce you to some today. So another product you might be interested in is called the outer product, and so that would be just the opposite of what I did before. So instead of transposing y and leaving x alone I’m going to transpose x and leave y alone. So that would give me a three by one vector times a 1 by 3 vector, and so the result would be a three-by-three vector. So that’s the outer product and there is no function for outer product in matlab, and so one thing to note is that usually when we represent vectors when you’re reading a textbook or sitting through a lecture, the vectors are column vectors so they have many rows but only one column, and so that’s kind of the opposite of what I’ve done just now, and so for the inner product you usually see x transpose times y and then for the outer product you see x times y transpose. So, just the opposite since I’ve been using row vectors. Lastly, there is the cross product and so the cross product is the magnitude is the magnitude of the first vector, times the magnitude of the second vector, times the sine of the angle between them, and then you use the right hand rule to determine the direction of the result. Alright, so dot product or inner product is not a vector so it doesn’t have a direction, but the cross product gives you back a vector and so that’s pretty simple to do in matlab, also. It’s just a function called cross.
Welcome to this matlab tutorial. This matlab tutorial is going to be on the basic syntax of matlab. So I’ve already opened matlab and this is generally the screen that you see most of the time. So the big main window’s called the command window, you’ll be working here most of the time. You can view your variables in the workspace on the bottom left, and you can view your current directory on the top left, and so that will tell you where in the file system you currently are. So, if you want to open a file from there you don’t have to type in the absolute path you can just type in the filename directly, or if you save a file that’s where it will go. So we call that the working directory and you can change the working directory to keep all your projects separate. So, you can go into the path up here and you know type in another path if you want to use the path for your particular project. So in matlab there are three basic types of numbers that you’ll be working with, or variables, that are all actually matrices. So you have scalars which are just plain numbers, you have vectors which is kind of like a list of numbers, and then you have matrices which are tables of numbers. So as I’ve mentioned before in matlab all these variables are represented as matrices. So if I do something like x = 5 you see that in a regular programming language you might just think of this as x = 5, but I can do something like check the size(x) and I see that it’s actually a 1 by 1 matrix. I can create a vector, we use square brackets, and you don’t need commas but I’m going to use them and so that creates a vector with the v = [1,2,3]; So if I do size(v) it’s a one by three vector, or you can also call it a one by three matrix. So this brings up some other interesting points about how matlab syntax works. I mentioned that you don’t actually need the commas so we can just do that and matlab knows that we want one by three matrix with the elements 123. So you also notice that I added this semicolon at the end of the line and what that does, it’s easier to see if I just don’t do it, so it prints out the last thing. So if I don’t use the semicolon it’s going to print everything out from the previous command, but if I do use the semicolon it will suppress the output. So, that’s why I didn’t put a semicolon after size(v). If I did I wouldn’t see anything. Now we’re going to go on to look at matrices, So it’s very similar to creating a vector except we need to have a way to separate each row, so that’s done with a semicolon and I’m not going to put one at the end so we can see the output. And so what that does is it creates a two by two matrix now with the elements 1234 in the variable A, and if I do size(A) it’s two by two. So those are the two things we’ve learned about initializing vectors and matrices that you can use commas to separate columns but you don’t need the commas you can just use spaces, and to separate rows you can use semi-colons. Another thing is that when you’re working with matrices and vectors
and you’re multiplying them and stuff like that, the dimensions of the vector are very important. So, typically you know you work with a column vector not a row vector like the one I just created, and so we can easily transform the previous vector which was a row vector into a column vector using an apostrophe and so that does a transpose. And so you see now that v used to be a one by three vector and now it’s a three by one column vector. You could have also initialized it using semicolons of course. Ok, so the next thing I want to talk about is how you can access elements of a matrix. Typically that’s done using parentheses and then indices and commas in between. So if I want to in general access the i row in the j column it would be a(i,j) So if I want a(1,1) for example I would do that. If I want a(1,2) and so on a(2,1), Another interesting thing you can do is access a vector within a matrix. So let’s say I want the first row of the matrix a, I can use the colon syntax to get the first row. So I’m going to put a 1 where I choose the row, and then I’m going to put a colon in the choice for column which means return me all the columns in row 1. So if I do that I get 1 2 which is the first world of A. Similarly I can use the colon syntax to get an entire column of A. So let’s say I want to choose the second row, or sorry the second column, so let’s say I want to choose the entire second column, I would do that. And of course if I do a colon in both parts of A it will just return me the entire A. So let’s create a bigger matrix now so I can better illustrate my next point. Alright, so I just created a four by four matrix. So the colon syntax can also be used to access ranges. So if I want I look at A it’s 1-16. If I want the part of A that has so a sub matrix that has the 6, 7, 10, 11 I could access that part by choosing the second and third row in the second and third column. If you’ve programmed before usually when you’re working with vectors, matrices, and arrays they’re zero indexed, in MATLAB it’s one indexed. So, a 1-1 returns me the first element, it’s not a 0-0. Another cool thing that we may use later is the colon syntax can be used all by itself it actually just creates a range. So if I did something like 1:10 gives me back all the numbers from one to ten, and so say I want to assign this to a variable, let me check the size of W, so that creates a row vector with the elements 1 to 10. It’s simpler to do than typing one through ten manually, right. So just to extend this a little bit we aren’t limited to only scalars, vectors, and matrices in matlab. You may have heard of a data structure called tensors, and so those are multi dimensional matrices, so it’s not a table it’s more like a cube or a hypercube. So you can instead of having a two by two matrix you can have a two by two by two tensor.
QUESTION: Where can you get MATLAB, add-ons, help with questions, etc?
Today we’re going to talk about, what is matlab, how do you get it, various add-ons and toolboxes you can purchase to extend the abilities of matlab, and generally how how you can get help using matlab. So the first thing I’m going to show you is just I’m going to Google matlab, and right at the top you see matlab’s official website. So that’s where you can go to get matlab and learn a little bit more about it just by reading on your own. To give you a short introduction though, matlab stands for matrix laboratory. So if you’ve ever programmed before it’s a little bit different because matlab is mostly for math and all the variables are matrices, even scalars are matrices. Matlab is usually used in academia and research so if you’re taking a university course which involves numerical computing you’ll probably use matlab. If you’re doing research and you’re analyzing data your professor probably already uses matlab and everyone in your lab as a result will be using matlab. Matlab is used in fields of engineering, science, finance, and economics. At its simplest matlab basically does calculations so it’s like a very fancy calculator. On top of that though, you can write code to evaluate a complicated formula, you can plot equations and data, and you can implement algorithms. Ok, so let’s talk about getting matlab. So I’ve already opened the website, if you scroll down to the bottom, or near the bottom, you can see a section called try or buy. So you can try matlab for free probably a trial that lasts 30 days…let’s check. It doesn’t say you have to enter your email but it’s a limited time trial so it won’t last forever. If you want to buy matlab there are several options. So the standard matlab you probably don’t want to use its over $2,000, and if you’re part of an educational institution you can use the education tab and get matlab for $500, the home version is just for personal use that’s a $149, and the student version is $49 so you probably have to go through some process to prove that you’re student. Another way that you can probably get matlab is if you are a student at a university there are options that the university has for you to get matlab, so you have to consult with your University’s resources such as the website or the bookstore to see if you can possibly even get it for free. For this tutorial I’m going to be using the 201 a version of matlab. So one thing you’ll notice is for the home version and the student version is they have these add-on products that you can get for a price. So add-ons are also called toolboxes and they have functionality and code that can be useful for you in your specific field of work. So, for example, if you do bio informatics you may want to purchase the bio informatics toolbox. So you can click on the links and get an idea of what’s included in the packages, maybe you do bio informatics and you don’t really even need the functions that the tool box comes with, so you might not want to purchase it. But, in general you can browse the documentation for free so you can see everything that you would get if you did purchase the toolbox. It’s essentially an API, so you can look at any function see what the input and the output is and decide if that’s useful for you. The next thing I want to talk about is how to get help with matlab. So you’re already taking this course and you’re going to learn a lot about matlab here, and when you start coding they’re probably going to be some very specific things you want to do that you don’t know how to do yet. So one place you can go I clicked on the community tab on the matlab website and so you can see there is a community of people who use matlab who post on this site. The file exchange I’ve used before people post useful functions and code that you know you may need in your projects that can be useful. There’s also a section called matlab answers where people ask questions and you can answer them or you can ask a question and someone else can answer you. Personally, I would recommend just searching your question on Google because they’ve already indexed all this stuff you could potentially put your question in the search box on the matlab site as well though, and you might on Google also get stackoverflow results so it wouldn’t only be matlab central which might be helpful.
Alright guys, welcome back. We are at the end of the second section for this course which means you guys have a basic understanding of Python. You actually have enough right now to build some basic programs. Now, you might be thinking well, hold on, we didn’t even do that much. That might seem like it’s true but we’ve learned some core principles and concepts here as well as the language syntax, so far. So, we currently know how to create loops like while and if, or while and for loops, we know the if else statement, we know how to create strings and all the different types like Booleans, and numbers, and we also know how to set variables, among some other stuff. So, what we’re going to do right now is for the project for this section we’re going to put most of what we’ve learned in this section and we’re going to build a calculator program. So, I’m going to open up Chrome here and we’re just going to search Python calculator, just to see you know basically how we might write a calculator, so this looks good. So as you can see in the top of the script their defining for functions, one for addition, subtraction, etc, and each one of them is going to take a limit of two numbers and it’s going to return the result of the chosen mathematical equation between the two numbers. So that’s going to be the same for multiply, divide, everything and here’s where the actual program is going to start to output/input. So basically it’s going to say select an operation and it’s going to tell you what each number is equal to, and then it’s going to create a variable called “choice” and it’s going to use an input function which basically allows the user to type something in that hit enter and whatever the types can be stored choice. So this is going to be the prompt, so this is going to print out before the input so basically it’s gonna say enter choice 1, 2, 3, 4 and then whatever the user types after that it’s going to be stored in the variable choice, and then they’re creating two other variables, the first number and the second number, and for each is going to ask you to enter the number. So, first you enter the type of math you’re gonna be doing, and then you enter the first number, and then you enter the second number, and once you enter here what it’s going to do it’s got everything it needs, so it’s going to use an if else statement to determine what your choice was so that it knows what function to call with the two numbers that you’ve put in, which is awesome, and you guys might be really excited to build this. But, we’re actually going to go a few steps further, and the reason is if we open up a calculator you’re probably going to know that I wasn’t asked right off the bat what mathematical operation I want to perform, it just loads up with the number 0, and then I can type a number so let’s say 50, and the mathematical operator which we are going to use multiply, then two, we’re going to enter and we’re going to get a 100. Now with the ending of the math equation here you’ll notice two differences between this program and the Python script in the background there, and that’s that it didn’t quit, and actually we can continue to operate on the result of that first equation. If I want to add 75 I’m going to hit + 75 and hit enter and it’s going to add it to the result. If i want to multiply that by 2 I just hit x 2 and it’s going to multiply by that. So this is a continuous mathematical operation where the Python script allows you to perform one operation and is limited to two numbers, and it’s not very efficient, and I guess very simple. So, that’s good but we’re going to go a little more advanced. So, go ahead and open your IDE. What we’re going to do here is we are going to be using the regex library, so we’re going to import regex, and we’re going to print out the name of our program and it’s going to be called “print(“Our Magical Calculator”)” Now right off the bat we’re going to create two variables, so “previous = 0” and what this is gonna do is the previous variable is going to hold the result of the previously calculated equation, so we’re going to set it to 0 because we haven’t done any math yet, and then we’re going to create a variable called “run = True” You guys can probably guess what this is gonna do. Now, we need to create a loop for our program so we’re going to type “while run”: and we’re just going to call a function called “performMath” Let’s go up here and create a function called “def performMath” So the first thing we need to do is be able to accept input from the user to type in something, so let’s go ahead and type “equation = input(“Enter equation:”” and for now we’re just going to print out whatever they type so “print(“You typed”, equation)” hit save, now let’s run this. So I’m just going to print “Hello World” and it says you’ve typed Hello World, and then it drops back into the prompt. So going to type “Again typing something” and this is going to loop forever because we haven’t created a way to stop it other than, you know, closing the terminal window, or hitting stop right here. So, what we’re going to do now is create a way to end or quit out of this application. So let’s go ahead and “print(“Type ‘quit’ to exit\n”)” so let’s save that, Now what we need to be able to do is if they type “quit” we want to be able to you know actually quit out of the program. So what we need to do is we need access to the run variable in here, so for example let me just do this first. “if equation == ‘quit’:” let’s go ahead and set “run = false” “else:” print out whatever we typed. So go ahead and save and we’re going to run this. So we can type “Hello” and what it’s doing is this we didn’t type in quit so it’s not doing this instead it’s doing this. So let’s type in “quit” it’s not doing anything. So, basically what’s happening here is this is variable scope. Basically, to explain this this is a variable we created the top level of our program, so not defined in any functions or anything. We are in a function here and we’re in an if statement so we’re further into the code and we’ve stated run equals false. Now this is not going to have any effect on this very well it doesn’t have access to it. So, what we need to do first is actually get that global variable into this function. The way we do that is just at the top of the function type “global run” and then the name of the global variable you’re trying to get access to. Let’s go ahead and save and restart. So you’ll see we can still type in stuff but soon as I type “quit” it actually exits. So that’s what we wanted to do. Now, let’s go ahead and restart this. We’re expecting equations to look like this. So, how can we do that? Well, let’s actually how can we do that without creating a limiting set of functions that will perform operations, what if we want it to do this, you know, or what if we wanted to do this? In the script that we just looked at this wouldn’t be possible because we can only use two numbers and they’re each collected in their own variable, So what we’re going to do is we are going to use a built-in function. So actually what I’m going to do is grab access to the previous global variable as well, going to set previous here “previous = equation” then if we go ahead and rerun this, well we see it still says you’ve typed 80+2, or you’ve typed you know whatever the equation was. So we want this to perform math and what we’re going to do is we’re going to use a built-in function called “eval” So let’s go ahead and type that here “previous = eval(equation)” and then let’s save, let’s restart, and you’re going to see that before it prints out the result, or whatever we’ve put in, it’s going to evaluate it. So this is going to be able to perform complex mathematical operations from strings. So we can literally type in “80*100+42-10+78*142” hit enter and it’s going to calculate that up, and it does it in the correct order. So we all know that multiplication happens before addition and the evaluate statement our function actually is aware of that and it does that for us. You might be thinking, well that’s awesome, why don’t you know in those basic calculator examples why don’t they just use the eval function? The reason is because the eval function is actually supposed to be avoided because it can be dangerous. So, let me show you why. I’m going to print out here “print(“Hello World”)” you’ll see what happens is when it evaluates actually if we type in Python code here it’s going to execute that. So, let me go ahead and set “global run” see so we can actually crash it as well. Now this is going to work as long as we assume that our users are going to be performing math equations, but what if they are not? Well, because of this we are actually going to do this. So go ahead and “equation = eval(equation)” is going to be equal to the evaluation of equation. Actually, hold on “previous” Okay, so what we need to do first is perform regex on it, so we want them to only be entering mathematical symbols or numbers, we don’t want them to be able to type anything or to issue commands. So the way we do that is basically let’s create a new variable, actually let’s assign here we’re going to be entering our regex so “equation = re.sub( ‘ [a-zA-Z,.()” “] ‘ , ‘ ‘, equation)” “equation = re.sub( ‘ [a-zA-Z,.()” “] ‘ , ‘ ‘, equation)” So we’re going to remove everything except you know well everything important. Let’s actually go ahead and also remove the colons, we’re going to replace it with nothing, we’re going to do this equation, so we go ahead and show you what I mean. We’re going to rerun this and I’m going to say, “Hello 6 World” and what happens is going to strip everything out of that before it evaluates it, so it’s only going to accept numbers or as you can see you know the plus symbol, sorry, plus right there, let me remove that, alright. There you go. So “5+11×2” is 27 because 11 x 2 is 22 + 5 = 7. So, we’ve already got an awesome calculator but it doesn’t look quite right. So what we’re going to do is we’re basically just going to put an if statement, it’s only going to ask for you to enter equation if there’s no previous. So what we’re going to do is actually “equation = ” “” here, we’re gonna go ahead and say “if previous == 0:” it’s gonna put that otherwise equation is going to be “else:” “equation = input(str(previous))” So let me go ahead and the first time it’s going to ask for an equation, so “4+3” and then it’s is going to drop down and we can go “-1” and you’ll see that it doesn’t quite work. So, what we need to do is we need to tell it if it is, let me separate this here, there. We want to evaluate it separately if there’s a previous result or not. So what we need to do is let’s go ahead and “if previous == 0:” we’re actually going to do this “previous = eval(equation)” which is going to only evaluate whatever we type in. “else: previous = eval((str(previous) + equation)” “else: previous = eval((str(previous) + equation” Go ahead and save, restart, we’re going to go “4+4” equals 8, “-2” is 6, “*10” is going to be 60, and you can see that we’re actually using different types of math on it. So “+5-2+3” is 66. So this is what we want, but let’s just remove where it says you typed because we no longer need that. “3-2” is 1, “+56” and you can see that now we can really get into the math here. You can evem type, let me start that again, so you can perform basically any mathematical operation here, and when you’re done just hit “quit” Now we wanted to give a specific message when it quits, so let’s just drop down here and have it print out “print(“Goodbye, human.”)” Go ahead run it and we type “quit” you’ll see it now says “Goodbye, human.” and then it closes. So this is actually a completely valid program, and it looks a lot different from the other example that we looked at. Now the eval function can be dangerous OK, it very well can be, and that’s why what we’re doing before we evaluate anything is we’re making sure there’s no letters and there’s none of these characters in it, because if there were you know somebody could…let me open up this. I can’t do that, okay. Somebody could you know if they import the system and the OS modules that are included in Python they could end up damaging their system, and so by using regex to remove all that before its evaluated we’re keeping things safe. So thank you guys for joining me and congratulations on finishing the second section of this course. In the next section, in the next bunch of videos, we’re going to be learning more advanced concepts, and we’re going to be developing more complicated programs than this.
Alright guys, so we are about to create a calculator program in Python and before we do that actually we need to learn a few more things. So we’re going to go over a couple of the loop types in Python, so there’s two. One of them is a for loop and one of them is a while loop. So basically a for loop is good for if you want to iterate over an array or a list in Python. You can do something for each item in the list, so let’s go ahead and create a list. So let’s just call it “numbers =” and we’re going to create array “[1, 2, 3, 4, 5]” So let’s type “for item in numbers:” and we’re going to just “print(item)” going to save and let’s go ahead and run this, and you’re going to see that on each line it prints out it’s number. So, we would be able to do this if we wanted to have different names in here. So, again, “[“Nick”, “Someone”, “Another Person”]” save this. Let’s go ahead and run the script again and it’s going to print out “Nick Someone Another Person” So what we can do here actually is we can “print(‘This persons name is”, item)” So let’s go ahead and you’ll see that for each one it does print out that. So that is a for loop and basically the second parameter here in the for loop is the array or the list, and then the first one here is what you want each item in the list to be called while inside it’s little block of code. So in this case we’re calling it item. So that is a for loop, now we’re going to learn about a while loop. So let’s go ahead and create two variables, one is going to be called “run = True” and the other is going to be called “current = 1” So let’s go ahead and what we’re going to do is type “while run:” and then we write what we want to happen you know if it’s currently running. So what we’re going to do is we are going to put an if statement here, so we’re going to go “if current == 100:” actually don’t need those brackets. Alright so if “if current == 100:” we are going to set “run = False” but if it’s not “else:” we’re going to “print(current)” then after we print current, “current += 1” Let’s go ahead and save this and what this is going to do is basically we’re setting run to true initially because we want it to run at least once, so while run which in this case the first time it goes over it’s definitely going to be true. It’s going to check is current equal to a 100. Well, on the first time no it’s not it’s equal to 1, so this block of code won’t run. If it’s not equal to a hundred what it’s going to do is it’s going to print the current number, and then add one to the current number, and then run is still true so it’s going to go over it again. So what we should see here is it’s going to print the numbers 1 to 99. So let’s go ahead and run the script, and that’s exactly what we see here. Now these are two useful concepts that we’re going to be using, so hold onto those, and if you didn’t quite understand what I’m doing here, let me know in the discussion section.
Alright guys, now we’re going to be learning about some conditional statements in Python. So we’re going to be learning about the if-else statement, and this basically provides a way to evaluate if something is true or false, or if something is something, or it’s not something and can do something different based on which condition is true. So let’s go ahead and let’s set “check = False” and what we’re going to do with that is we’re gonna say “if check == false:” “print(“It is false”)” and I’m going to save this and this is the minimal you need for an if statement. We’re going to go ahead and run this and it’s going to say it is false because it is false. Now if we were to change this to true, and run the same code, it’s not going to do anything, and that’s because we haven’t provided and else. So if it’s equal to false it will do this otherwise it’s just going to do nothing. So let’s go ahead and drop back four spaces to where the if statement began, I guess, and that type “else:” and we’re going to “print(“It is actually equal to True”)” So let’s go ahead and let’s run the script and you’ll see that now where this is not the case, check is not equal to false, so then it’s going to do this instead. Now, what we’re going to do is this is good if you want two conditions, but what if you want more the two conditions? Between the if and else you can actually use something called “elif:” and here we actually need to supply a condition to the elif. So think of it like another level to the if statement, and think of the else as a catch-all like if none of the above are true than just do this. So if check equals false it’s gonna do that. “elif_check == “Hamburger”:” we are going to “print (“Yummm, hamburgers”)” and let’s throw another “elif_check == “Yo”:” and say “print(“Hello”)” So let’s go ahead and save this, we’re going to run it again, and as you can see check is equal to true. So it says is this true? Nope, move on. Is this true? Nope, move on. Is this true? No, move on. Okay, well the else is going to say well if none of the above is true them we’re just going to do this. So what we’re going to do right now is we’re going to set this to “check = Hamburger” and we’re gonna run the script, and you’ll see what’s happening is it’s checking this one first, that’s not true, so it moves on its like okay this one’s true so let’s just do this and then let’s get out of the if statement, so it doesn’t check any further. So that’s what an if statement is in Python and that’s necessary for what we are about to do.
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.