Moving Data Across the Bridge
Convert between Mojo and Python values.
Moving Data Across the Bridge is a free Mojo Academy lesson on CoddyKit — lesson 3 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.
Two Worlds of Values
Mojo has its own native types, and Python has its own. Interop means converting values as they cross between the two worlds.
Mojo to Python
When you pass a Mojo value into a Python call, Mojo automatically wraps it into a PythonObject Python can understand.
var n: Int = 42
print(np.sqrt(n))Numbers Convert Cleanly
Simple values like Int and Float move across smoothly, becoming Python ints and floats with no extra work from you.
var x: Float64 = 3.5
var r = math.floor(x)Strings Travel Too
A Mojo String becomes a Python str on the way in, so text-based functions just work across the bridge.
var name = String("Mojo")
print(name)Python to Mojo
Coming back, Python results arrive as a PythonObject. You then convert it into a native Mojo type when you need one.
var obj = math.factorial(5)Unwrap to Int
To get a real Mojo number from a result, construct the type from the object, like wrapping it in Int.
var count = Int(math.factorial(5))Unwrap to Float
The same idea works for decimals: build a Float64 from the Python result to use it in Mojo math.
var ratio = Float64(math.pi)Unwrap to String
Turn a Python text result into a Mojo String the same way, so you can store and process it natively.
var label = String(obj)Indexing Python Collections
You can index a Python list or array result with brackets, getting back another PythonObject to convert.
var first = arr[0]Convert at the Edges
A good habit: keep values as PythonObjects while talking to Python, then convert to Mojo types at the boundary you control.
Why It Matters
Knowing how data crosses the bridge keeps your types predictable, so Python results flow safely into fast Mojo code. 🌉
Quick Check
Recall how to turn a Python result into a Mojo number.
Recap
You saw Mojo values become PythonObjects going in, and learned to convert results back into Int, Float, or String. 🎯
Frequently asked questions
Is the “Moving Data Across the Bridge” lesson free?
Yes — the full text of “Moving Data Across the Bridge” 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 “Moving Data Across the Bridge”?
Convert between Mojo and Python values. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Moving Data Across the Bridge” 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
- Importing a Python Module
- Calling Python Functions
- Moving Data Across the Bridge
- When to Use Python Interop