You're recording daily temperatures for a week: 72, 75, 68, 71, 73, 76, 74. Rather than creating 7 separate variables (day1, day2, ...), you store them in one list and access each by its position.

Create a list with values

Declare a list and fill it with initial values.

nums
create.py
Replay: real traced execution (multi-file project)
nums = [10, 20, 30, 40, 50]

print("nums=" + str(nums))
first = nums[0]
second = nums[1]
last = nums[4]
nums = [5, 10, 15, 20, 25]

print("nums=" + str(nums))
first = nums[0]
second = nums[1]
last = nums[4]
nums = [1, 1, 2, 3, 5]

print("nums=" + str(nums))
first = nums[0]
second = nums[1]
last = nums[4]
  1. nums ← [10, 20, 30, 40, 50], first ← 10, second ← 20, last ← 50

    1nums→ [10, 20, 30, 40, 50] = [10, 20, 30, 40, 50]  #@nums=[5, 10, 15, 20, 25], [1, 1, 2, 3, 5]23print("nums=" + str(nums[10, 20, 30, 40, 50]))4first→ 10 = nums[0]105second→ 20 = nums[1]206last→ 50 = nums[4]50
    outputnums=[10, 20, 30, 40, 50]
  1. nums ← [5, 10, 15, 20, 25], first ← 5, second ← 10, last ← 25

    1nums→ [5, 10, 15, 20, 25] = [5, 10, 15, 20, 25]23print("nums=" + str(nums[5, 10, 15, 20, 25]))4first→ 5 = nums[0]55second→ 10 = nums[1]106last→ 25 = nums[4]25
    outputnums=[5, 10, 15, 20, 25]
  1. nums ← [1, 1, 2, 3, 5], first ← 1, second ← 1, last ← 5

    1nums→ [1, 1, 2, 3, 5] = [1, 1, 2, 3, 5]23print("nums=" + str(nums[1, 1, 2, 3, 5]))4first→ 1 = nums[0]15second→ 1 = nums[1]16last→ 5 = nums[4]5
    outputnums=[1, 1, 2, 3, 5]

The list holds 5 numbers. We can print each one by its position. Position starts at 0, not 1 - this is called zero-based indexing.

list A container holding elements in order: `[10, 20, 30]`. Can grow or shrink.

Get elements by index

Access any element using its position number.

get.py
Replay: real traced execution (multi-file project)
scores = [85, 92, 78, 95, 88]

print("scores=" + str(scores))
first = scores[0]
third = scores[2]
last = scores[4]
  1. scores ← [85, 92, 78, 95, 88], first ← 85, third ← 78, last ← 88

    1scores→ [85, 92, 78, 95, 88] = [85, 92, 78, 95, 88]23print("scores=" + str(scores[85, 92, 78, 95, 88]))4first→ 85 = scores[0]855third→ 78 = scores[2]786last→ 88 = scores[4]88
    outputscores=[85, 92, 78, 95, 88]
index Position in the list. First element is index 0, second is index 1, etc.
len `len(nums)` gives the number of elements in the list.

Put (change) elements

Modify values at specific positions.

put.py
Replay: real traced execution (multi-file project)
nums = [1, 2, 3, 4, 5]

print("before: nums=" + str(nums))
nums[0] = 100
nums[2] = 300
print("after: nums=" + str(nums))
  1. nums ← [1, 2, 3, 4, 5], nums[0] ← 100, nums[2] ← 300

    1nums→ [1, 2, 3, 4, 5] = [1, 2, 3, 4, 5]23print("before: nums=" + str(nums[1, 2, 3, 4, 5]))4nums[0]→ 100 = 1005nums[2]→ 300 = 3006print("after: nums=" + str(nums[100, 2, 300, 4, 5]))
    outputbefore: nums=[1, 2, 3, 4, 5]
    after: nums=[100, 2, 300, 4, 5]

Lists can be modified after creation. Assign a new value to any position.

See List Mutation

A list update changes one slot while the other slots stay in place. These diagrams pin the exact put.py and swap.py values.

List slots after each assignmentList slots after each assignmentstep01234start12345nums[0]=1001002345nums[2]=300100230045
`nums[0] = 100` changes only index 0. `nums[2] = 300` then changes only index 2, producing `[100, 2, 300, 4, 5]`.
Temporary variable keeps one value safeTemporary variable keeps one value safe[10,20,30,40,50]temp=10nums[0]=50nums[4]=10[50,20,30,40,10]
The swap saves `nums[0]` in `temp`, copies `nums[4]` into index 0, then writes `temp` into index 4.

Fibonacci with lists

Store the entire Fibonacci sequence in one structure.

fibonacci.py
Replay: real traced execution (multi-file project)
fib = [0] * 8

fib[0] = 0
fib[1] = 1
fib[2] = fib[0] + fib[1]
fib[3] = fib[1] + fib[2]
fib[4] = fib[2] + fib[3]
fib[5] = fib[3] + fib[4]
fib[6] = fib[4] + fib[5]
fib[7] = fib[5] + fib[6]

print("fib=" + str(fib))

  1. fib ← [0, 0, 0, 0, 0, 0, 0, 0], fib[0] ← 0, fib[1] ← 1, fib[2] ← 1

    1#?list_multiply2fib→ [0, 0, 0, 0, 0, 0, 0, 0] = [0] * 834fib[0]→ 0 = 05fib[1]→ 1 = 16fib[2]→ 1 = fib[0]0 + fib[1]17fib[3]→ 2 = fib[1]1 + fib[2]18fib[4]→ 3 = fib[2]1 + fib[3]29fib[5]→ 5 = fib[3]2 + fib[4]310fib[6]→ 8 = fib[4]3 + fib[5]511fib[7]→ 13 = fib[5]5 + fib[6]81213print("fib=" + str(fib[0, 1, 1, 2, 3, 5, 8, 13]))
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13]

Much cleaner than having c0, c1, c2... as separate variables.

Swap two elements

Exchange the first and last elements of a list.

swap.py
Replay: real traced execution (multi-file project)
nums = [10, 20, 30, 40, 50]

print("before: nums=" + str(nums))
temp = nums[0]
nums[0] = nums[4]
nums[4] = temp
print("after: nums=" + str(nums))
  1. nums ← [10, 20, 30, 40, 50], temp ← 10, nums[0] ← 50, nums[4] ← 10

    1nums→ [10, 20, 30, 40, 50] = [10, 20, 30, 40, 50]23print("before: nums=" + str(nums[10, 20, 30, 40, 50]))4temp→ 10 = nums[0]105nums[0]→ 50 = nums[4]506nums[4]→ 10 = temp107print("after: nums=" + str(nums[50, 20, 30, 40, 10]))
    outputbefore: nums=[10, 20, 30, 40, 50]
    after: nums=[50, 20, 30, 40, 10]

We need a temporary variable to hold one value during the swap. Without it, we'd lose one of the values.