- Julia requires
end
to end a block. Unlike Python, Julia has nopass
keyword. - In Julia, indexing of arrays, strings, etc. is 1-based not 0-based.
- Julia's slice indexing includes the last element, unlike in Python.
a[2:3]
in Julia isa[1:3]
in Python. - Julia does not support negative indices. In particular, the last element of a list or array is indexed with
end
in Julia, not-1
as in Python. - Julia's
for
,if
,while
, etc. blocks are terminated by theend
keyword. Indentation level is not significant as it is in Python. - Julia has no line continuation syntax: if, at the end of a line, the input so far is a complete expression, it is considered done; otherwise the input continues. One way to force an expression to continue is to wrap it in parentheses.
- Julia arrays are column major (Fortran ordered) whereas NumPy arrays are row major (C-ordered) by default. To get optimal performance when looping over arrays, the order of the loops should be reversed in Julia relative to NumPy (see relevant section of Performance Tips).
- Julia's updating operators (e.g.
+=
,-=
, ...) are not in-place whereas NumPy's are. This meansA = [1, 1]; B = A; B += [3, 3]
doesn't change values inA
, it rather rebinds the nameB
to the result of the right-hand sideB = B + 3
, which is a new array. For in-place operation, useB .+= 3
(see also dot operators), explicit loops, orInplaceOps.jl
. - Julia evaluates default values of function arguments every time the method is invoked, unlike in Python where the default values are evaluated only once when the function is defined. For example, the function
f(x=rand()) = x
returns a new random number every time it is invoked without argument. On the other hand, the functiong(x=[1,2]) = push!(x,3)
returns[1,2,3]
every time it is called asg()
. - In Julia
%
is the remainder operator, whereas in Python it is the modulus.

点击阅读原文可查看历史文章
网友评论