A syntax bridge before the performance story
The four differences that actually trip people up: 1-based arrays, blocks close with
end, element-wise math uses the dot, and behaviour lives in functions, not classes.
▶ Notebook (all of this, runnable): Julia for Python users
⬅ Previous: Course home · Next: 1 — Interpreted vs compiled ➡
end, not indentationJulia does not use indentation to delimit blocks, and there is no : after the header.
for, while, if, function, struct is closed by a matching end.elseif.The difference that bites hardest:
| Python | Julia | |
|---|---|---|
| First element | v[0] |
v[1] |
| Last element | v[-1] |
v[end] |
Slice 2:4 |
elements 2,3 | elements 2,3,4 |
Matrices are stored column-major (columns contiguous) — the opposite of NumPy. It won’t matter until the memory module, but it’s why the first index is the fast one.
.Julia’s answer to NumPy vectorisation and element-wise comprehensions — put a dot on any function or operator:
np.sqrt(x) being vectorised, Julia makes the intent explicit with the dot.return is optional (the last expression is returned); keyword args come after a ;.
A trailing
!in a name (push!,sort!) is a convention meaning “mutates its argument” — a habit, not syntax.
Julia has no classes and no self. Group data in a struct; the behaviour lives outside, in functions.
Putting behaviour outside the type is the whole point of the next language module (multiple dispatch vs OOP). Here we just note:
struct, immutable, noself.
Identifiers can be Unicode. Type a LaTeX name then TAB in the editor:
A number written right before a name multiplies:
2xis2 * x. Great for maths, and it’s how formulas on the slides map straight to code.
| Topic | Python | Julia |
|---|---|---|
| First / last element | v[0] / v[-1] |
v[1] / v[end] |
| Slice (both ends) | v[1:4] excl. 4 |
v[2:4] incl. 4 |
| Block delimiter | indentation + : |
keywords, closed by end |
| Else-if | elif |
elseif |
| Power / int-div | 2**10 / // |
2^10 / ÷ |
| String concat / f-string | "a"+"b" / f"{x}" |
"a"*"b" / "$x" |
| Object method | obj.method() |
method(obj) |
| Class | class C (has self) |
struct C (immutable, no self) |
| Element-wise | np.f(x) |
f.(x) — the dot |
| Copy vs alias | b = a[:] |
b = a aliases; copy(a) |
| Absent value | None |
nothing |
end, element-wise math is the dot.⬅ Previous: Course home · Next: 1 — Interpreted vs compiled ➡