Using NumPy from Mojo
Call NumPy arrays and functions.
Using NumPy from Mojo is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
NumPy, From Mojo
NumPy powers most Python number-crunching, and Mojo lets you call it directly, so you keep that ecosystem while writing fast code. 🔢
Import It First
Bring NumPy in the same way you import any Python module: call Python.import_module with the name "numpy".
var np = Python.import_module("numpy")Make an Array
With the handle in hand, call NumPy functions through dot access. Here you build an array from a Python list of numbers.
var a = np.array([1, 2, 3])Use Array Functions
Every NumPy creator works: np.zeros and np.ones build filled arrays, perfect for setting up buffers before computation.
var z = np.zeros(5)Math on Arrays
Call NumPy math functions just like in Python. np.sum adds every element of an array and hands back the total.
var total = np.sum(a)Whole-Array Ops
NumPy shines at vectorized math: one call like np.sqrt works on the entire array at once, no manual loop needed.
var roots = np.sqrt(a)Reading Shape
Access an array's attributes through dot syntax. The shape attribute tells you the dimensions of your array.
var dims = a.shapeIt May Raise
Calls across the bridge can fail, so NumPy work belongs in a function marked raises to handle any error cleanly.
fn run() raises:
var np = Python.import_module("numpy")Same API You Know
The NumPy API is identical to Python's: if you know np.linspace or np.dot, you already know how to call them from Mojo.
var grid = np.linspace(0, 1, 10)Build, Then Use
A common pattern is to create an array, then reshape it. np.reshape reorganizes the same data into new dimensions for you.
var m = np.reshape(a, (3, 1))Best of Both
You get NumPy's mature, battle-tested routines while orchestrating them from Mojo, blending convenience with Mojo's performance mindset. ⚡
Quick Check
Recall how you start using NumPy inside a Mojo program.
Recap
You imported NumPy with Python.import_module, then built arrays and ran vectorized math using the very same API you know from Python. 🎯
Frequently asked questions
Is the “Using NumPy from Mojo” lesson free?
Yes — the full text of “Using NumPy from Mojo” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Using NumPy from Mojo”?
Call NumPy arrays and functions. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using NumPy from Mojo” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using NumPy from Mojo
- Passing Mojo Data to Python
- Reading Python Objects Back
- Interop Performance Gotchas