Saturday, 29 February 2020

CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)

Numpy Arrays (Part-1) - Question and Answers

CBSE Class 11 - Informatics Practices - Python Basics

CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)


Q1: The following statement has an error. Write the correct statement.
>>> import numpy as np
>>> a = np.array(1,2,3,4)

Answer: The second statement should be a = np.array([1,2,3,4]) to create an ndarray.


Q2: What is the output of the following program?

        import numpy as np
        list1 = [1,2,3,4,5]
        np1 = np.array(list1)
        print(type(np))
        print(type(np1))
        print(np1[2])
        print(np1.shape)


Answer:
<class 'module'>
<class 'numpy.ndarray'>
3
(5,)




CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)

Q3: Create an 1D array of 5 elements with random values in numPy.

Answer: empty( ) creates array filled with random numbers.

import numpy as np
arr1 = np.empty(5)
print(arr1)

eduvictors.com Q3: Create an 1D array of 5 elements with random values in numPy.


Q4: What will be the output of the following snippet?
import numpy as np
n1 = np.zeros(5)
print(n1)


Answer: Here, the zeros( ) creates an array (1D) of 5 elements filled with zero float values.

eduvictors.com: the zeros( ) creates an array (1D) of 5 elements filled with zero float values.


Q5: Write a python statement to create Numpy 1D array of five elements filled with zero integers.

Answer:
        n1 = np.zeros(5, dtype = np.int)

eduvictors.com Q5: Write a python statement to create Numpy 1D array of five elements filled with zero integers.

Q6: Write python statement to create Numpy 1D array of five elements having all ones.

Answer:
        n1 = np.ones(5)

eduvictors.com Write python statement to create Numpy 1D array of five elements having all ones.

Q7: Explain what do the following three statements do?
        import numpy as np
        n1 = np.full(5,9)
        print(n1)

Answer:
Statement 1: Imports numpy module.

Statement 2: Creates a numpy constant array (1 dimensional) of five elements. Each element has value 9.

Statement 3: displays values of n1 array i.e. [9.  9.   9.   9.   9 ]



CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)


Q8: What does the following state do?
        a1 = np.fromstring('1, 2, 3, 4', dtype=int, sep=',')

Answer: It creates 1D numpy integer array from the given string.


CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)



Q9: Consider the following ndarray creation, what will be the output of array2.dtype? What does it tell?
        array2 = np.array([5,-7.4,'a',7.2])
        print(array2.dtype)

Answer: Output is: 'U<32'
'U<32' indicates that there is a string value in the list, all values are promoted to string type i.e. Unicode-32 data type.


CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)


Q10: What is the purpose of arange( ) function? Explain by giving an example.

Answer:  arange() is a shorthand for arrayrange(). This function is analogous to the range() function of Python. aranage( ) returns an array with evenly spaced elements as per the interval.

Syntax is:
        arange([start,] stop[, step,][, dtype])
where
        start : [optional] start of interval range. By default start = 0
        stop : end of interval range
        step  : [optional] step size of interval. By default value is 1
        dtype: type of output array

e.g.,
        import numpy as np

        np.arange(5)             #creates an array of 5 elements is created with stop value 5 and step size 1 i.e. [0 1 2 3 4]

        np.arange(5.0)          # creates an array of floats with stop value 5.0 i.e. [0. 1. 2. 3. 4. ]

        print(np.arange(1,10))  #creates an array  with start value 1, stops at 9 and step size is 1

        print(np.arange(10, -10, -2))   # creates an array from 10 to -8 decremented by 2 i.e. [10  8  6  4  2  0  -2  -4  -6  -8]


CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)


Q11: Create an ndarray with start value -2, end value 24 and step size 4.

Answer: array1 = np.arange( -2, 24, 4 )


Q12: Are Numpy arrays mutable or not?

Answer: Mutable.

eduvictors.com: Numpy arrays are mutable


Q13: Write a program to copy existing NumPy array. 

Answer:
        import numpy as np
        x=np.array([1,2,3, 6, 10])
        y=x
        z=np.copy(x)
        print(x)
        print(y)
        print(z)

CBSE Class 11 - Informatics Practices - Python Basics - Numpy Arrays (Part-2) - Question and Answers (#CBSEclass11Python)(#cbse)(#eduvictors)



☞See also:

No comments:

Post a comment

We love to hear your thoughts about this post!