# We wrote and ran this in class, to compare # mutable and immutable types. def fun1(x): x = x + 1 return x def fun2(x): x.append(1) y = 3 z = fun1(y) print("y",y,"z",z) w = [1,2,3] fun2(w) print("w",w) # Shell >>> help(str.split) Help on method_descriptor: split(...) S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. >>> x = "My~Dog~Ate~Fleas" >>> x.split("~") ['My', 'Dog', 'Ate', 'Fleas'] >>> "23,45,23,12,66" '23,45,23,12,66' >>> x = "23,45,23,12,66" >>> x '23,45,23,12,66' >>> x.split(",") ['23', '45', '23', '12', '66'] >>> line = "3 5 55 1\n" >>> numStrs = line.split() >>> numStrs ['3', '5', '55', '1'] >>> # add them up >>> total = 0 >>> for i in numStrs: total += int(i) >>> total 64 >>> help(sum) Help on built-in function sum in module builtins: sum(...) sum(iterable[, start]) -> value Returns the sum of an iterable of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the iterable is empty, returns start. >>> #iterable >>> #range string list >>> sum(range(2,5)) 9 >>> sum("hello") Traceback (most recent call last): File "", line 1, in sum("hello") TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> sum([2,3,4,5]) 14 >>> x '23,45,23,12,66' >>> numStrs ['3', '5', '55', '1'] >>> intVals = [] >>> for i in numStrs: intVals.append(int(i)) >>> intVals [3, 5, 55, 1] >>> intVals = [] >>> for i in numStrs: print("i",i,"intVals",intVals) intVals.append(int(i)) i 3 intVals [] i 5 intVals [3] i 55 intVals [3, 5] i 1 intVals [3, 5, 55] >>> #is append a function or a method? a method! >>> x = [] >>> x.append(1) >>> x [1] >>> help(list.append) Help on method_descriptor: append(...) L.append(object) -> None -- append object to end >>> x.append(54) >>> x [1, 54] >>> x.append([3,4,5]) >>> x [1, 54, [3, 4, 5]] >>> x[0] 1 >>> x[1] 54 >>> x[2] [3, 4, 5] >>> y = ["he","there"] >>> x.append(y) >>> x [1, 54, [3, 4, 5], ['he', 'there']] >>> x[3] ['he', 'there'] >>> x.append([33,44,55,66,7,8]) >>> x [1, 54, [3, 4, 5], ['he', 'there'], [33, 44, 55, 66, 7, 8]] >>> len(x[4]) 6 >>> x[4] [33, 44, 55, 66, 7, 8] >>> x[4][0] 33 >>> x[4][2] 55 >>> x.append([]) >>> x [1, 54, [3, 4, 5], ['he', 'there'], [33, 44, 55, 66, 7, 8], []] >>> x[5] [] >>> len(x[5]) 0 >>> x = [[[3,4,5],[4,6,8]]] >>> x[0] [[3, 4, 5], [4, 6, 8]] >>> len(x) 1 >>> x[0][1] [4, 6, 8] >>> x[0][1][2] 8 >>> # mutable and immutable objects >>> x = 3 >>> #ints and strings are immutable >>> x = 5 >>> s = "hello" >>> s = "there" >>> # lists are mutable >>> x = [0,1,2,3] >>> x[2] = 33 >>> x [0, 1, 33, 3] >>> x.append(55) >>> x [0, 1, 33, 3, 55] >>> #since strings are immutable this is an error: >>> s = "hello" >>> x[2] = "m" >>> s[2] = "m" Traceback (most recent call last): File "", line 1, in s[2] = "m" TypeError: 'str' object does not support item assignment >>> a = 19 >>> b = a >>> a = 42 >>> print(a,b) 42 19 >>> a = [0,1,2,3] >>> b = a >>> a[2] = 4 >>> print(a,b) [0, 1, 4, 3] [0, 1, 4, 3]