Dec-23-2018, 08:36 PM
The first answer is a little tricky. Lists are mutable types. One consequence of this is that if you modify a list you passed to a function, the list outside will be modified also. But you didn't modify the list. You reassigned the variable name of the parameter to a new list. The variable name exists only inside the function, and has no affect outside of it.
Note that my_list + [4, 5] is almost the same as my_list.extend([4, 5]). The difference is that the addition creates a new list you can reassign, while the second modifies the original list in place.
The second question is easier. When you print a function call, you are printing the return value of that function. Since you didn't specify a return value with a return statement, you get the default return value of None. If you had ended your function with
Note that my_list + [4, 5] is almost the same as my_list.extend([4, 5]). The difference is that the addition creates a new list you can reassign, while the second modifies the original list in place.
The second question is easier. When you print a function call, you are printing the return value of that function. Since you didn't specify a return value with a return statement, you get the default return value of None. If you had ended your function with
return my_list
, printing the function call would have printed the modified list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures