Importing NumPy
By convention everybody imports NumPy under the short name np, so code looks the same in every tutorial and project.
In this page:
Syntax
import numpy as np
Importing NumPy
The standard import is import numpy as np. The alias np is a near-universal convention that keeps code short and readable. Once imported, every NumPy function is available as np.something.
Note:
Avoid
from numpy import *; it overwrites built-ins like sum, min and max.Example: Importing NumPy
import numpy as np
print("NumPy version is a string:", isinstance(np.__version__, str))
print("Pi from NumPy:", np.pi)
print("Square root of 16:", np.sqrt(16))
# Output:
# NumPy version is a string: True
# Pi from NumPy: 3.141592653589793
# Square root of 16: 4.0
Related Topics
Common Mistakes
- Using
from numpy import *and shadowing Python built-ins - Using a different alias than np and confusing teammates
- Forgetting to import before calling np functions
Chapter Summary
- Use import numpy as np
- np is the universal alias
- Avoid star imports
- Check np.__version__ to confirm the install
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: