> For the complete Mojo documentation index, see [llms.txt](/llms.txt).
> Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

# Mojo quickstart

This page quickly teaches you Mojo's syntax by focusing on code.

For more explanation of the language features, see the [Mojo get
started tutorial](/docs/manual/get-started) or [Mojo language
basics](/docs/manual/basics).

:::note System requirements

You can use Mac, Linux, or Windows with WSL. For details, see the
[system requirements](/docs/requirements/).

:::

## Project setup

You can install Mojo using any Python or Conda package manager,
but we recommend either `pixi` or `uv`.

## Hello Mojo

Create `analyzer.mojo` in your favorite IDE or editor.

Add this to `analyzer.mojo`:

```mojo
def main():
    print("Temperature Analyzer")
```

Run it:

```bash
mojo analyzer.mojo
```

Insights:

- 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:

```mojo
def main():
    print("Temperature Analyzer")

    # Square brackets tell Mojo the `List` 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 `main()` under the print statement:

```mojo
def main():
    # ... existing code ...

    for index in range(len(temps)):  # The range is [0, len(temps))
        print("  Day {}: {}°C".format(index + 1, temps[index]))
```

Insights:

- The day is offset by 1 because the range uses zero-based indexing.

## Functions

Add this function above `main()` to calculate the average temperature:

```mojo
def calculate_average(temps: List[Float64]) -> Float64:
    var total: Float64 = 0.0
    for index in range(len(temps)):
        total += temps[index]
    return total / Float64(len(temps))

def main():
    # ... existing code ...
```

Add to the end of `main()` to call the function:

```mojo
    var avg = calculate_average(temps)
    print("Average: {}°C".format(avg))
```

## Conditionals

Classify the average temperature. Add to the end of `main()`:

```mojo
    if avg > 25.0:
        print("Status: Hot week")
    elif avg > 20.0:
        print("Status: Comfortable week")
    else:
        print("Status: Cool week")
```

## Raise errors

Handle empty data by updating `calculate_average`. Now the function
can raise an error.

```mojo
def calculate_average(temps: List[Float64]) raises -> Float64:
    if len(temps) == 0:  # Empty list of temperatures
        raise Error("No temperature data")

    var total: Float64 = 0.0
    for index in range(len(temps)):
        total += temps[index]
    return total / Float64(len(temps))
```

What changed:

- You add `raises` before the return arrow.
- You add the check for an empty list.
- You raise an `Error` if it's empty.

## Handle errors

In `main()`, wrap your code in `try-except` for error handling:

```mojo
    try:
        var avg = calculate_average(temps)
        print("Average: {}°C".format(avg))

        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 `List[Float64]()`.
Confirm that your app errors with "No temperature data".

## Python integration

Add statistics with Python's numpy. First, install it:

**uv:**

```bash
uv pip install numpy
```

---

**pixi:**

```bash
pixi add numpy
```

---

**pip:**

```bash
pip install numpy
```

---

**conda:**

```bash
conda install numpy
```

Then, add the following imports at the top of your file:

```mojo
from std.python import Python, PythonObject
```

Now, calculate Python stats at the end of the `try` block in `main()`:

```mojo
        var np = Python.import_module("numpy")
        var pytemps: PythonObject = [20.5, 22.3, 19.8, 25.1]
        print("Temperature standard deviation:", np.std(pytemps))
```

## Final code

Your complete `analyzer.mojo`:

```mojo
from std.python import Python, PythonObject

def calculate_average(temps: List[Float64]) raises -> Float64:
    if len(temps) == 0:
        raise Error("No temperature data")

    var total: Float64 = 0.0
    for index in range(len(temps)):
        total += temps[index]
    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("  Day {}: {}°C".format(index + 1, temps[index]))

    try:
        var avg = calculate_average(temps)
        print("Average: {}°C".format(avg))

        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: PythonObject = [20.5, 22.3, 19.8, 25.1]
        print("Temperature standard deviation:", np.std(pytemps))
        _ = pytemps^ # Allow the Python object to deallocate
    except e:
        print("Error:", e)
```

## What you touched

Mojo variables, lists, loops, functions, conditionals, error handling, and
Python integration, all in one working program.

## Use Mojo with AI coding assistants

If you use AI coding assistants, the `mojo-syntax` skill keeps you aligned
with the latest nightly language releases. Stay up to date with Mojo's
rapid development:

```bash
npx skills add modular/skills
```

This installs all four [Mojo agent skills](/docs/tools/skills), including
`mojo-syntax`.

## Keep going

**Build something bigger:** [Tutorial: Game of Life](/docs/manual/get-started)
**Language guide:** [Mojo Manual](/docs/manual)
**API docs:** [Standard Library](/docs/std)
