hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    What will the following code result in? import numpy as np A = np.array([12, 23]) B = np.array([10, 20]) print(A - B)
    Question

    What will the following code result in?
    import numpy as np
    A = np.array([12, 23])
    B = np.array([10, 20])
    print(A - B)

    A.

    [-2 -3]

    B.

    [ [12 23]
    [10 20] ]

    C.

    [ [10 20]
    [12 23] ]

    D.

    [2 3]

    Correct option is A


    The code performs element-wise subtraction between two NumPy arrays, A and B. Here's the breakdown:
    · A = np.array([12, 23]): This creates the array A with elements [12, 23].
    · B = np.array([10, 20]): This creates the array B with elements [10, 20].
    · A - B: This performs element-wise subtraction between corresponding elements of the arrays A and B.
    So, for each index i:
    · 12 - 10 = 2
    · 23 - 20 = 3
    Thus, the result of A - B will be [2, 3].
    Important Key Points:
    1. Element-wise Operation: In NumPy, operations like subtraction are performed element-wise, meaning each corresponding pair of elements in A and B are operated on individually.
    2. Resulting Array: The result will be a new array of the same shape as A and B, with the corresponding subtracted values.
    3. Arrays with Same Shape: NumPy operations require that the arrays being operated on have the same shape, otherwise, they would throw an error (unless broadcasting rules apply).
    Knowledge Booster:
    · Option (b): This is incorrect because it represents a matrix-like structure, but the operation A - B is element-wise and results in a 1D array, not a 2D matrix.
    · Option (c): This is incorrect as well. The arrays A and B will not change positions and this does not match the result of element-wise subtraction.
    · Option (d): This is incorrect because the correct subtraction results in [2, 3], not [2, 3] as in a matrix form. The provided answer matches, but the formatting is incorrect as well.

    Free Tests

    Free
    Must Attempt

    CDP Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    EVS Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    CTET Paper-I PYP (07 July 2024)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    test-prime-package

    Access ‘EMRS PGT’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    289k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow