Zoom Basics of the Julia language

Last updated: January 12, 2023

 Table of contents

Date:   Tuesday, May 31
Time:   10:30 am Pacific Time
Access: You should have received the meeting id and password by email.

Please have a look in your spam box if you haven’t received the email.

How to run Julia

There are several ways to run Julia interactively:

Julia can also be run non interactively as we will see in a further section.

For now, we will run Julia directly in the REPL.

Comments

Comments do not get evaluated by Julia and are for humans only.

# Comments in Julia are identified by hastags
#=
Comments can also spread over multiple lines
if you enclose them with this syntax
=#
x = 2          # Comments can be added at the end of lines
2

Basic operations

# By default, Julia returns the output
2 + 3
5
# Trailing semi-colons suppress the output
3 + 7;
# Alternative syntax that can be used with operators
+(2, 5)
7
# Updating operators
a = 3
a += 8    # this is the same as a = a + 8
11
# Operator precedence follows standard rules
3 + 2 ^ 3 * 10
83

More exotic operators

# Usual division
6 / 2
3.0
# Inverse division
2 \ 6
3.0
# Integer division (division truncated to an integer)
7 ÷ 2
3
# Remainder
7 % 2        # equivalent to rem(7, 2)
1
# Fraction
4//8
1//2
# Julia supports fraction operations
1//2 + 3//4
5//4

Variables

A variable is a name bound to a value:

a = 3;

It can be called:

a
3

Or used in expressions:

a + 2
5

Assignment

You can re-assign new values to variables:

a = 3;
a = -8.2;
a
-8.2

Even values of a different type:

a = "a is now a string"
"a is now a string"

You can define multiple variables at once:

a, b, c = 1, 2, 3
b
2

Variable names

These names are extremely flexible and can use Unicode character:

\omega       # press TAB
\sum         # press TAB
\sqrt        # press TAB
\in          # press TAB
\:emoji:     # press TAB
# for the last example, replace the word "emoji" by e.g. "phone" or "snail"
δ = 8.5;
🐌 = 3;
δ + 🐌
11.5

Admittedly, using emojis doesn’t seem very useful, but using Greek letters to write equations really makes Julia a great mathematical language:

σ = 3
δ = π
ϕ = 8

(5σ + 3δ) / ϕ
3.0530972450961724
Note how the multiplication operator can be omitted when this does not lead to confusion.
Also note how the mathematical constant π is available in Julia without having to load any module.

If you want to know how to type a symbol, ask Julia: type ? and paste it in the REPL.

The only hard rules for variable names are:

  • They must begin with a letter or an underscore,

  • They cannot take the names of built-in keywords such as if, do, try, else,

  • They cannot take the names of built-in constants (e.g. π) and keywords in use in a session.

false = 3
LoadError: syntax: invalid assignment location "false" around In[216]:1

In addition, the Julia Style Guide recommends to follow these conventions:

  • Use lower case,

  • Word separation can be indicated by underscores, but better not to use them if the names can be read easily enough without them.

The ans variable

The keyword ans is a variable which, in the REPL, takes the value of the last computation:

a = 3 ^ 2;
ans + 1
10

Printing

To print the value of a variable in an interactive session, you only need to call it:

a = 3;
a
3

In non interactive sessions, you have to use the println function:

println(a)
3

Collections

Values can also be stored in collections.

Tuples

Tuples are immutable, indexable, and possibly heterogeneous collections of elements. The order of elements matters.

# Possibly heterogeneous (values can be of different types)
typeof((2, 'a', 1.0, "test"))
Tuple{Int64, Char, Float64, String}
# Indexable (note that indexing in Julia starts with 1)
x = (2, 'a', 1.0, "test");
x[3]
1.0
# Immutable (they cannot be modified)
x[3] = 8
LoadError: MethodError: no method matching setindex!(::Tuple{Int64, Char, Float64, String}, ::Int64, ::Int64)

Named tuples

Tuples can have named components:

typeof((a=2, b='a', c=1.0, d="test"))
NamedTuple{(:a, :b, :c, :d), Tuple{Int64, Char, Float64, String}}
x = (a=2, b='a', c=1.0, d="test");
x.c
1.0

Dictionaries

Julia also has dictionaries: associative collections of key/value pairs:

x = Dict("Name"=>"Roger", "Age"=>52, "Index"=>0.3)
Dict{String, Any} with 3 entries:
  "Index" => 0.3
  "Age"   => 52
  "Name"  => "Roger"

"Name", "Age", and "Index" are the keys; "Roger", 52, and 0.3 are the values.

The => operator is the same as the Pair function:

p = "foo" => 7
"foo" => 7
q = Pair("bar", 8)
"bar" => 8

Dictionaries can be heterogeneous (as in this example) and the order doesn’t matter. They are also indexable:

x["Name"]
"Roger"

And mutable (they can be modified):

x["Name"] = "Alex";
x
Dict{String, Any} with 3 entries:
  "Index" => 0.3
  "Age"   => 52
  "Name"  => "Alex"

Sets

Sets are collections without duplicates. The order of elements doesn’t matter.

set1 = Set([9, 4, 8, 2, 7, 8])
Set{Int64} with 5 elements:
  4
  7
  2
  9
  8
Notice how this is a set of 5 (and not 6) elements: the duplicated 8 didn’t matter.
set2 = Set([10, 2, 3])
Set{Int64} with 3 elements:
  2
  10
  3

You can compare sets:

# The union is the set of elements that are in one OR the other set
union(set1, set2)
Set{Int64} with 7 elements:
  4
  7
  2
  10
  9
  8
  3
# The intersect is the set of elements that are in one AND the other set
intersect(set1, set2)
Set{Int64} with 1 element:
  2
# The setdiff is the set of elements that are in the first set but not in the second
# Note that the order matters here
setdiff(set1, set2)
Set{Int64} with 4 elements:
  4
  7
  9
  8

Sets can be heterogeneous:

Set(["test", 9, :a])
Set{Any} with 3 elements:
  :a
  "test"
  9

Arrays

Arrays are indexable and mutable collections. The order of elements matters and they can be heterogeneous. We will talk about them in a later section.

Quotes

Note the difference between single and double quotes:

typeof("a")
String
typeof('a')
Char
"This is a string"
"This is a string"
'This is not a sring'
LoadError: syntax: character literal contains multiple characters
'a'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

Comments & questions