Numpy's array

Hello everyone, I’m in this exercise https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-3-introduction-to-numpy/modules/ida-3-1-numpy-syntax/lessons/numpy-introduction/exercises/numpy-arrays 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)
print(my_array)

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.

4 Likes

Further:

and if needed:
https://appdividend.com/2020/05/15/how-to-convert-numpy-array-to-list-in-python/

5 Likes