Numpy's array

Hello everyone, I’m in this exercise of Numpy. And I was experimenting with the code. I tried this :

import numpy as np
my_list = [1, 2, 3, 4, 5, 6,9.45,'foo']
my_array = np.array(my_list)

If you remove the string, then all the elements become a floating-point number, and with the string, the elements change into a string. But the lesson mentioned that arrays can be of any type. But Numpy makes sure that everything is of the same type. In general, aren’t arrays supposed to be of the same type ?? And I’m also confused between a list and an array.

Array = Contiguous memory
List = objects scattered in memory, each object has the address of the next object
There are advantages/disadvantages to each - but that gets into computer science.
Realise that there is a difference between numpy and python. Numpy is a library and meant to behave that way - doesn’t mean that it is the golden rule or convention. Some libraries/languages will require arrays all be of one type, others not so much.



and if needed:


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.