Playing as a guest? to earn XP and track your progress.
🧮 Introduction to NumPy Quiz
🧮 Introduction to NumPy
What is the full form of NumPy?
Numerical Python
New Python
Number Python
Numeric Program
NumPy stands for Numerical Python.
NumPy is a:
Database
Operating system
Programming language
Library (package) for Python
NumPy is a free, open-source library added to Python for fast numerical work.
What is the main object (array) of NumPy called?
series
ndarray
list
matrix
NumPy's array object is called ndarray.
ndarray stands for:
nested array
n-dimensional array
new data array
numbered array
An ndarray can have one, two or more dimensions.
Who created NumPy?
Charles Babbage
Wes McKinney
Guido van Rossum
Travis Oliphant
Travis Oliphant created NumPy in 2005.
In which year was NumPy created?
1995
1991
2005
2015
NumPy was created in 2005.
Which command installs NumPy?
import numpy
install numpy as np
pip install numpy
python numpy
pip is Python's package installer and is typed at the command prompt.
Where is the command pip install numpy typed?
Inside the Python Shell after >>>
Inside a print() statement
At the command prompt / terminal
In a NumPy array
pip is a command-prompt command, not a Python statement.
Which statement imports NumPy with its usual alias?
import np as numpy
include numpy
from np import numpy
import numpy as np
np is the standard alias (nickname) for numpy.
In import numpy as np, np is called a(n):
Module file
Array
Alias
Keyword
An alias is a short name used in place of numpy.
What error do you get if you use np.array([1, 2]) without importing NumPy?
IndexError
TypeError
NameError
ValueError
Python doesn't know what np is, so it gives NameError: name 'np' is not defined.
What error appears for import numpy if NumPy is NOT installed?
TypeError
SyntaxError
NameError
ModuleNotFoundError
If the package is missing, Python cannot find the module.
Which of these is a reason to use NumPy?
Arrays take less memory
Maths works on all elements without a loop
Arrays are faster than lists for large numerical data
All of the above
NumPy arrays are fast, compact and support element-wise operations.
Which libraries studied in Class 12 are built on NumPy?
Pandas and Matplotlib
MySQL and Tkinter
Turtle and Pygame
Random and Math
Pandas and Matplotlib are built on top of NumPy.
An array is a collection of elements that are:
Stored randomly in memory
Always strings
Of the same data type and stored contiguously in memory
Of different data types
All elements share one type and are stored side by side.
What does 'contiguous' mean for array elements?
Stored next to each other in memory
Stored in random order
Stored only on the hard disk
Stored in different files
Contiguous means side by side, with no gaps.
Indexing of a NumPy array starts from:
Any number
-1
0
1
Like lists, array indexes start at 0.
A 1-D array is like:
A single row of elements
A nested dictionary
A table with rows and columns
A dictionary
A one-dimensional array is a single row.
A 2-D array has:
Rows and columns, like a table
Only one row
Keys and values
Only one column always
A two-dimensional array is like a table.
Which is TRUE about a Python list?
It can hold elements of different data types
It supports element-wise maths
It always takes less memory than an array
It needs import numpy
A list can mix types, e.g. [1, 'a', 2.5].
Which is TRUE about a NumPy array?
It is built into Python without import
All its elements are of the same data type
It can mix types freely
It is slower than a list for large data
An array holds one data type only.
Which uses LESS memory for a large amount of numbers?
String
NumPy array
Python list
Both use the same
Arrays store data compactly.
How does a NumPy array print?
With commas, like [10, 20, 30]
Without commas, like [10 20 30]
In curly brackets
As a tuple
NumPy prints arrays with spaces between elements.
What is the output?
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr)(10 20 30 40)
Error
[10 20 30 40]
[10, 20, 30, 40]
np.array() converts the list into an array, printed without commas.
What is the output?
import numpy as np
arr = np.array([1, 2, 3])
print(type(arr))<class 'numpy.ndarray'>
<class 'list'>
<class 'numpy.array'>
<class 'array'>
A NumPy array is of type numpy.ndarray.
Which function creates a NumPy array from a list?
array.np()
np.create()
np.array()
np.list()
np.array(list) converts a list into an array.
What is the output?
import numpy as np
L = [10, 20, 30]
print(L * 2)[10, 20, 30, 10, 20, 30]
[20 40 60]
[20, 40, 60]
Error
For a list, * 2 repeats the list.
What is the output?
import numpy as np
A = np.array([10, 20, 30])
print(A * 2)[10, 20, 30, 10, 20, 30]
[20 40 60]
[20, 40, 60]
[10 20 30 10 20 30]
For an array, * 2 multiplies every element (element-wise).
What is the output?
import numpy as np
A = np.array([10, 20, 30])
print(A + 5)[10 20 30 5]
[15, 25, 35]
[15 25 35]
Error
5 is added to every element.
What does [10, 20, 30] + 5 give for a Python LIST?
[10, 20, 30, 5]
TypeError
65
[15, 25, 35]
A number cannot be added to a list.
Doing an operation on every element of an array at once, like A + 5, is called:
Concatenation
Repetition
Slicing
Element-wise operation
NumPy applies the operation to each element without a loop.
What is the output?
import numpy as np
marks = [45, 78, 92, 60]
print(np.array(marks) + 5)Error
[50, 83, 97, 65]
[45 78 92 60 5]
[50 83 97 65]
Grace marks: 5 is added to every element.
What is the output?
import numpy as np
a = np.array([1, 2.5, 3])
print(a)[1. 2.5 3. ]
[1, 2.5, 3]
[1 2.5 3]
[1 2 3]
Because of 2.5, all elements become floats (upcasting).
What is the output?
import numpy as np
b = np.array([7, "x", 9])
print(b)[7, 'x', 9]
[7 'x' 9]
Error
['7' 'x' '9']
One string makes every element a string.
What is the output?
import numpy as np
c = np.array([True, 2, 3])
print(c)[True 2 3]
[1 2 3]
Error
[1.0 2.0 3.0]
True becomes 1 when mixed with integers.
NumPy automatically converting all elements to a common type is called:
Slicing
Downloading
Upcasting
Indexing
Mixed values are converted to the 'strongest' type present.
What is the order of 'strength' used in upcasting?
string → float → int → bool
int → bool → string → float
float → int → bool → string
bool → int → float → string
If one element is further along this order, all elements become that type.
np.array([1, 2, 3.0]) — what will be the type of all its elements?
float
string
int
bool
One float makes all elements floats.
Which argument of np.array() is used to choose the data type?
datatype
dtype
type
kind
Example: np.array([1, 2, 3], dtype=float).
What is the output?
import numpy as np
a = np.array([1, 2, 3], dtype=float)
print(a)Error
[1 2 3]
[1.0, 2.0, 3.0]
[1. 2. 3.]
dtype=float stores every element as a float.
What is the output?
import numpy as np
b = np.array([1.9, 2.7, -3.5], dtype=int)
print(b)[ 1 2 -3]
[1.9 2.7 -3.5]
[ 2 3 -4]
Error
dtype=int cuts off the decimal part; it does not round.
Which attribute shows the data type of an existing array arr?
type(arr)
arr.dtype
arr.type
arr.datatype
arr.dtype shows the type of the elements, e.g. int64 or float64.
What is the output?
import numpy as np
print(np.array([1.5, 2.5]).dtype)float
int64
float64
<class 'float'>
Decimal numbers are stored as float64.
What does np.array(1, 2, 3) give?
[1, 2, 3]
TypeError
[1 2 3]
3
np.array() expects one list; the values must be inside [ ].
A 2-D array is created from:
A dictionary
A string
Two separate print statements
A nested list (list of lists)
Each inner list becomes one row.
What is the output?
import numpy as np
t = np.array([[1, 2], [3, 4]])
print(t[1])2
[1 2]
[[3 4]]
[3 4]
t[1] is the second row.
How many rows and columns does np.array([[2.4, 3], [4.91, 7], [0, -1]]) have?
3 rows and 2 columns
1 row and 6 columns
2 rows and 3 columns
6 rows and 1 column
There are 3 inner lists (rows), each with 2 values (columns).
Why does np.array([[1, 2], [3]]) give an error?
Nested lists are not allowed
3 is not allowed
Arrays cannot have 2 rows
All rows of a 2-D array must have the same length
The first row has 2 elements and the second only 1.
What error does np.array([[1, 2], [3]]) give in current NumPy?
No error
IndexError
ValueError
TypeError
Rows of different lengths give ValueError: setting an array element with a sequence...
What is the output?
import numpy as np
arr = np.array([10, 25, 40], dtype=float)
print(arr * 2)[20. 50. 80.]
Error
[10. 25. 40. 10. 25. 40.]
[20 50 80]
Each float element is doubled.
What is the output?
import numpy as np
subjects = np.array(["CS", "IP", "Maths"])
print(subjects)Error
['CS', 'IP', 'Maths']
[CS IP Maths]
['CS' 'IP' 'Maths']
String arrays print with quotes and no commas.
What is the output?
import numpy as np
A = np.array([2, 4, 6])
print(A[0] + A[2])8
Error
6
[2 6]
A[0] is 2 and A[2] is 6.
Which statement correctly creates a NumPy array from list L = [5, 10, 15, 20]?
arr = array.np(L)
arr = np(L)
arr = np.array(L)
arr = np.array[L]
np.array() is a function, so the list goes inside round brackets.
What is the output?
import numpy as np
print(np.array([5, 10, 15, 20]))[ 5 10 15 20]
Error
[5, 10, 15, 20]
[5 10 15 20]
NumPy pads numbers to the same width, so 5 gets a leading space.
Which statement is FALSE?
Array operations are element-wise
A list is built into Python
An array needs import numpy
A NumPy array can store a mix of int and str values as they are
Mixed int and str values are all converted to strings in an array.
A list is like a school bag and a NumPy array is like an egg tray. What does the egg tray show?
It has no order
It cannot be changed
Every slot holds the same kind of item, neatly in a row
It can hold anything
Arrays hold one type of element, stored side by side.