Functions are a fundamental unit of code in Python. This lesson describes how functions are created, and how they are used.
- [Narrator] Functions in Python serve the purpose of both functions and subroutines in other languages. Here in Komodo, I've opened a working copy of function.py from chapter two of the exercise files and here we have a very simple function, with just one line of code in its block. The function is defined with the D-E-F, def keyword that defines a function, then we have the name of the function, and it will always have parenthesis, even if it does not take any arguments.
And any arguments are separated by commas inside of those parentheses. And then that argument becomes a variable within the scope of the function, and that variable can be printed in this case. And so, here's the call to the function, the function call, I have the name of the function, and then the parentheses again, they're not optional, even if there's no argument, and a value. So here, we're passing the function the value 47, and it will print the number 47 when I save and run it.
And there we have the number 47 in our command output. So, if I give it a different number, say 12, and save and run, it prints a 12. If I don't give it a number, then we get an error, because the function requires an argument, but we're not giving it one. We can however give the argument in the function a default value, say n equals one, and that way if I do not provide a argument, then it'll default to one.
So now when I save and run, we get, the function works, we get a value, even though I didn't pass in a value. But still, if I do pass it a value, it will use that value instead. Now, in Python, all functions return a value, and so, if I say X equals function, and then print X, we'll see what this returns. We're not returning it anything explicitly, so by default it returns the default value of none, which is the absence of value.
It's a special keyword, and a special value in Python that means the absence of a value. And yet, if I give it a return statement, say n times two, then it will return that instead, and so now it's returned a value of 84. As a practical example, here I have a working copy of primes.py, I'm just going to close this bottom pane so you can see the whole thing. This is a simple example, where we assign a value of five, and we test to see if it is a prime, or it is not a prime.
And this is the function isprime, and it returns true if the number we pass it is a prime number, and it returns false if it is not prime. So, if I run this, of course five is a prime value, a prime number, and if I give a six instead, then it will say six is not prime, because six is not a prime number. If I want a list of prime numbers, then I can define a function to give me a list of primes.
You notice that I have to use the parentheses, even though I'm not passing a value to this. It's just going to define a list of primes, and I'm going to say for n in range, 100. So, this will give us a range of numbers, from zero to 100, and not including 100, because that's the way the range function works. If isprime, and give it that n, then we print n. And I'm going to use a special argument to print, it'll have it end the print with a space, instead of a new line, because normally print gives us a new line after each printing.
This'll give us a space instead. And, because of the way that some operating systems work, I'm going to flush the output buffer, and we'll talk about that later. And when we're all done, we'll just print a new line by itself. So now we have a function that will list primes, and I'm going to call that here, list primes. And again, I have to use the parentheses, even though I'm not passing it anything, and when I save and run this, you see we get a list of prime numbers up to 100.
So, functions are a fundamental tool in Python. They're used for creating reusable code, and you see here in this example, I reused isprime for two different purposes. They're used as methods and objects, and they're simply used for breaking down code into smaller, more manageable pieces, as I did with list primes here. We'll cover functions in more detail later in the course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api