Worksheet: Building your own Python functions
Defining new Python functions
You have seen builtin functions, like type(), int(), str(), and functions from packages, like math.sqrt(). In Python, you can also define functions yourself. This is useful when you need to do the same thing (or something very similar) again and again. Suppose that you are in a country where temperature is given in Celsius, but you are more familiar with Fahrenheit. Here is how you can figure out that 20 degrees Celsius are 68 degrees Fahrenheit, and 30 degrees Celsius are 86 degrees Fahrenheit:
If you need to convert temperatures very often, this gets tedious. Instead, you can define your own function, similar to the way it is done in mathematics.
"def", which tells Python that you're defining a function
The name of the function, celsius_2_fahrenheit, which is another variable that you define
The argument of the function in round brackets
and a colon
Then, in the next line, indent by hitting "tab"
then define what the function does
Note that the Python shell changes its prompt from ">>>" to "..." after the first line. This indicates that you are in the middle of defining something that is not finished yet. To show Python that you are finished with the function definition, hit "enter" on the second line of "...".
Note also that function definition uses a variable, temp. Python uses this variable in much the same way as in function definitions in mathematics:
f(x) = x+1
There, you don't have to specify beforehand what x is. Rather, for each x, the function value is f(x) = x+1. In the same way, when you call your function saying celsius_2_fahrenheit(20), then at this time, Python stores the value 20 in the variable temp and executes the code of the function with temp set to 20.
Now we can use the function:
When you define your function, you have to indent the second line using either "tab" or spaces. Why is that? It is because the indentation is what Python uses to figure out the length of the function definition. You could in principle also define functions that take up more than one line (and in future, you often will). Here is a somewhat contrived example:
How does Python figure out how long the function definition is? By using the indentation. All the consecutive code that is indented the same number of spaces forms a block. It belongs together. In this case, it all forms a function definition together.
If you use different numbers of indentations, or if you don't use any indentation after the first line, Python complains.
Many of the builtin functions that you have encountered return a value that you can store in a variable, or use further:
That is, you can use len("dynamic") in expressions like you would the number 7. The function len() returns an integer, which we can see by inspecting wordlen:
Our Celsius to Fahrenheit function, on the other hand, does not return any value:
This means that temp contains the special value None. If we want our function to return a value, we have to say so explicitly.
Note the new reserved Python word "return" in our function definition. It tells Python to return the given value. Now we can use the value returned by our temperature conversion function like we would any other floating point value:
The command "return" means that a value is returned, and the function is terminated. That is, lines after "return" are completely ignored:
As you can see, the string "this line is never executed" is not printed, so this line is, in fact, never executed.
Try it for yourself:
Define a function that converts degrees Fahrenheit to degrees Celsius (for the benefit of Europeans like me). Make the function return, rather than print, the result
Define a function that takes a string as input, converts it to a floating point number and adds 3.14 to it. Make the function return the result. (For now, just assume that the string can be converted to a number successfully.)
Functions with multiple arguments
So far we have only defined functions with one argument. Functions with multiple arguments are straightforward:
If you define a function and give a value for an argument in the definition, you can leave that argument off when calling the function. Or you can specify the argument to give it a different value from the default.
But you have to put all the arguments without default values before the ones with defaults.
The function print() also has arguments with default values. For example, the default separator between two things to print is a single space. But you can change that. ("" below is the empty string, the string with zero characters.)
Executing a whole program at once
Open your favorite editor
In it, write the following very short Python program:
Save the file as first.py, making sure to save it as text only!
On a command line -- that is, not in a Python shell, but in a fresh terminal--, type
Depending on which directory you used to store first.py, you will have to type that directory name in front of "first.py".
If you are running Unix, this probably sounds straightforward to you. For instructions on how to execute a Python script under Windows, see http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows
Note: Windows needs to know where Python is located on your machine in order to execute Python scripts. You let it know where Python is by modifying the Environment Variable "PATH". Here is a good explanation how to do this (the above link does not help much): http://www.imladris.com/Scripts/PythonForWindows.htmlhttp://www.imladris.com/Scripts/PythonForWindows.html
Another way of running your Python program first.py is to use idle:
Start up idle
Load your program first.py into it by using the "File" menu, entry "Open".
Then in the "Run" menu, choose "Run module".
You should be able to see the output of your program in the idle shell now.
Whenever Python encounters the character #, it ignores the rest of the line. (Except when you use # inside a string, that is.) So this character is used to insert comments into code.
Where should you put comments?
At the beginning of your program: Who wrote it? When? What does it accomplish?
At the beginning of each function: What are the arguments? What does the function return? What does it do?
At other places in the code, whenever you are doing something nonobvious.
Imagine yourself 3 months from now, trying to make sense of the code that you have written earlier. What pointers would you appreciate getting?
You can also use comments to temporarily deactivate code that you don't want to use at the moment.