Python 3D Academyবাংলা

Memory as a row of numbered boxes

🌍 In real life. At Kamalapur station the luggage lockers stand in one long row, numbered 0, 1, 2, … from the left, every locker the same width. If the row starts 100 cm from the wall and each locker is 40 cm wide, locker 7 is at 100 + 7 × 40 = 380 cm. You walk straight to it: you never need to open lockers 0 to 6 first.

That is exactly how a computer finds an item in an array, and a Python list is built on one.

The idea in one sentence: an array keeps its items in one unbroken block of memory, side by side, each taking the same space, so the place of item i can be worked out instead of searched for.

Its words:

  • array: items stored side by side in one block of memory, each taking the same space
  • address: the number of one place in memory
  • index: an item's position counted from 0; the item sits at start + index × size
  • random access: reaching any item in one step by working out its address, without walking from the start

How it works:

  1. memory is a long row of numbered places (addresses)
  2. an array starts at some address start, and each item takes size places
  3. item i is at start + i × size: one multiply and one add, whatever i is
  4. that is why counting starts at 0: the first item is 0 steps from the start

What it costs: reading or writing items[i] is O(1): the same work for item 0 and item 1,000,000. (A Python list stores a small reference in each place, pointing at the actual value, so every place has the same size even when the values don't.)

def address(start, i, size):
    return start + i * size

lockers = ["bag", "box", "trunk", "tiffin", "guitar"]
print(address(100, 3, 40), "cm: locker 3 holds", lockers[3])
print(lockers[0], lockers[-1])   # the first, and the last (counting back from the end)
lockers[1] = "parcel"            # writing is O(1) too
print(lockers)

Mistakes and edge cases. items[len(items)] is one past the end: IndexError: list index out of range. The last item is items[len(items) - 1], or simply items[-1]. And an empty list has no items[0] at all, so check if items: first.

Quiz

  1. The array starts at address 200 and each item takes 8 places. What does this print?

    start, size = 200, 8
    print(start + 5 * size)
  2. Why is reading items[999999] as fast as reading items[0]?

    • Its place is worked out with one multiply and add, not searched for
    • Python remembers which items you read most
    • It isn't: later items take longer
  3. What does this print?

    items = [10, 20, 30, 40]
    print(items[-1], items[len(items) - 2])
  4. Why do array positions start at 0?

    • The position is how many items you skip from the start, and the first skips none
    • 0 is easier to type
    • Python's first item is always empty
  5. Which line has a mistake?

    lockers = ["bag", "box", "tiffin"]
    last = lockers[len(lockers)]
    print(last)

Coding tasks

  1. Write address(start, i, size): where item i of an array is, if the array starts at start and every item takes size places.

  2. Write swap_ends(items) in O(1) time: swap the first and the last item in place and give back the list (an empty list stays empty).