The Julia programming language is fast, dynamically typed, easy to read and write, and open source. Julia is known for being used in Artificial Intelligence/Machine Learning. The language is pretty easy to pick up, and a lot of fun to write when you get the basics down.
The Environment
You have plenty of options when setting up your environment to start programming in Julia. First, though, you need to download and install Julia.
There is a list of IDEs and Editors when you scroll down on https://julialang.org/.
The editor that I use, personally, is Visual Studio Code. To use VS Code to write programs in Julia, you will need to install the Julia extension:
In VS Code, all you need to do after installing Julia and the Julia extension is create a new file, and save the file with a .jl file extension.
The Julia download comes with its own REPL (Read, Evaluate, Print, Loop). This allows you to test drive lines of code to see what they do or if you are looking for a problem in your code. You can also use Jupyter notebooks for this. Just click the Julia option and when the notebook appears, go to File > New Notebook > Julia 1.4.1.
You can also check the Julia online documentation.
The Very Basics
Getting started, I found the Julia documentation and an Intro to Julia YouTube tutorial posted on the Julia Language’s YouTube channel to be extremely helpful.
PERL
The REPL that comes with the Julia language when you download and install it is extremely helpful to to test and see what lines do if you are ever unsure.
When you write lines of code in the REPL, the last line will output to the console. To suppress this output, add a semicolon (;) to the last line.
NOTE: Semicolons are not required in Julia, and aren’t used often.
Hello World
To print a line in Julia, you will use the print() or println() functions. The println() function will add a new line after the message you print. Two ways to write your first program in Julia, the “Hello World” program are:
print("Hello World")
println("Hello World");
Comments
You will also need to know how to comment your code. For a single line comment, you will use the pound sign (#), and for a multi-line comment, the pound sign followed by the equal sign (#=) to open the comment and the equals sign followed by the pound sign to close it (=#). Example:
# This is a single line comment
#= This is
a multi-line
comment =#
Variables
Next, it’s important to know how variables work. You don’t specify a type for variables, Julia is dynamically typed. A variable type just depends on what type of value the variable is currently holding, and you can even reassign the variable a value of a new type. A variable can hold anything including integers, floats, characters, strings, Booleans, arrays, and any other type. To assign variables, you will enter a variable name followed by the assignment operator (=) and the value you wish to assign to the variable. For example:
firstVariable = 4
If you want to make a string, you would use double quotation marks (“example”) or three sets of double quotation marks (“””another example”””). When you use three sets of quotation marks, it allows you to use double quotation marks within your string. Single quotation marks (‘_’) are used to create a character, so you can only have one character within single quotation marks.
Arrays
Arrays are created with square brackets ([]) and they can contain anything, they may contain a mixture of different types.
NOTE: The indexing starts at 1 rather than 0 like many other languages.
Some methods that are helpful when working with arrays push!() and pop!(). push!() adds a new element to the end of an array and pop!() removes and element from the end of an array.
You can also pass more than one argument to the print() and println() functions, separated by commas. Variables can be included in strings using the dollar sign ($). For example:
name = "Cindy"
print("Hello, ", name)
does the same thing as:
print("Hello, $name")
NOTE: Variables need to be assigned before they can be used.
Arithmetic Operators
It is also important to know the arithmetic operators. Some of these are the same as other languages. Julia is specifically comparable to python for the most part, but there are some variations, of course. The arithmetic operators are:
Operator | Description | Example |
+ | Addition | x + y |
– | Subtraction | x – y |
/ | Division | x / y |
÷ | Integer Division | x ÷ y |
\ | Inverse Division | x \ y |
* | Multiplication | x * y |
^ | Power | x ^ y |
% | Modulus (Remainder) | x % y |
+=, -=, /=, ÷=, \=, *=, ^=, %= | Updating arithmetic operators | x += y is the same as x = x + y |
NOTE: Unlike many other languages, the increment (++) and decrement (—) operators are not supported, instead, the updating arithmetic operators are available.
The following is a table of comparison operators in the Julia language.
Operator | Description |
== | Equality |
!= | Inequality |
< | Less Than |
<= | Less Than or Equal To |
> | Greater Than |
>= | Greater Than or Equal To |
The following is a table of logical operators in the Julia language:
Operator | Description | Example |
! | NOT | !true == false |
|| | OR | true | false == true |
& | AND | true & false == false |
xor | XOR (EXCLUSIVE OR) | true xor false == true |
Conditionals and Loops
Finally, conditionals and loops. The if…else statement in Julia is very similar to other languages. The keywords that you will need to know for this are if, ifelse, else, and end. An if…else conditional may look like this:
if #condition
#instructions
elseif #condition
#instructions
else
#instructions
end
In Julia, you can use a for loop and a while loop. The while loop is set up to execute the code between the condition and the keyword end until the condition no longer evaluates to true:
while #condition
#instructions
end
The for loop loops through a range or collection like an array as a for each loop. It is set up as shown below:
for element in range
#instructions
end
Conclusion
From the information in this tutorial, you should have enough information to get started with Julia. Next time, we will expand more on the topics brushed over in this tutorial and introduce some more Julia features that are more unique and advanced.