0Pricing
Mojo Academy · Lezione

Utilizzare NumPy da Mojo

Chiami array e funzioni NumPy.

Utilizzare NumPy da Mojo è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.shape

It 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. 🎯

Domande Frequenti

La lezione «Utilizzare NumPy da Mojo» è gratuita?

Sì — il testo completo di «Utilizzare NumPy da Mojo» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.

Cosa imparerò in «Utilizzare NumPy da Mojo»?

Chiami array e funzioni NumPy. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Mojo Academy?

Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Utilizzare NumPy da Mojo»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Mojo Academy?

Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Utilizzare NumPy da Mojo
  2. Passare dati Mojo a Python
  3. Leggere gli oggetti Python restituiti
  4. Insidie prestazionali dell'interoperabilità
← Torna a Mojo Academy