Tuples
Tuples are just like lists, except they are immutable. The lack of mutability affects programs in more ways than limiting the methods that can be used. In this activity you will explore how list and tuple references are different.
For each of the following, create reference diagram and specify what the program would print if run.
-
a_list = [1, 0, 1, 1, 0] print(a_list.count(1))
-
a_tuple = (1, 0, 1, 1, 0) print(a_tuple.count(1))
-
a_list = [1, 2, 3] del a_list[0] print(a_list)
-
a_tuple = (1, 2, 3) del a_tuple[0] print(a_tuple)
-
a_tuple = (1, 2) b_tuple = a_tuple b_tuple += (, 3) print(a_tuple) print(b_tuple)
-
a_list = [1, 2] b_list = a_list b_list.append(3) print(a_list) print(b_list)
-
a_list = [1, 2] b_list = a_list b_list += [3] print(a_list) print(b_list)