Introduction to Julia
James Fulton
Climate informatics researcher
# Predefine array x = [1,2,3,4]
# Add the number 5 to end of array push!(x, 5)
println(x)
[1,2,3,4,5]
# Predefine array x = [1,2,3,4] # Add the float 5.0 to end of array push!(x, 5.0)
println(x) println(eltype(x))
[1,2,3,4,5]
Int64
# Predefine array
x = [1,2,3,4]
# Add the float 5.2 to end of array
push!(x, 5.2)
ERROR: InexactError: Int64(5.2)
# Create float array x = Float64[1,2,3,4] println(typeof(x))
# Add the float 5.2 to end of array push!(x, 5.2) println(x)
Vector{Float64}
[1.0, 2.0, 3.0, 4.0, 5.2]
# Create empty float array
x = Float64[]
println(typeof(x))
println(x)
Vector{Float64}
Float64[]
# Create empty string array
x = String[]
println(typeof(x))
println(x)
Vector{String}
String[]
# Create empty string array
x = String[]
# Add some elements to the array
push!(x, "one")
push!(x, "two")
push!(x, "three")
println(x)
["one", "two", "three"]
# Create empty string array x = String[] # Add some elements to the array append!(x, ["one", "two", "three"])
println(x)
["one", "two", "three"]
x = [1,2,3,4]
# Remove 1 element from end
x = x[1:end-1]
println(x)
[1, 2, 3]
x = [1,2,3,4] # Remove 1 element from end last_element = pop!(x)
println(x) println(last_element)
[1, 2, 3]
4
# Create integer array with 4 zeros x = zeros(Int64, 4)
println(x)
[0, 0, 0, 0]
# Create integer array with 4 zeros x = zeros(Int64, 4) # Replace element in position 3 with value 1 x[3] = 1
println(x)
[0, 0, 1, 0]
# Create integer array with 4 zeros x = zeros(Int64, 4) # Replace many elements x[2:3] = [2,3]
println(x)
[0, 2, 3, 0]
push!(x, 1)
append!(x, [1,2,3])
pop!(x)
Int64[1,2,3]
, Float64[1,2,3]
, etc.Int64[]
, Float64[]
, etc.zeros(Int64, n)
x[index] = value
x[a:b] = [value1, value2, ...]
Introduction to Julia