Syntax
Contents
Syntax#
This notebook briefly goes over basic syntax in Julia.
Differences from Python#
This page summarises the main differences between Julia and Python: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python
Basic Types#
A = 3.14 # `Float64`
3.14
B = 10 # `Int64`
10
C = "hello" # `String`
"hello"
'h' # `Character`
'h': ASCII/Unicode U+0068 (category Ll: Letter, lowercase)
D = C[1] # Strings are collections of `Character`s
'h': ASCII/Unicode U+0068 (category Ll: Letter, lowercase)
E = true # `Boolean`
true
typeof(A) # Find the type of a variable
Float64
supertype(Integer) # Find supertypes
Real
subtypes(Integer) # Find subtypes
3-element Vector{Any}:
Bool
Signed
Unsigned
Integer <: Real # Check if something is a “Subtype of”
true
:something # Symbol for a name or label
:something
Arithmetic#
2 + 2 - 1 == 3
true
2^2 == 4
true
sqrt(9) == 3
true
10 / 2 == 5.0
true
exp(π * im) ≈ -1 # NB: this is `\approx` (≈) not equals
true
Containers#
Lists, arrays, dictionaries, tuples, etc…
t = (1, 2, 3) # Tuple (immutable)
(1, 2, 3)
t = (a=2, b=1 + 2) # Named tuple, access: t.a
(a = 2, b = 3)
d = Dict(:A => 1, :B => 2) # Dictionary
Dict{Symbol, Int64} with 2 entries:
:A => 1
:B => 2
a = [1, 2, 3, 4] # 4-element Vector{Int64}
4-element Vector{Int64}:
1
2
3
4
Vector{Int64}(undef, 10) # undef 1-D array length n
10-element Vector{Int64}:
140308137896976
140308137897008
140308137897040
140308137897072
140308137897104
140308137897136
140308137897168
140308137897200
140308137897232
140308137897264
Float64[1, 2] # 2-element Vector{Float64}
2-element Vector{Float64}:
1.0
2.0
Array(1:5) # 5-element Array{Int64,1}
5-element Vector{Int64}:
1
2
3
4
5
[1:5;] # 5-element Array{Int64,1}
5-element Vector{Int64}:
1
2
3
4
5
[1:5] # 1-element vector with a range
1-element Vector{UnitRange{Int64}}:
1:5
a = [i^3 for i in [1, 2, 3]] # Array comprehension
3-element Vector{Int64}:
1
8
27
[range(0, stop=2π, length=5);] # 5-element Vector{Float64}
5-element Vector{Float64}:
0.0
1.5707963267948966
3.141592653589793
4.71238898038469
6.283185307179586
rand(5) # random 5-elem vector in [0,1)
5-element Vector{Float64}:
0.5191453942059896
0.1172917547222545
0.400891263220127
0.46935731766886424
0.6427127357180475
rand(Int, 5) # random vector with integers
5-element Vector{Int64}:
9023148357981655655
-9209514901102199357
-4932026185504444220
3427665566901318944
6579442520360223700
ones(5) # 5-elem vector with FP64 ones
5-element Vector{Float64}:
1.0
1.0
1.0
1.0
1.0
zeros(5) # 5-elem vector with FP64 zeros
5-element Vector{Float64}:
0.0
0.0
0.0
0.0
0.0
[1, 2, 3] .^ 2 # Element-wise dot-operation
3-element Vector{Int64}:
1
4
9
Array{Float16,2}(undef, 2, 3) # New undef array type `T`, elements `dims`
2×3 Matrix{Float16}:
-0.004456 NaN 1.13e-6
-0.0637 0.0 0.0
mat = [1 2; 3 4] # 2×2 Matrix{Int64}
2×2 Matrix{Int64}:
1 2
3 4
zeros(4, 4, 4, 4) # Zero 4×4×4×4 Array{Float64,4}
4×4×4×4 Array{Float64, 4}:
[:, :, 1, 1] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 2, 1] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 3, 1] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 4, 1] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 1, 2] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 2, 2] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 3, 2] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 4, 2] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 1, 3] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 2, 3] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 3, 3] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 4, 3] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 1, 4] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 2, 4] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 3, 4] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
[:, :, 4, 4] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
rand(12, 4); # Random 12×4 Matrix{Float64}
Indexing#
a[1] # first element
1
a[1:3] # 3-element vector
3-element Vector{Int64}:
1
8
27
a[3:end] # end is last element
1-element Vector{Int64}:
27
a[1:2:end] # step size of 2
2-element Vector{Int64}:
1
27
a[3:end] # end is last element
1-element Vector{Int64}:
27
Functions#
length(a)
3
first(a)
1
last(a)
27
minimum(a)
1
maximum(a)
27
argmin(a)
1
argmax(a)
3
size(a)
(3,)
push!(a, 10) # Append in-place
4-element Vector{Int64}:
1
8
27
10
insert!(a, 1, 42) # Insert in given position
5-element Vector{Int64}:
42
1
8
27
10
append!(a, [3, 5, 7]) # Append another array
8-element Vector{Int64}:
42
1
8
27
10
3
5
7
splice!(a, 3, -1) # Rm in given pos and replace
8
Loops and Conditionals#
# Basic looping over a list
for i in [1, 2, 3, 4, 5]
println("i = $i")
end
i = 1
i = 2
i = 3
i = 4
i = 5
# Looping over a list, by index
A = [1 2; 3 4]
for i in eachindex(A)
println("i = $i, A[i] = $(A[i])")
end
i = 1, A[i] = 1
i = 2, A[i] = 3
i = 3, A[i] = 2
i = 4, A[i] = 4
# Loop through key-value pairs in a dictionary
for (k, v) in Dict("A" => 1, "B" => 2, "C" => 3)
println("$k is $v")
end
B is 2
A is 1
C is 3
if 1 == 10
println("Something is wrong")
elseif 1 == 1
println("Sounds right!")
end
Sounds right!