Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
Functions, or subroutines, are a very common form of reusable code, so let's look at how functions are done in Python. We'll make a working copy of syntax.py. We'll call this syntax-functions.py. Go ahead and open our working copy. We'll notice, of course, that main here is a function. And the purpose of that is not so much reusability as it is modularity in allowing us to create more functions, and be able to run them from main.
So let's go ahead and create a function here. And I'll just call it func. And we'll print out a range of numbers. And in my print statement I am going to say end= ' '. And that way instead of putting each number on a separate line, we'll put them all in the same line, separated by spaces.
And I'll put another print after it, an empty one to print a new line. And so I'll save that, and I'll call the function from in main. And we'll just call it instead of this print function here, we'll just say func. And that's all we need to do to call our function. So we'll save it and run. And we see we are printing out this sequence of 10 numbers. And this is, in fact, how the range function works. It prints out everything not including the number of the range.
So it's 10 numbers staring at 0. Now if I wanted to call the function again, I would just say func again, and I can do that several times. And each time it will run this same block of code. So I save it and I run it, and that's how a function works. So let's look at how we define the function. The def keyword is for defining the function. That means that what comes next is going to be the name of the function, so there is the name, and then there is a set of parentheses where we would put arguments, or parameters.
And the colon says that this is going to be followed by a block of code, or a suite of codes, as it's called in Python. And then we have the suite of code itself. The suite of code is indented under the definition of the function, and in this case of course, we're using the traditional four spaces of indenting, like that. Inside the parentheses we could put arguments. So for example, if I put an argument called a there, then I could say a, 10.
So it would start at the number a in its range here, and so here I could use a 1, and here I could use a 3, and here I could use a 5. And it would use that as an argument to the range function. What that does is it defines a starting point for the range. And so if I save that and run it, then we see we have one that starts with 1, one that starts with 3, and one that starts with 5. And so this argument in the parentheses is used inside the function in this way.
We can give the argument a default value by saying a=0. Well, let's give it something different so we can see. Let's say a=7. And if in this middle one I don't put an argument, then that will be the default argument that's used. It'll default to 7. So if I save this and run it, you see that the middle one starts at 7. So there are a lot of other things we can do with functions and with function arguments, and we'll cover those later in our chapter on functions. But this is, in a nutshell, how you define a function in Python, and how you call and use that function, and how you can use arguments when you call and use that function.
Find answers to the most frequently asked questions about Python 3 Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.