0
0
NumPydata~5 mins

Specifying dtype during creation in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does dtype mean when creating a NumPy array?

dtype stands for data type. It tells NumPy what kind of data the array will hold, like integers, floats, or strings.

Click to reveal answer
beginner
How do you create a NumPy array of integers with dtype specified?

You use the dtype parameter in np.array(). For example: np.array([1, 2, 3], dtype=int) creates an integer array.

Click to reveal answer
intermediate
Why is specifying dtype useful when creating arrays?

It helps control memory use and precision. For example, using float32 uses less memory than float64. It also avoids errors by ensuring data is the right type.

Click to reveal answer
beginner
What happens if you create a NumPy array without specifying dtype?

NumPy guesses the data type based on the input values. This usually works but might not be what you want, especially for mixed or precise data.

Click to reveal answer
beginner
Show how to create a NumPy array of strings with dtype specified.

Use dtype=str or dtype='U'. Example: np.array(['apple', 'banana'], dtype=str) creates an array of strings.

Click to reveal answer
What does specifying dtype=float32 do when creating a NumPy array?
AStores numbers as 32-bit floating point values
BStores numbers as 64-bit integers
CStores numbers as strings
DStores numbers as boolean values
If you create np.array([1, 2, 3]) without dtype, what type does NumPy choose?
AString
BInteger
CFloat
DBoolean
Which dtype would you use to store text data in a NumPy array?
Astr or 'U'
Bfloat
Cint
Dbool
Why might you want to specify dtype as float32 instead of float64?
ATo store boolean values
BTo use more memory
CTo store text data
DTo use less memory and speed up calculations
What happens if you specify dtype=int but provide float numbers when creating an array?
ANumPy stores the floats as floats
BNumPy throws an error
CNumPy converts floats to integers by truncating decimals
DNumPy stores them as strings
Explain why specifying dtype is important when creating NumPy arrays.
Think about how data type affects storage and calculations.
You got /4 concepts.
    Describe how to create a NumPy array of strings and why you might want to specify dtype in this case.
    Consider what happens if you mix strings and numbers.
    You got /4 concepts.