28. Variables and functions

Variables, which we have seen in the previous lesson, are especially useful in function definitions. Remember our print2 function?

>>> def print2():
...     print 2
...    
>>> print2()
2

We can make it more general as follows:

>>> def print_number(n):
...     print n
...    
>>> print_number(2)
2
>>> print_number(78)
78

By writing a variable as an argument between the two parentheses when we define the function, we tell Python that, whatever value we use when we call the function must be assigned to the variable (n in this case).


Combining numbers and strings

Suppose we want to print both numbers and strings together. By this, I mean, suppose we want the output of the print_number() function to be something like:

The number you have chosen is 2.

There are many ways to do this as you will see later. Here's one way which is not obvious, but which is very useful to know.

>>> def print_number(n):
....    print "The number you have chosen is %s." % n
....    
>>> print_number(3)
The number you have chosen is 3.

Python replaces the combination %s inside the string by the value of the variable that follows the % after the string. Try it on your own, and then keep on reading.


Moving on to greater things

Let's go back to our greater() function and make it more general. This is how we can do it

>>> def greater(first, second):
...     if first > second:
...         print "%s is greater than %s" % (first, second)
...     else:
...         print "%s is greater than %s" % (second, first)
...     
>>> greater(5, 4)
5 is greater than 4
>>> greater(0, 10)
10 is greater than 0

Note that we must put parentheses () around the two variables that follow the percent % sign so that Python knows that we mean to use them to replace the two %s that are inside the string.

Try it!

Remark: You tried and tested the function with many values? You must have noticed your program has a little defect, haven't you? When programming, you must pay attention to particular cases:

>>> greater(10, 10)
10 is greater than 10

Do you see what I mean now? I deliberately omitted the case when the two numbers are equal in order to simplify, but now, it's your turn to fix this omission. You may use the elif keyword to do that (you didn't forget it, I hope? if you did look back a few lesson). You will see the solution in what follows but like always, try first!


Moving on to even greater things

We have just seen how we could make our function greater() more general than always comparing the same two values (as it was before) by using variables. Now, as it is written, it always require two arguments: the two numbers to compare. Suppose we would like have Python decide if some numbers are greater than the special value 0 (zero). We could write another function to do that ... or we could change slightly our existing function as follows:

>>> def greater(first, second=0):
...     if first > second:
...         print "%s is greater than %s" % (first, second)
...     elif second > first:
...         print "%s is greater than %s" % (second, first)
...     else:
...         print "%s is equal to itself!" % first
...     
>>> greater(5, 4)
5 is greater than 4
>>> greater(1, 10)
10 is greater than 1
greater(5)
>>> 5 is greater than 0
greater(-2)
>>> 0 is greater than -2

By writing "second=0" between the parentheses in the function definition, we are telling Python to use that value for the second variable if we call the function with only one argument. When we give such a default value to an argument, we call it a named argument.

Named arguments (there can be more than one) must always appear after the unnamed arguments. Therefore, the following definitions [only the first line is shown below] would be allowed:

>>> def four_arguments(a, b, c, d=33):
>>> def four_arguments(a, b, c=7, d=2):
>>> def four_arguments(a, b=1, c=3, d=56):
>>> def four_arguments(a=4, b=1, c=3, d=0):

but the following definitions would not be allowed:

>>> def four_arguments(a=1, b, c, d=33):
>>> def four_arguments(a, b=2, c=3, d):
>>> def four_arguments(a=5, b=1, c=3, d):

Try it while making up your own function definitions.


Final word of caution

Python does not allow you to use one of its keyword as a variable. Try the following:

>>> def = 2

and see what you get!

previousVariables - home - What's your name? next