Using the REPL
OverviewTeaching: 18 min
Exercises: 2 minQuestions
How to use the REPL?Objectives
Explore basic functionality of input.
Learn how to declare variables.
Learn about REPL modes.
Entering the REPL
Melissa and her classmates open a terminal and launch
_ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.7.0 (2021-11-30) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia>
This is the so-called REPL, which stands for read-evaluate-print-loop. The interactive command-line REPL allows quick and easy execution of Julia statements.
Like the terminal, the Julia REPL has a prompt, where it awaits input:
Most of the code boxes that follow do not show the
julia>prompt, even though it’s there in the REPL. Why?
It’s important to delineate input (what you type) and output (how the machine responds). The prompt can be confusing, so it is excluded. You may assume that any Julia box prepends the prompt on each line of input.
The first thing they try is to perform basic arithmetic operations:
1 + 4 * 7.3
That works as expected.
It is also possible to bind a name to a value via the assignment operator
which makes it easier to refer to the value later on.
These names are called variables.
distance = 30.2 distance_x_2 = 2 * distance
Melissa notices that assignment also returns the value. She can also check which variables are defined in the current session by running
name size summary –––––––––––––––– ––––––––––– ––––––– Base Module Core Module InteractiveUtils 270.164 KiB Module Main Module ans 8 bytes Float64 distance 8 bytes Float64 distance_x_2 8 bytes Float64
In Julia, Unicode characters are also allowed as variables like
α = 2.
Unicode characters can be entered by a backslash followed by their LaTeX
name and then pressing tab (in this case
Unfortunately Melissa can’t remember the LaTeX name of ∂ so she copies the character, presses ? for help mode,
pastes the ∂ character, then presses enter:
"∂" can be typed by \partial<tab>
Great! This way she can easily look up the names she needs. She gets back to normal mode by pressing backspace.
Another useful mode is the shell mode that can be entered by pressing ;. The prompt has now changed:
Shell mode can be used to issue commands to the underlying shell, but don’t confuse it with an actual shell: special shell syntax like piping won’t work. Like before, hit backspace to get back to the Julia prompt.
Use the shell mode to create a file called
hello.jlwith the nano terminal text editor. Inside that file write the simple hello world program
Check the content of the file using
cat hello.jland then run the program using
shell> nano hello.jl shell> cat hello.jl
shell> julia hello.jl
Finally there is package mode that is entered with ] which is used for package management, which will be covered later on:
To exit shell, help or pkg mode, hit backspace.
The REPL reads the given input, evaluates the given expression and prints the resulting output to the user.
Pressing ? enters help mode.
Pressing ; enters shell mode.
Pressing ] enters pkg mode.