Syntax cheat sheet

Welcome to the next chapter! We hope that you made it through the setup part without too many headaches. Please don't hesitate to ask us any questions or provide feedback on what you liked and didn't like or thought was too easy or too hard. Your input is always welcome and appreciated!

In the following, you will find a quick cheat sheet covering the basic Julia syntax. You will see that Julia's syntax is similar to Matlab, Python, and R (in this order). We recommend that you quickly skim this cheat sheet and do the exercises at the end of this chapter. You might want to keep this page open in another tab to have it as a reference at your disposal. There is a collection of side-by-side comparisons of Julia and R, Python or Matlab in the Additional Resources section of the Additional Materials chapter. Because these are more comprehensive, you should not need them during the workshop.

Variable assignment

In Julia, variables are used to store data and represent values in your program. Variables have a name and are assigned a value using the assignment operator =. Julia supports both regular (ASCII) variable names (like alpha) and Unicode variable names (like α).

my_variable = "awesome"
α = 1

To create a Unicode variable, type a backslash followed by the Unicode character name, and then press the Tab key to convert it into the corresponding Unicode symbol. For example, type \alpha and then press <tab> to create the α variable. If you are interested you can find a table of all available unicode characters and how to type them out in the Julia documentation.

Functions

Excellent! Now that you have an understanding of variables and the use of unicode symbols, let's dive into the topic of functions.

The basic syntax for defining a function in Julia is as follows:

function function_name(argument1, argument2)
    # function body
    return output
end

For example:

function add(x, y)
    return x + y
end
add (generic function with 1 method)

You can also define functions as:

function_name(argument1, argument2) = #function body

For example:

function square(x)
    return x*x
end
# or
square(x) = x * x

Vectors

first_vector = [1, 2, 3]

Julia uses 1-based indexing, which means the first element has an index of 1:

first_element = first_vector[1]

You can also assign a new value to an element of a vector via indexing:

first_vector[1] = 10

Operations on Vectors

second_vector = [4, 5, 6]
subtracted_vectors = first_vector - second_vector
3-element Vector{Int64}:
  6
 -3
 -3

Vectors can be concatenated with the vcat function or the ; operator:

concatenated_vector = vcat(first_vector, second_vector)
concatenated_vector = [first_vector; second_vector]
6-element Vector{Int64}:
 10
  2
  3
  4
  5
  6

Broadcasting

All functions can be applied element-wise to a collection (vector/matrix) by using the broadcast . operator. For example, we can take the element-wise square root of a vector:

sqrt.(first_vector)
3-element Vector{Float64}:
 3.1622776601683795
 1.4142135623730951
 1.7320508075688772

Or we mutliply two vectors element-wise:

first_vector .* second_vector
3-element Vector{Int64}:
 40
 10
 18

Note that infix operators (i.e., +, *, =, and many more) place the dot before the operator (i.e., .+, .*, .=)

Sequences

Create a range of integers from 1 to 10:

integer_sequence = 1:10
1:10

Create a range with a specific step size:

even_sequence = 2:2:10
2:2:10

Matrices

Create a square matrix of integers:

my_matrix = [1 2 3; 4 5 6]
# or alternatively:
my_matrix =
    [1 2 3
    4 5 6]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

As you can see, you can put rows in the matrix on different rows of your script, or separate them by semicolons ;.

You can access elements of a matrix by specifying the row and column indices in square brackets, separated by a comma.

upper_left_element = my_matrix[1, 1]
lower_right_element = my_matrix[2, 3]

Control Flow

For Loops

for variable in collection
    # Code to be executed for each value in the collection
end

For example:

for i in 1:5
    println(i^2)
end
1
4
9
16
25

Conditional evaluation

if condition
    # Code to be executed if the condition is true
end

You can also use elseif and else to test multiple conditions:

if condition1
    # Code to be executed if condition1 is true
elseif condition2
    # Code to be executed if condition1 is false and condition2 is true
else
    # Code to be executed if both condition1 and condition2 are false
end

For example, to check if a number is positive, negative, or zero:

number = -3

if number > 0
    println("The number is positive")
elseif number < 0
    println("The number is negative")
else
    println("The number is zero")
end
The number is negative

Packages

The section Package mode explained how to install packages. To use an installed package called Example in the current session:

using Example

This makes all functions from the Example package available, for example the function hello:

hello("world")
"Hello, world"

(As you can see, this is very similar to library(Example) in R or import Example in Python)

Tasks

Exercise

Write a function that takes two arguments and divides them.

show solution
Solution
function divide(a, b)
    return a/b
end
# or
divide(a, b) = a/b


Exercises

Create a vector of numbers and perform an element-wise square. Hint: In Julia, a variable x can be squared with x^2.

show solution
Solution
a = [1 2 3]
a .^ 2
# or
a .* a

Notice

If you're finding the pace overwhelming and feel like you need more training, don't worry! This cheat sheet is just meant to be a reference that you can use for the exercises in the next chapters. However, if you have trouble solving the coming exercises because you find the syntax too hard, we have the Foundations Revisited section, an extended version of this chapter. This resource is especially helpful if you're getting stuck, feeling demotivated and feel inadequately addressed by our cheat sheet.

And if you have any question, always remember, we're here to support your learning journey!