Today we will ask one single question. We may just ask it a few times, not just once, until we get the final answer. The question will be: What does a = b mean? You will immediately realize this brings up a lot of other questions. Like: a) who's a? b) who's b? c) will there be other questions?, etc. a and b are variables. They hold values. How do they do that? Not always in the same way. If a and b hold numbers the values actually fit inside the variables. Thus, if we have: a = 3 b = a # [1] print "a =", a print "b =", b a = a + 1 print "a =", a print "b =", b Can you trace the changes in the two variables, if any? This is very important. You see our b = a listed as line [1]. Can we put together now an answer to our question? The template was: a = ... (some initial value) print a b = a (our interest, here) print b use a to change the value it points to in some way print a print b Let's try this template again: a = [1, 2, 3] print "a = ", a b = a print "b = ", b a.append(3) print "a = ", a print "b = ", b First question this brings up is: What is a.append(...)? The answer is relatively simple and it has several parts: a) every list can extend itself by accepting new member elements b) append for lists is called like replace(..., ...) for strings c) the difference is that replace creates a new string as requested d) meanwhile append simply changes the list by adding the element For example: >>> a = [1, 2, 3] b = [1, 2, 3] a = [1, 2, 3, 3] b = [1, 2, 3, 3] >>> [1, 2, 3] [1, 2, 3] >>> "apple" 'apple' >>> "apple".replace("ap", "snap") 'snapple' >>> a = "apple" >>> a.replace("ap", "snap") 'snapple' >>> a 'apple' >>> [1, 2, 3] [1, 2, 3] >>> [1, 2, 3].append(5) >>> a = [1, 2, 3] >>> a [1, 2, 3] >>> a.append(5) >>> a [1, 2, 3, 5] >>> OK, so what's the conclusion, with respect to our original question? It looks like in some cases the variable doesn't quite hold the value. It looks like sometimes there is some sharing. The model is this: a) numbers are primitive values b) they fit inside the variables c) a = b means that the value of b is copied into a d) lists are not primitive values, they are objects e) this means their values are too big to fit in a named memory location (variable) f) so what is stored in that named memory location (variable) is the address where they are located g) a = b still means that the value in b is copied into a h) but this time around that means both a and b point to the same thing. What other questions can we ask: a) are numbers the only primitive values? Answer: no, booleans are also primitive and their values fit inside a variable completely. b) what kind of values are strings, tuples, dictionaries? Answer: they're reference values. They don't fit in a single named memory location. c) are there any other kind of variables besides primitive and reference? Answer: no, there is a small set of primitive values and a much larger set of reference values. Basically everything is a reference value if it's not a number or a boolean. Now let's talk about functions. Functions are generalized operators. You've seen functions in mathematics: f(x) = x + 1 g(x, y) = x + y In Python we have a set of primitive operators: +, -, *, /, %, and, not, or and such. We can define any number of other ways of making up expressions with functions. With the examples above: 3 + f(5) evaluates to: ______ g(3, 4) evaluates to: ______ g(f(4), 3) evaluates to: ______ g(g(1, 2), 3) evaluates to: ______ Notice that the functions have a way to receive ingredients, computing a result, and returning it. Here's how we define f, g in Python: def f(x): return x + 1 def g(x, y): return x + y Notice the new keywords: def, return. Notice the new syntax: the function has a block of statements as a body. Are other definitions of f, g possible in Python? For example, are f, g, allowed to modify their ingredients (arguments)? Let's look at this: def f(x): x = x + 1 return x Is this allowed? If it is, what happens in the following context: def f(x): x = x + 1 print "Inside f, x =", x return x x = 3 print "Before f is called x = ", x f(x) print "After f is called x = ", x What happens? Answer: ________________________ Let's look at something similar: def g(x, y): a = x + y return a What's the status of a? Let's imagine that this happens in this context: def g(x, y): a = x + y print "Inside g there is a local variable a =", a return a a = 3 print "Before g is called a =", a b = 4 print g(a, b) print "After g is called a =", a Can you explain what happens? Explanation: _______________________________________________________ x, y, a are local to g and separate from anything on the outside as long as g initializes them (via call or explicitly). To better understand this mechanism let's discuss this: def fun(x): if x == 0: result = 0 else: result = x + fun(x - 1) return result print fun(4) What does it do, what does it print? Answer: ______________ Why? Answer: ______________________________________________ Let's repeat this exercise twice, as follows: def fun(x): if x == 0: result = 0 else: print x # [1] result = x + fun(x - 1) # [2] return result print fun(6) Same questions as before. Answers: ________________________ You notice that we've added a line, marked [1], above. Let's swap [1] and [2] and repeat the experiment: def fun(x): if x == 0: result = 0 else: result = x + fun(x - 1) print x return result print fun(6) Does anything change? Why or why not? Answer: ______________ You're now ready to use functions in your programs. We just need to practice a bit more.