Julia Fundamentals
Last updated on 2026-02-23 | Edit this page
Overview
Questions
- What basic data types can I work with in Julia?
- How can I create a new variable in Julia?
- How do I use a function?
- Can I change the value associated with a variable after I create it?
Objectives
- Assign values to variables.
Variables
Any Julia REPL or script can be used as a calculator:
OUTPUT
23
This is great, but not very interesting. To do anything useful with
data, we need to assign its value to a variable. In Julia, we
assign a value to a variable using the equals sign =. For
example, we can track the weight of a patient who weighs 60 kilograms by
assigning the value 60 to a variable
weight_kg:
Now, whenever we use weight_kg, Julia will substitute
the value we assigned to it. In simple terms, a variable is a
name for a value.
In Julia, variable names:
- can include letters, digits, and underscores
- cannot start with a digit
- are case sensitive
This means that:
-
weight0is valid, but0weightis not -
weightandWeightrefer to different variables
Types of Data
You probably know that computers work with sequences of bits. Bits can have either one of two states, which is commonly denoted using the numbers 0 and 1. A data type specifies how a given sequence of bits is to be interpreted.
Julia supports various data types. Common ones include:
- Integer numbers
- Floating point numbers
- Strings
For example, weight_kg = 60 creates an integer
variable. If we want to represent a fractional number, we can use a floating
point number:
To store text, we create a string by using double quotes:
Using Variables in Julia
Once we’ve assigned variables to values, we can use them in calculations:
OUTPUT
132.66
Or modify strings:
OUTPUT
"inflam_001"
Built-in Julia Functions
Functions are called with parentheses. You can include variables or
values inside them. Julia provides many built-in functions. To display a
value, we use print or println, which adds a
newline at the end of the output:
OUTPUT
132.66
inflam_001
To display multiple values in Julia, we can pass them to
println separated by commas.
This prints the value of patient_id, followed by the
string " weight in kilograms: ", and then the value of
weight_kg, all in one line.
In Julia, every value has a specific data type (e.g., integer,
floating-point number, string). To check the type of a value or
variable, use the typeof function:
OUTPUT
Float64
String
In this example:
-
60.3is interpreted as a 64-bit floating-point number (specifically, aFloat64). -
patient_idcontains a sequence of characters, so its type isString.
Understanding data types is important because they determine how values behave in operations, and some functions may only work with certain types.
You can also use typeof to explore the structure of more
complex objects like arrays or dictionaries:
OUTPUT
Vector{Int64}
Vector{String}
We can even do math directly when passing arguments to
println:
OUTPUT
weight in pounds: 132.66
The above doesn’t change weight_kg:
To change the value of the weight_kg variable, we have
to assign weight_kg to a new value
OUTPUT
weight in kilograms is now: 65.0
Check Your Understanding
What values do the variables mass, speed
and age have?
JULIA
mass = 50.0
age = 56
speed = "fast"
println("very " * speed)
mass = mass * 2.0
age_new = age - 20
mass == 50.0, speed == "fast", age == 56mass == 100.0, speed == "very fast", age == 56mass == 100.0, speed == "fast", age == 56mass == 100.0, speed == "fast", age == 36
-
massindeed gets reassigned atmass = mass * 2.0. -
println("very " * speed)prints “very fast” as output, but does not alterspeeditself. - Thats the correct solution
-
age_new = age - 20binds the result ofage - 20to a new variable and does not changeageitself.
OUTPUT
Hello World!
(Note: println prints without space by default. We
insert a space by adding a string with just one space character
" ".)
- Basic data types in Julia include integers, strings, and floating-point numbers.
- Use
variable = valueto assign a value to a variable. - Use
println(value)to display output. - Julia provides many built-in functions, such as
typeof.