[Tutor] still not getting 'it' (fwd)
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Oct 6 21:27:16 CEST 2005
> No the question doesn't seem to simple at all, I appreciate your help. I
> have totally confused myself and I need to start from the beginning to
> sort things out. Thanks for you time and help.
>
> >>>double = 2 * 5
> >>>print 'The sum of 2 times 5 equals', double
> The sum of 2 times 5 equals 10
Hi Rosalee,
Ok, that's probably the problem then: it sounds like you might not be
familiar with writing and using functions.
Let's do a quick primer. For the purposes of trying to connecting to
things that you should know about, I'll borrow a math example, but if you
want, we can make examples using different domains besides math.
In algebra math classes, we may have seen things like:
f(x) = 2 * x
We might write on a blackboard such things like:
f(2) = 2 * 2 = 4
f(8) = 2 * 8 = 16
In a math function definition like "f(x) = 2 * x", we're trying to capture
the concept of doubling something, and we give that particular concept the
name 'f', just so we can talk about it later.
A "function" in Python is sorta like a "function" in mathematics. (It's
not quite the same, and we'll can talk about this in more detail later.)
But anyway, the doubling function above can be written in Python like
this:
######
def f(x):
return 2 * x
######
In a Python function definition like "def f(x): return 2 * x", we're
trying to capture the process of doubling something, and we give that
particular process the name 'f', just so we can use it later on.
Let's play with this 'f' function that we've defined. From the
interactive interpreter, we can enter in the function, and then test it
out on a few inputs:
######
>>> def f(x):
... return 2 * x
...
>>>
>>> f
<function f at 0x403a6ae4>
######
When we say 'f', Python knows that we're talking about some function.
(Um... ignore the weird '0x403a6ae4' thing for now. *grin*) The main
concept here is that 'f' is now a name for some thing that we can hold and
use, just like a number or a string.
Let's try using 'f':
######
>>> f(2)
4
>>> f(3)
6
>>> f(8)
16
######
We can use 'f' with different inputs, and we get back different outputs.
And we can even use f() like this:
######
>>> f(f(2))
8
######
Usually, programmers like to give their functions nicer names than 'f':
mathematicians like brevity, but programmers often like to be able to read
their programs. We can name 'f' as 'double':
######
>>> def double(x):
... return 2 * x
...
>>> double(double(2))
8
######
And we can follow this naming idea a bit further, and rename 'x' to
something else, like 'something':
######
>>> def double(something):
... return 2 * something
...
>>> double(42)
84
######
"To double something, just multiply 2 to that something."
Does this make sense so far?
Please feel free to ask questions on this: this is very fundamental
stuff, and if you're getting stuck here, we need to figure out what we can
to do help get you unstuck.
Good luck!
More information about the Tutor
mailing list