For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
Mojo quickstart
Welcome to Mojo, a systems language for the AI era.
This tutorial offers a quick tour of Mojo language fundamentals by showing you how to build a simple application. You'll get a taste of Mojo's syntax and enough familiarity to read and write basic Mojo code.
It should take about 15-30 minutes if you stop to explore, or less if you work straight through.
Setup
Before starting:
- Check the system requirements.
- Install Mojo in a
pixioruvenvironment. - Open a terminal and make sure
mojois in your path or environment.
As you work through this tour, look for Takeaways items. They connect new Mojo syntax to concepts you may already know from other languages.
Hello Mojo
Create analyzer.mojo in your favorite IDE or editor.
Add this to analyzer.mojo:
def main():
print("Temperature Analyzer")
Run it:
mojo analyzer.mojo
Takeaways
- If you see "Temperature Analyzer", your setup works.
- All Mojo executables use
main()as their entry point.
Variables and data
Update your file to add temperature data:
def main():
print("Temperature Analyzer")
# [Float64] sets the List element type at compile time
var temps: List[Float64] = [20.5, 22.3, 19.8, 25.1]
print("Recorded", len(temps), "temperatures")
Loops
Print each temperature. Add to the end of main():
def main():
# ... existing code ...
for index in range(len(temps)): # The range is [0, len(temps))
print(t" Day {index + 1}: {temps[index]}°C")
Takeaways
-
This loop uses indexes. Normally you iterate over elements.
-
The
t"..."prefix creates a template string. Braces{}interpolate expressions into the output. This avoids memory allocations for intermediate values.
Functions
Add a function above main() to calculate the average temperature:
def calculate_average(temps: List[Float64]) -> Float64:
# A literal with a decimal component defaults to Float64
var total = 0.0
for temp in temps:
total += temp
return total / Float64(len(temps))
def main():
# ... existing code ...
Call the function by adding this to the end of main():
var avg = calculate_average(temps)
print(t"Average: {round(avg, 2)}°C")
Takeaways
- Mojo's
deffunctions don't raise by default. round()returnsavgrounded to two decimal places.
Conditionals
Classify the average temperature. Add to the end of main():
if avg > 25.0:
print("Status: Hot week")
elif avg > 20.0:
print("Status: Comfortable week")
else:
print("Status: Cool week")
Raise errors
Empty data means no average. Update calculate_average() to handle the
error:
- Add
raisesbefore the return arrow. - Add an empty list check.
- Raise an
Errorif it's empty.
def calculate_average(temps: List[Float64]) raises -> Float64:
if len(temps) == 0:
raise Error("No temperature data")
var total = 0.0
for temp in temps:
total += temp
return total / Float64(len(temps))
After updating, your app will no longer compile.
Once calculate_average() can raise, its callers must handle or propagate
the error. You do that in the next step.
Handle errors
Wrap failable code in try-except for error handling:
try:
var avg = calculate_average(temps)
print(t"Average: {round(avg, 2)}°C")
if avg > 25.0:
print("Status: Hot week")
elif avg > 20.0:
print("Status: Comfortable week")
else:
print("Status: Cool week")
except e:
print("Error:", e)
To test the error, replace temps with [].
Confirm that your app errors with "No temperature data".
Takeaways
- Each
trystatement requires at least oneexceptorfinallyclause. - An
elseclause runs only if no error occurs. - A
finallyclause always runs.
try:
operation()
except e:
handle_error(e) # Runs if an error occurs
else:
on_success() # Runs only if no error occurred
finally:
cleanup() # Always runs
Python integration
Mojo integrates with libraries written in other languages. Use Python's NumPy to calculate the standard deviation.
Install NumPy with your package manager:
pixi add numpy
or:
uv pip install numpy
Add these imports at the top of your file:
from std.python import Python
from std.python.numpy import copy_to_numpy_array
Then calculate the standard deviation at the end of the try block in
main():
var np = Python.import_module("numpy")
var pytemps = copy_to_numpy_array(temps)
var std_dev = np.std(pytemps)
print("Temperature standard deviation:", std_dev)
Final code
Your complete analyzer.mojo:
from std.python import Python
from std.python.numpy import copy_to_numpy_array
def calculate_average(temps: List[Float64]) raises -> Float64:
if len(temps) == 0:
raise Error("No temperature data")
var total = 0.0
for temp in temps:
total += temp
return total / Float64(len(temps))
def main():
print("Temperature Analyzer")
var temps: List[Float64] = [20.5, 22.3, 19.8, 25.1]
print("Recorded", len(temps), "temperatures")
for index in range(len(temps)):
print(t" Day {index + 1}: {temps[index]}°C")
try:
var avg = calculate_average(temps)
print(t"Average: {round(avg, 2)}°C")
if avg > 25.0:
print("Status: Hot week")
elif avg > 20.0:
print("Status: Comfortable week")
else:
print("Status: Cool week")
var np = Python.import_module("numpy")
var pytemps = copy_to_numpy_array(temps)
var std_dev = np.std(pytemps)
print("Temperature standard deviation:", std_dev)
except e:
print("Error:", e)
What you touched
You just used: Mojo variables, lists, loops, functions, conditionals, error handling, and Python integration in one working program.
Your first day? Try these
- Build Conway's Game of Life with the get started tutorial. It normally takes about 45-60 minutes.
- Keep the language reference handy for syntax, keywords, and more.
- Download the cheat sheets for printable reference cards that bring entire concepts together.
- Play Mojo Quest to practice Mojo syntax with coding challenges.
Use Mojo with AI coding assistants
If you use an AI coding assistant, install the Mojo agent skills to give it up-to-date information about the rapidly evolving language:
npx skills add modular/skills
This installs all Mojo agent skills, including the
mojo-syntax skill for the latest nightly language releases.
Keep going
- Learn Mojo in depth with the Mojo Manual.
- Look up APIs in the Standard Library.
- Ask questions and connect with other Mojo developers on the Discourse forums and Discord.
- Follow Mojo development on the Mojo Blog.