Python vs Mojo: A Syntax Comparison
Mojo is gaining attention as a new language designed to combine Python’s simplicity with C-level performance. If you’re familiar with Python, you’ll find many similarities in Mojo — but also some key differences. In this article, we compare Python and Mojo syntax side-by-side to help you transition smoothly.
1. Variable Declaration
Python | Mojo |
---|---|
x = 10 name = "Alice" | let x = 10 let name: String = "Alice" |
Note: Mojo uses let
or const
and supports static typing.
2. Function Definition
Python | Mojo |
---|---|
def add(a, b): return a + b | fn add(a: Int, b: Int) -> Int: return a + b |
Difference: Mojo requires type annotations and uses fn
instead of def
.
3. Data Types
- Python: Dynamic types like
int
,float
,str
,bool
- Mojo: Statically typed system with
Int8
,Int32
,Float64
,String
Python is dynamically typed by default, while Mojo emphasizes performance with static types.
4. Arithmetic Operators
Both Python and Mojo share the same common operators:
+
,-
,*
,/
,//
,%
,**
No significant differences here — arithmetic expressions work the same in both.
5. Control Flow
Python | Mojo |
---|---|
if x > 0: print("Positive") | if x > 0: print("Positive") |
Control structures like if
, for
, and while
are nearly identical in syntax.
6. Summary Table
Feature | Python | Mojo |
---|---|---|
Variable Declaration | x = 10 | let x = 10 |
Functions | def , no type required | fn , type required |
Typing | Optional | Mandatory |
Control Flow | Pythonic | Very similar |
Performance | Flexible | Optimized for speed |
Final Thoughts
Mojo is designed to feel familiar to Python users while offering better performance through strict typing and low-level control. If you already know Python, learning Mojo will be faster — and this comparison should help guide your journey.
Follow for more Mojo tutorials, Python tips, and real-world comparisons!
Comment