Skip to content

Commit 4297097

Browse files
lungbenjonas-schulzewaldyriousStefanKarpinskitkf
authored
Enhance Noteworthy Differences to Python in Documentation (#35268)
Co-Authored-By: Jonas Schulze <jonas.schulze7@t-online.de> Co-Authored-By: Waldir Pimenta <waldyrious@gmail.com> Co-authored-by: Stefan Karpinski <stefan@karpinski.org> Co-Authored-By: Takafumi Arakaki <aka.tkf@gmail.com>
1 parent c2787b1 commit 4297097

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

doc/src/manual/noteworthy-differences.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,19 @@ For users coming to Julia from R, these are some noteworthy differences:
197197

198198
## Noteworthy differences from Python
199199

200-
* Julia requires `end` to end a block. Unlike Python, Julia has no `pass` keyword.
200+
* Julia's `for`, `if`, `while`, etc. blocks are terminated by the `end` keyword. Indentation level
201+
is not significant as it is in Python. Unlike Python, Julia has no `pass` keyword.
202+
* Strings are denoted by double quotation marks (`"text"`) in Julia (with three double quotation marks for multi-line strings), whereas in Python they can be denoted either by single (`'text'`) or double quotation marks (`"text"`). Single quotation marks are used for characters in Julia (`'c'`).
203+
* String concatenation is done with `*` in Julia, not `+` like in Python. Analogously, string repetition is done with `^`, not `*`. Implicit string concatenation of string literals like in Python (e.g. `'ab' 'cd' == 'abcd'`) is not done in Julia.
204+
* Python Lists—flexible but slow—correspond to the Julia `Vector{Any}` type or more generally `Vector{T}` where `T` is some non-concrete element type. "Fast" arrays like Numpy arrays that store elements in-place (i.e., `dtype` is `np.float64`, `[('f1', np.uint64), ('f2', np.int32)]`, etc.) can be represented by `Array{T}` where `T` is a concrete, immutable element type. This includes built-in types like `Float64`, `Int32`, `Int64` but also more complex types like `Tuple{UInt64,Float64}` and many user-defined types as well.
201205
* In Julia, indexing of arrays, strings, etc. is 1-based not 0-based.
202206
* Julia's slice indexing includes the last element, unlike in Python. `a[2:3]` in Julia is `a[1:3]`
203207
in Python.
204208
* Julia does not support negative indices. In particular, the last element of a list or array is
205209
indexed with `end` in Julia, not `-1` as in Python.
206-
* Julia's `for`, `if`, `while`, etc. blocks are terminated by the `end` keyword. Indentation level
207-
is not significant as it is in Python.
210+
* Julia requires `end` for indexing until the last element. `x[1:]` in Python is equivalent to `x[2:end]` in Julia.
211+
* Julia's range indexing has the format of `x[start:step:stop]`, whereas Python's format is `x[start:(stop+1):step]`. Hence, `x[0:10:2]` in Python is equivalent to `x[1:2:10]` in Julia. Similarly, `x[::-1]` in Python, which refers to the reversed array, is equivalent to `x[end:-1:1]` in Julia.
212+
* In Julia, indexing a matrix with arrays like `X[[1,2], [1,3]]` refers to a sub-matrix that contains the intersections of the first and second rows with the first and third columns. In Python, `X[[1,2], [1,3]]` refers to a vector that contains the values of cell `[1,1]` and `[2,3]` in the matrix. `X[[1,2], [1,3]]` in Julia is equivalent with `X[np.ix_([0,1],[0,2])]` in Python. `X[[0,1], [0,2]]` in Python is equivalent with `X[[CartesianIndex(1,1), CartesianIndex(2,3)]]` in Julia.
208213
* Julia has no line continuation syntax: if, at the end of a line, the input so far is a complete
209214
expression, it is considered done; otherwise the input continues. One way to force an expression
210215
to continue is to wrap it in parentheses.
@@ -221,9 +226,26 @@ For users coming to Julia from R, these are some noteworthy differences:
221226
On the other hand, the function `g(x=[1,2]) = push!(x,3)` returns `[1,2,3]` every time it is called
222227
as `g()`.
223228
* In Julia `%` is the remainder operator, whereas in Python it is the modulus.
224-
* The commonly used `Int` type corresponds to the machine integer type (`Int32` or `Int64`).
225-
This means it will overflow, such that `2^64 == 0`. If you need larger values use another appropriate type,
229+
* In Julia, the commonly used `Int` type corresponds to the machine integer type (`Int32` or `Int64`), unlike in Python, where `int` is an arbitrary length integer.
230+
This means in Julia the `Int` type will overflow, such that `2^64 == 0`. If you need larger values use another appropriate type,
226231
such as `Int128`, [`BigInt`](@ref) or a floating point type like `Float64`.
232+
* The imaginary unit `sqrt(-1)` is represented in Julia as `im`, not `j` as in Python.
233+
* In Julia, the exponentiation operator is `^`, not `**` as in Python.
234+
* Julia uses `nothing` of type `Nothing` to represent a null value, whereas Python uses `None` of type `NoneType`.
235+
* In Julia, the standard operators over a matrix type are matrix operations, whereas, in Python, the standard operators are element-wise operations. When both `A` and `B` are matrices, `A * B` in Julia performs matrix multiplication, not element-wise multiplication as in Python. `A * B` in Julia is equivalent with `A @ B` in Python, whereas `A * B` in Python is equivalent with `A .* B` in Julia.
236+
* The adjoint operator `'` in Julia returns an adjoint of a vector (a lazy representation of row vector), whereas the transpose operator `.T` over a vector in Python returns the original vector (non-op).
237+
* In Julia, a function may contain multiple concrete implementations (called *Methods*), selected via multiple dispatch, whereas functions in Python have a single implementation (no polymorphism).
238+
* There are no classes in Julia. Instead they are structures (mutable or immutable), containing data but no methods.
239+
* Calling a method of a class in Python (`a = MyClass(x), x.func(y)`) corresponds to a function call in Julia, e.g. `a = MyStruct(x), func(x::MyStruct, y)`. In general, multiple dispatch is more flexible and powerful than the Python class system.
240+
* Julia structures may have exactly one abstract supertype, whereas Python classes can inherit from one or more (abstract or concrete) superclasses.
241+
* The logical Julia program structure (Packages and Modules) is independent of the file strucutre (`include` for additional files), whereas the Python code structure is defined by directories (Packages) and files (Modules).
242+
* The ternary operator `x > 0 ? 1 : -1` in Julia corresponds to conditional expression in Python `1 if x > 0 else -1`.
243+
* In Julia the `@` symbol refers to a macro, whereas in Python it refers to a decorator.
244+
* Exception handling in Julia is done using `try``catch``finally`, instead of `try``except``finally`. In contrast to Python, it is not recommended to use exception handling as part of the normal workflow in Julia due to performance reasons.
245+
* In Julia loops are fast, there is no need to write "vectorized" code for performance reasons.
246+
* Be careful with non-constant global variables in Julia, especially in tight loops. Since you can write close-to-metal code in Julia (unlike Python), the effect of globals can be drastic (see [Performance Tips](@ref man-performance-tips)).
247+
* In Python, the majority of values can be used in logical contexts (e.g. `if "a":` means the following block is executed, and `if "":` means it is not). In Julia, you need explicit conversion to `Bool` (e.g. `if "a"` throws an exception). If you want to test for a non-empty string in Julia, you would explicitly write `if !isempty("")`.
248+
* In Julia, a new local scope is introduced by most code blocks, including loops and `try``catch``finally`. Note that comprehensions (list, generator, etc.) introduce a new local scope both in Python and Julia, whereas `if` blocks do not introduce a new local scope in both languages.
227249

228250
## Noteworthy differences from C/C++
229251

0 commit comments

Comments
 (0)