01 — fn vs def
fn is the strict, fast path — explicit types, no Python dynamism; def stays relaxed.
fn fact(n: Int) -> Int:
var r = 1
for i in range(1, n + 1):
r *= i
return r§ 02 — Eras · 2023
Python’s syntax, systems performance — built for AI hardware
Left: era context, features, and the 101 hello. Right: 2 core concepts, 3 deeper 201 lessons, the factorial specimen, and where Mojo lives today — each column scrolls on its own.
Python owns AI research but not its hardware. Mojo keeps the syntax and reclaims the performance — one language for lab and kernel.
Young, single-vendor, and starting its ecosystem from zero; the bet is huge and unproven at scale.
Session one is the smallest program that still counts as speaking Mojo: what every tutorial prints first, in the dialect that mattered when the language arrived (2023).
fn main():
print("Hello, world")101First contact — hello world and 2 ideas that define how Mojo reads. 201 — 3 lessons on mechanisms you meet after the factorial specimen: the sharp edges practitioners actually use.
2 fragments, each naming a habit of mind in Mojo. Read the note, then the code — together they are the 101 in one sitting.
01 — fn vs def
fn is the strict, fast path — explicit types, no Python dynamism; def stays relaxed.
fn fact(n: Int) -> Int:
var r = 1
for i in range(1, n + 1):
r *= i
return r02 — struct
Compile-time-bound structs for zero-cost data — ML’s answer to C.
struct Ship:
var tons: Int
fn heavy(self) -> Bool:
return self.tons > 20The 201 assumes you have read the 101 and the specimen. Each lesson is one mechanism — what it is for, how it looks in code, and the trap or payoff that makes Mojo distinctive at scale.
fn is statically typed and compiled ahead of time; def keeps Python’s dynamism for exploration. Moving code from def to fn is how Mojo earns its speed.
fn compute(n: Int) -> Int:
var total = 0
for i in range(n):
total += i
return totalStructs are laid out at compile time and passed by value — no heap allocation unless you ask for it, unlike Python objects.
struct Ship:
var tons: Int
var name: String
fn is_heavy(self) -> Bool:
return self.tons > 20SIMD and the compiler pipeline target accelerators directly, which is why a language with Python’s syntax can approach C++ speed on the right hardware.
The fast path: fn and struct mark the zero-cost roads through a Python-shaped language — syntax borrowed wholesale, performance reclaimed.
fn fact(n: Int) -> Int:
var r = 1
for i in range(1, n + 1):
r *= i
return rinstallcurl -ssL https://get.modular.com | bash -
runmojo run fact.mojo
AI kernels and Modular’s inference stack; the ecosystem is young, the performance claims are loud, and the bet is unproven at scale.