What is the result of the following code?
```python
import numpy as np
arr = np.array([1, 2, 3], dtype=np.float64)
print(arr.dtype)
```
Analysis & Theory
The dtype of the array is explicitly set to `np.float64`, so `arr.dtype` will return `float64`.
What will be the result of `arr.astype(np.int32)` for the following array?
```python
import numpy as np
arr = np.array([1.5, 2.5, 3.5])
print(arr.astype(np.int32))
```
Analysis & Theory
The `astype(np.int32)` function converts the float elements of the array to integers by truncating the decimal part, resulting in [1, 2, 3].
Which NumPy datatype is used to store complex numbers?
```python
import numpy as np
arr = np.array([1 + 2j, 3 + 4j])
print(arr.dtype)
```
Analysis & Theory
NumPy uses `complex128` to store complex numbers, which consists of a real and imaginary part, each represented by a 64-bit float.
What will be the result of `arr = np.array([1, 2, 3], dtype=np.uint8)`?
```python
import numpy as np
arr = np.array([1, 2, 3], dtype=np.uint8)
print(arr.dtype)
```
Analysis & Theory
`np.uint8` represents unsigned 8-bit integers, so the array's dtype will be `uint8`.
What is the output of `np.array([True, False], dtype=np.int32)`?
```python
import numpy as np
arr = np.array([True, False], dtype=np.int32)
print(arr)
```
Analysis & Theory
In NumPy, the boolean values `True` and `False` are converted to integers `1` and `0` when cast to `np.int32`. So, the result is [1, 0].
What will `np.array([1, 2, 3], dtype=np.complex128)` return?
```python
import numpy as np
arr = np.array([1, 2, 3], dtype=np.complex128)
print(arr)
```
C
C) [1.0 + 2j, 3.0 + 4j]
Analysis & Theory
`np.complex128` creates complex numbers with a real part and an imaginary part of 0. So the result is `[1+0j, 2+0j, 3+0j]`.
What is the output of `np.array([1, 2, 3], dtype=np.float32)`?
```python
import numpy as np
arr = np.array([1, 2, 3], dtype=np.float32)
print(arr.dtype)
```
Analysis & Theory
The `dtype=np.float32` explicitly specifies that the array should store 32-bit floating-point numbers, so the dtype will be `float32`.
What will the result of `np.array([1, 2, 3], dtype=np.int64)` be when we try to multiply the array by 2?
```python
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int64)
print(arr * 2)
```
Analysis & Theory
Multiplying a NumPy array of `dtype=int64` by a scalar (2) results in an array of integers, so the output will be `[2, 4, 6]`.
What will be the result of `arr = np.array([True, False], dtype=np.float64)`?
```python
import numpy as np
arr = np.array([True, False], dtype=np.float64)
print(arr)
```
Analysis & Theory
In NumPy, `True` is represented as `1.0` and `False` is represented as `0.0` when using `dtype=np.float64`. Therefore, the result is `[1.0, 0.0]`.
What is the default data type for NumPy arrays if no dtype is specified?
```python
import numpy as np
arr = np.array([1, 2, 3])
print(arr.dtype)
```
Analysis & Theory
If no dtype is specified, NumPy uses `int64` as the default data type for integer values on most systems.