Syntax Summaries
Throughout this book I use summaries to summarize parts of Python syntax. These are the grammatical rules of the language, and—although they are generally easier and more natural than syntax rules for human language—they must be followed precisely. If you’re required to use a colon at the end of the line, you must not forget it.
Here is the syntax summary for function definitions:
def function_name(argument) :
indented_statements
There actually is more to function syntax than this, as you’ll see in Chapters 9 and 10. As I’ll show later in this chapter, you can have more than one argument; if you do, use commas to separate them.
In a syntax display—such as the one shown previously—items in bold must be typed in as shown; the items in italics are items you supply, such as names.
Here’s another example you can compare to the syntax summary:
>>>def print_age(n):
print(’Happy birthday.’)
print(’I see that you are’, n)
print(’years old.’)
>>>
Remember, as always, that to end the statement block from within the interactive environment, type an extra blank line at the end.
Remember, also, that certain errors are not detected until the function is executed. Suppose a function does not contain syntax errors, but it tries to refer to a variable that is not yet recognized. Executing the function will generate an error unless the variable is created before the function is executed.
For a variable to be recognized, one of several things must happen.
The function creates a variable by assigning it a value during an assignment (=).
The variable must already exist because of an earlier assignment.
Or, the variable exists because it represents a value passed to the function (for example, n in the previous function-definition example).
Here’s a sample session that executes the print_age function. It assumes that this function has already been defined through the use of a def statement, as shown earlier.
>>>print_age(29)
Happy birthday.
I see that you are 29
years old.
Here is the same function, this time called with the value 45 rather than 29:
>>>print_age(45)
Happy birthday.
I see that you are 45
years old.
The built-in print function has a simple syntax—although there are some special features I’ll introduce later.
print(items)
When print is executed, it displays the items on the console, with an extra blank space used to separate one item from the next.
During the call to print, you use commas to separate arguments if there is more than one.
>>>i = 10
>>>j = 5
>>>print(i, ’is greater than’, j)
10 is greater than 5
Example 3.1. Quadratic Equation as a Function
Now let’s do something a little more interesting: take the quadratic formula example from Chapter 2 and place it in a function definition, by using the def keyword.
The quadratic formula computes the value of x, given the following relationship to arguments a, b, and c.
0 = ax2 + bx + c
The following interactive session defines quad as a function taking three arguments and returning a value, which is the solution for x.
>>>def quad(a, b, c):
determ = (b * b - 4 * a * c) ** 0.5
x = (-b + determ) / (2 * a)
return x
>>>
With this definition entered into the environment, you can then call the quad function with any values you like. For example, a simple quadratic equation is as follows:
0 = x2 + 2x + 1
In this statement, a, b, and c correspond to the values 1, 2, and 1, respectively. Therefore, by giving the values 1, 2, and 1 as arguments to the quad function, we will get the value of x that satisfies the equation.
>>>quad(1, 2, 1)
-1.0
This means that we should be able to plug the value –1.0 in for x and get the quadratic equation to come out right. Let’s try it.
0 = (-1)2 + 2(-1) + 1
= 1 - 2 + 1
It works! Everything checks out nicely, because plugging –1.0 in for x does indeed produce 0. But a more interesting equation involves the numbers 1, –1, and –1, which give us the golden ratio. That ratio has the following property:
x/1 = (x + 1)/x,
That equation, in turn, implies the following:
x2 = x + 1
This in turn yields a quadratic equation, as shown here:
0 = x2 - x - 1
Finally, that gives us values for a, b, and c of 1, –1, and –1, which we can evaluate with the quad function. Let’s try it!
>>>quad(1, -1, -1)
1.618033988749895
And this turns out to be correct to the 15th decimal place. This is the special number “phi.” One of its many special properties is phi squared minus 1 produces phi itself. This is the golden ratio.
You can verify it this way:
>>>phi = quad(1, -1, -1)
>>>phi
1.618033988749895
>>>phi * phi - 1
1.618033988749895
A-ha! Phi squared, minus 1, gives us phi again! This is indeed the golden ratio or, rather, a close approximation of it.