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)

  1. A.

    (20, 50, 80, 30) 4, (4,)

  2. B.

    [20, 50, 80, 30] 4, (4,)

  3. C.

    (20, 50, 80, 30) (4,) 4

  4. D.

    [20, 50, 80, 30] (4,) 4

Attempted by 895 students.

Show answer & explanation

Correct answer: B

Correct output: [20 50 80 30] 4 (4,)

Explanation:

  • print(B) prints the NumPy array as [20 50 80 30]. NumPy displays arrays with square brackets and elements separated by spaces (no commas).

  • B.size is the total number of elements, which is 4.

  • B.shape is the shape of the 1-D array, which is (4,).

Combined, the print statement outputs the array, then the size, then the shape, separated by spaces: [20 50 80 30] 4 (4,).

Explore the full course: Bpsc