June 2017
Beginner
352 pages
8h 39m
English
To demonstrate Python's argument passing semantics, we'll define a function at the REPL which appends a value to a list and prints the modified list. First we'll create a list and give it the name m:
>>> m = [9, 15, 24]
Then we'll define a function modify() which appends to, and prints, the list passed to it. The function accepts a single formal argument named k:
>>> def modify(k):... k.append(39)... print("k =", k)...
We then call modify(), passing our list m as the actual argument:
>>> modify(m)k = [9, 15, 24, 39]
This indeed prints the modified list with four elements. But what does our list reference m outside the function now refer to?
>>> m[9, 15, 24, 39]
The list referred to by m has been modified ...
Read now
Unlock full access