Samuli
0
Q:

python declare array of size n

array1 = [] #an empty array
array2 = list() #another empty arrat
array3 = ["1", "2", "3"] #array initialization with value
arrat4 = [x for x in range(1,11) #list comprehension
0
>>> n = 5                     #length of list
>>> list = [None] * n         #populate list, length n with n entries "None"
>>> print(list)
[None, None, None, None, None]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, None, None, 1]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, None, 1, 1]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, 1, 1, 1]
0
numpy.array([0] * n) #creates an int64 numpy array of size n with each element having a vaule of 0
numpy.array([0] * n, dtype = '<type>') #creates a numpy array of size n with a type of <type>
0

New to Communities?

Join the community