Read section 6.1 in the book and do the examples.
Let's create a new fruitful function:
def add_three(x, y, z): """Returns the sum of three numbers.""" return x + y + z
We can test this out:
>>> x = add_three(1, 3, 5) >>> x 9 >>>
Sometimes we need to use conditionals, as the book shows. Here's another example:
def largest_of_three(x, y, z): """Returns the largest of three numbers.""" if x >= y and x >= z: return x else: #here, either y > x or z > x if y > z: #in this case, y must be greater than x too, so just return return y else: #in this case, z must be greater than x and y return z
Test it!
>>> x = largest_of_three(1, 3, 5) >>> x 5 >>> largest_of_three(2, 4, 2) 4 >>> largest_of_three(-100, -100, -100) -100