Click here for a transcript.
In this video, let's talk a little bit more about functions. So, when I introduce this print function you might have asked yourselves, “Well how did Dr. Morgan know to put in, ‘hello world’ here in quotes, or x or y here? How do we know how this print function works?” Well, in a subsequent video we'll talk a bit more about debugging code and getting help with functions and various things in Python. But I want to point one thing out to you and discuss the basic usage of how functions work in Python.
So, to do that, let's insert a new code cell and let's continue on with print as an example, here. Suggesting print is the built-in function. If I do a parenthesis what automatically pops up in Google Colab, and this is very nice, is how to use the function. So, first I would have to know that some function called print exists. And that's a separate issue. In this class we’ll introduce you to many functions to use for data processing visualization, and analysis, and statistics, and things like that. So, we'll cover a lot of those. So if we know print exists, we want to use it, well the next question is well how do we use it? And that's what these instructions are telling you here. Although initially, this might not make a whole lot of sense to you.
One key thing to focus on here is this line of example code right here. This is generally stating how to use the print function. So, we have the function, print, open parentheses, some value. This could be a text or a number, as you've seen in examples previous. Dot, dot, dot means we could have any number of additional values included here. And then we've got some strange things. We've got sep, end, file, all those business. What are those? Well, all these things separated by commas here are called arguments to the function. These are things that could be supplied to the function and so it needs values, those are required arguments, and then these are optional arguments. And what's being shown here are the default values for those. And we can read about them further down.
So file, a file-like object or a stream. Default is to do the current system standard out. This is basically just printing the screen, is the default. We see that here, sys.stdout means print to screen for this file argument. But you could provide paths to other files if you wanted to write to those. sep string or text inserted between values, the default is a space. And then, end string. Again, string is referring to text appended after the last default, a new line, and so on so forth.
Okay, so how can we put these to use? Well, we could have one value, we could say our value of z is… and then z. Let's see what happens here. So, we're supplying two values now. Of course, I didn't need to run these again. There we go. Our value of z is 36. And I could change how these are separated. I could say sep equals, and I could put anything I want in here. If I put a colon, it changes that space to a colon, if I wanted a new line, it's dash n, or I should say backslash n, is 36, and so on and so forth. And so, I could tack on any number of things here. I could say z equals x plus y. Note that each instance is separated by a new line. [unintelligible] Or want to go back to just a space again, include that, and so on, and so forth. So, I think you get the idea here. Okay, so that's a little bit more detail of how functions are structured and how they operate in Python, and how to learn about what arguments to supply to specific functions once you know what those are.