Julia Basics - Part 1

This post is exploring the basics of Julia programming
Author

Quantilogy

Published

January 20, 2024

Getting Started

Check the version of julia being used.

VERSION
v"1.10.4"

Comments in julia begin with a #

# This is a comment

Variables

There are different types of variables in julia. typeof function can be used to know more about the types of variables available. I am using the println function below to print the type of variable, we don’t need it in the REPL.

println(typeof(2))    ## Integer 64-bit
println(typeof(2.0))  ## Floating point number 64-bit
println(typeof(2.0::Float64)) ## Explicitly declaring a variable as a Float
println(typeof("mystring")) ## this is a string
println(typeof([3,5,7,9]))  ## a vector of Integers
println(typeof([3.0,5.0,7.0,9.0]))  ## a vector of Integers
Int64
Float64
Float64
String
Vector{Int64}
Vector{Float64}

One key concept to know that in julia the assignment operator binds rather than copying the variable (similar to the behavior in Python), see the code below

x = [2,3,4,5]
println("x (before changing) is $(x)")
y = x        ## y binds to the values, rather than copying x
z = copy(x)  ## z explicitly makes a copy of x
x[1] = -5
println("x (after changing) is $(x)" )
println("y changes automatically after changing x, y is $(y)")
println("z is a copy of x, it does not change. z is $(z)")
x (before changing) is [2, 3, 4, 5]
x (after changing) is [-5, 3, 4, 5]
y changes automatically after changing x, y is [-5, 3, 4, 5]
z is a copy of x, it does not change. z is [2, 3, 4, 5]

In addition to binding, the code above also shows string interpolation, i.e., using the $ to refer to value of the variable in a string.

Testing using Boolean

Be careful about testing for exactness as Float only approximately stores the values of real numbers, e.g., the famous example

println(0.1 + 0.2 == 0.3)          ## false
println(isapprox(0.1 + 0.2, 0.3))  ## true
false
true

Control Flow

Unlike python, white space indentation does not matter in julia.

if Statement

The if statement is similar to that in R and python (without white-space rules), e.g.,

x = 8
if (x % 2 == 0)
  println("x is even")
else
  println("x is odd")
end
x is even

for loop

x = 2:2:20
println("length of x is $(length(x))") 
for i in 1:length(x)
  println("x[$(i)] is $(x[i])")
end
length of x is 10
x[1] is 2
x[2] is 4
x[3] is 6
x[4] is 8
x[5] is 10
x[6] is 12
x[7] is 14
x[8] is 16
x[9] is 18
x[10] is 20

Functions

Functions can change the value of their argument since values are passed by reference, e.g.,

x = [4, 6, 8]
function change_val!(y)
  y[1] = -1
  return(y)
end
println("x before applying function is $(x)")       ## original x
change_val!(x)   ## apply function
println("x after applying function is $(x)")       ## after appying the function
x before applying function is [4, 6, 8]
x after applying function is [-1, 6, 8]
Note

Note the function has an ! at the end of its name. By convention, if the function may change its inputs (also called arguments), it should have an ! at the end of the name. They don’t get any other special treatment from the language compiler.


Support my work with a coffee