011. Installing NumPy
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
If you already have Python, you can install NumPy with:
pip install numpy
If you are using Anaconda, you can use:
conda install numpy
In cloud environments like Google Colab or Kaggle, NumPy is pre-installed. You can just open a notebook and start coding.
022. The `np` Alias
In the Python data science community, NumPy is always imported with the alias np:
import numpy as np
This is a universal standard. While you could technically import it as n or num, using np ensures your code is readable and instantly recognizable to any other developer or data scientist.
033. Creating Your First Array
The fundamental object in NumPy is the ndarray (n-dimensional array). You create one by passing a Python list (or tuple) into the np.array() function.
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(type(arr)) # <class 'numpy.ndarray'>
`
?Frequently Asked Questions
How do I check my NumPy version?
You can check your installed version by printing `np.__version__` after importing the library. This is useful for debugging compatibility issues with other libraries like Pandas or TensorFlow.
Can I use a tuple instead of a list in np.array()?
Yes! The `np.array()` function accepts any array-like object. You can pass a list, a tuple, or even another array, and NumPy will convert it into an `ndarray`.
Why do I get a ModuleNotFoundError for NumPy?
This error means NumPy is not installed in the Python environment you are currently running. Ensure you ran `pip install numpy` in the correct virtual environment or terminal.
