Getting started: a quick start guide to Julia

Quick Start

Variables and Some Basic Types

In Julia, you can define a variable similar to how you define it in Python. For example, you can define a x using =(assignment):

julia> x = 1
1

Every variable has a type. You can check it using typeof:

julia> typeof(x)
Int64

By default, Julia displays the output of the last operation. You can suppress the output by adding ; (a semicolon) at the end.

Functions

In Julia, you can also define short-form, one-line functions using = (assignment) similar to how you write things mathematically.

julia> f(x) = 2x
f (generic function with 1 method)

Typing the function’s name gives information about the function. To call it, we must use parentheses:

julia> f
f (generic function with 1 method)
julia> f(2)
4

For longer functions, we use the following syntax with the function keyword and end:

julia> function g(x, y)
       	z = x + y
       	return z^2
       end
g (generic function with 1 method)

Control Flows

In Julia, there are for, if and while control flows. For example, the for loop looks like:

julia> s = 0
0
julia> for i in 1:10
           s += 1
       end

we can now check the value of s by typing it again:

julia> s
10

Here, 1:10 is a range representing the numbers from 1 to 10:

julia> typeof(1:10)
UnitRange{Int64}

the if else statement looks like the following:

julia> if s < 10
       	# do something
       elseif 10 < s < 13
       	# do something
       else
       	# do something
       end

Matrix and Array

Julia carries its own Array type. If you use Python, it is similar to numpy.array in Python except:

  1. index starts from 1,
  2. the multi-dimensional index is column-wise.

You can also use list comprehension:

julia> [i for i in 1:10]
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

It works for multi-dimensional case too:

julia> [(i, j) for i in 1:10, j in 1:5]
10×5 Matrix{Tuple{Int64, Int64}}:
 (1, 1)   (1, 2)   (1, 3)   (1, 4)   (1, 5)
 (2, 1)   (2, 2)   (2, 3)   (2, 4)   (2, 5)
 (3, 1)   (3, 2)   (3, 3)   (3, 4)   (3, 5)
 (4, 1)   (4, 2)   (4, 3)   (4, 4)   (4, 5)
 (5, 1)   (5, 2)   (5, 3)   (5, 4)   (5, 5)
 (6, 1)   (6, 2)   (6, 3)   (6, 4)   (6, 5)
 (7, 1)   (7, 2)   (7, 3)   (7, 4)   (7, 5)
 (8, 1)   (8, 2)   (8, 3)   (8, 4)   (8, 5)
 (9, 1)   (9, 2)   (9, 3)   (9, 4)   (9, 5)
 (10, 1)  (10, 2)  (10, 3)  (10, 4)  (10, 5)

Most functions involving matrices and arrays follow the same convention as numpy or MATLAB. For example, you can create a random matrix using:

julia> rand(5, 5)
5×5 Matrix{Float64}:
 0.87212   0.0694519  0.26476   0.315297  0.514814
 0.770019  0.625702   0.027029  0.861861  0.63027
 0.193426  0.307376   0.573832  0.286339  0.9424
 0.86084   0.991154   0.814427  0.894352  0.920268
 0.403303  0.734528   0.344152  0.106539  0.837469

If you have questions about using a function, you can always type the question mark ? in your REPL following the function name:

julia> ?rand

Package Manager & Environments

Julia carries its own package manager. You can use it as a normal package:

julia> using Pkg

To install a package, you can use:

julia> Pkg.add("Bloqade")

To remove a package, you can use:

julia> Pkg.rm("Bloqade")

All Julia programs run inside an environment. The default is the global environment. It is usually recommended to run your notebook in a local environment, so you won’t hit any version conflicts between different packages.

Resources

For more resources, check the official website julialang.org/learning or download Julia here

It may be worth to note that Julia can be downloaded and installed from here