Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
a = [1, 2, 3]
b = np.array([1, 2, 3])
print(len(a * 2), len(b * 2))6 3
- Expecting
3for the list:a * 2repeats a Python list, so it holds 6 elements; only a numpy array multiplies each element. - Expecting
6for the array:b * 2multiplies each element of a numpy array, so the result[2, 4, 6]keeps length 3.
- What do
a + aandb + bgive here, and why do they differ? - What happens when you multiply the list
aby1.5, and the arraybby1.5?