CPSC120A
Fundamentals of Computer Science I

Activity 28

Tuples

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.

  1. a_list = [1, 0, 1, 1, 0]
    print(a_list.count(1))
    
  2. a_tuple = (1, 0, 1, 1, 0)
    print(a_tuple.count(1))
    
  3. a_list = [1, 2, 3]
    del a_list[0]
    print(a_list)
    
  4. a_tuple = (1, 2, 3)
    del a_tuple[0]
    print(a_tuple)
    
  5. a_tuple = (1, 2)
    b_tuple = a_tuple
    b_tuple += (, 3)
    print(a_tuple)
    print(b_tuple)
    
  6. a_list = [1, 2]
    b_list = a_list
    b_list.append(3)
    print(a_list)
    print(b_list)
    
  7. a_list = [1, 2]
    b_list = a_list
    b_list += [3]
    print(a_list)
    print(b_list)