Index Error when creating Numpy Array from For Loop

The below code excerpt produces the following error at the last line

IndexError: list assignment index out of range

I’m expecting that w is an array with 3 values in it, w[0], w[1], w[2]. Do you see my error?

import numpy as np
n = 7
q = 0.02
pnum = 0
pden = 0
w = [(n - 1) // 2]
for i in range((n - 1) // 2):
    for m in range(5):
        pnum += (-1)**m * q**(m*(m+1)) * np.sin((2*m+1) * np.pi * i / n)
        pden += (-1)**(m+1) * q**((m+1)**2) * np.cos(2 * (m+1) * np.pi * i / n)
    num = 2 * q**(0.25) * pnum
    den = 1 + 2 * pden
    w[i]= num / den

Answer

This line w = [(n – 1) // 2] Is creating a list of size 1 with the integer value 3 in it. You should review the way you created the array or create it empty and use the function append.