T1W5‐Tuples‐and‐Lists - tstorrnetnz/teaching2025 GitHub Wiki

Lists

Many of you will be familiar with lists in Python, so this section should mostly be familiar. Lists can have elements of the same or different types e.g. strings mixed with integers. Lists can also have other lists as elements - these are called nested lists.

Tuples

Whiles Tuple may be an odd word, Tuples are very, very useful. In its simplest firm, a tuple is group of data that can be referenced by a variable name.

The data in a tuple cannot be changed (unlike a list) and is said to be immutable. The data in a tuple can be of different types (e.g. strings, ints and even lists or other tuples). A simple tuple could be for a car:

car = ("Toyota", "Camry", "US9401", 1995, 302335, "silver", 1998)

For you to do:

The problems for you to do this week can be found below and the supporting material in Chapter 9 will help you revise tuples. Note that some of them do need you to have read Chapter 11, especially where the problems relate to how Python uses memory.

Chapter 11 excercises:

I suggest you create a single file containing the test scaffolding from our previous chapters, and put all functions that require tests into that file.

  1. What is the Python interpreter’s response to the following?

    
    >>> list(range(10, 0, -2))
    
    

    The three arguments to the range function above are start, stop, and step, respectively. In this example, start is greater than stop. What happens if start < stop and step < 0? Write a rule for the relationships among start, stop, and step.

  2. Consider this fragment of code:

    
    import turtle
    
    tess = turtle.Turtle()
    alex = tess
    alex.color("hotpink")
    
    

    Does this fragment create one or two turtle instances? Does setting the color of alex also change the color of tess? Explain in detail.

  3. Draw a state snapshot for a and b before and after the third line of the following Python code is executed:

    a = [1, 2, 3]
    b = a[:]
    b[0] = 5
    
    
  4. What will be the output of the following program?

    this = ["I", "am", "not", "a", "crook"]
    that = ["I", "am", "not", "a", "crook"]
    print("Test 1: {0}".format(this is that))
    that = this
    print("Test 2: {0}".format(this is that))
    
    

    Provide a detailed explanation of the results.

  5. Lists can be used to represent mathematical vectors. In this exercise and several that follow you will write functions to perform standard operations on vectors. Create a script named vectors.py and write Python code to pass the tests in each case.

    Write a function add_vectors(u, v) that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each:

    
    test(add_vectors([1, 1], [1, 1]) == [2, 2])
    test(add_vectors([1, 2], [1, 4]) == [2, 6])
    test(add_vectors([1, 2, 1], [1, 4, 3]) == [2, 6, 4])
    
    
  6. Write a function scalar_mult(s, v) that takes a number, s, and a list, v and returns the scalar multiple of v by s:

    
    test(scalar_mult(5, [1, 2]) == [5, 10])
    test(scalar_mult(3, [1, 0, -1]) == [3, 0, -3])
    test(scalar_mult(7, [3, 0, 5, 11, 2]) == [21, 0, 35, 77, 14])
    
    
  7. Write a function dot_product(u, v) that takes two lists of numbers of the same length, and returns the sum of the products of the corresponding elements of each (the dot_product).

    
    test(dot_product([1, 1], [1, 1]) ==  2)
    test(dot_product([1, 2], [1, 4]) ==  9)
    test(dot_product([1, 2, 1], [1, 4, 3]) == 12)
    
    
  8. Extra challenge for the mathematically inclined: Write a function cross_product(u, v) that takes two lists of numbers of length 3 and returns their cross product. You should write your own tests.

  9. Describe the relationship between " ".join(song.split()) and song in the fragment of code below. Are they the same for all strings assigned to song? When would they be different?

    song = "The rain in Spain..."
    
    
  10. Write a function replace(s, old, new) that replaces all occurrences of old with new in a string s:

    
    test(replace("Mississippi", "i", "I") == "MIssIssIppI")
    
    s = "I love spom! Spom is my favorite food. Spom, spom, yum!"
    test(replace(s, "om", "am") ==
        "I love spam! Spam is my favorite food. Spam, spam, yum!")
    
    test(replace(s, "o", "a") ==
        "I lave spam! Spam is my favarite faad. Spam, spam, yum!")
    
    

Hint: use the split and join methods.

  1. Suppose you want to swap around the values in two variables. You decide to factor this out into a reusable function, and write this code:
    
    def swap(x, y):      # Incorrect version
        print("before swap statement: x:", x, "y:", y)
        (x, y) = (y, x)
        print("after swap statement: x:", x, "y:", y)
    
    a = ["This", "is", "fun"]
    b = [2,3,4] 
    print("before swap function call: a:", a, "b:", b)
    swap(a, b)
    print("after swap function call: a:", a, "b:", b)
    
    

Run this program and describe the results. Oops! So it didn’t do what you intended! Explain why not. Using a Python visualizer like the one at http://pythontutor.com may help you build a good conceptual model of what is going on. What will be the values of a and b after the call to swap?