What will be the output of the following Python code ? import numpy as np…
2023
What will be the output of the following Python code ? import numpy as np B=np.array([20, 50, 80, 30]) print(B, B.size, B.shape)
- A.
(20, 50, 80, 30) 4, (4,) / (20, 50, 80, 30) 4, (4,)
- B.
[20 50 80 30] 4 (4,) / [20 50 80 30] 4 (4,)
- C.
(20, 50, 80, 30) (4,) 4 / (20, 50, 80, 30) (4,) 4
- D.
[20, 50, 80, 30] (4,) 4 / [20, 50, 80, 30] (4,) 4
Attempted by 882 students.
Show answer & explanation
Correct answer: B
Answer: [20 50 80 30] 4 (4,)
Printed output: [20 50 80 30] 4 (4,)
Why the array looks like that: Numpy arrays are displayed with square brackets and elements separated by spaces (no commas).
Meaning of B.size: It returns the total number of elements in the array, which is 4 for B.
Meaning of B.shape: It returns a tuple describing the array dimensions. For a 1-D array of length 4 it is (4,), where the trailing comma indicates a one-element tuple.