Julia Fundamentals

Last updated on 2026-01-27 | Edit this page

Estimated time: 12 minutes

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:

JULIA

3 + 5 * 4

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::

JULIA

weight_kg = 60

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:

  • weight0 is valid, but 0weight is not
  • weight and Weight refer to different variables

Types of Data


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 a more precise value, we can use a floating point value:

JULIA

weight_kg = 60.3

To store text, we create a string by using double quotes:

JULIA

patient_id = "001"

Using Variables in Julia


Once we’ve stored values in variables, we can use them in calculations:

JULIA

weight_lb = 2.2 * weight_kg

OUTPUT

132.66

Or modify strings:

JULIA

patient_id = "inflam_" * patient_id

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 println or print:

JULIA

println(weight_lb)
println(patient_id)

OUTPUT

132.66
inflam_001

To display multiple values in Julia, we can pass them to println separated by commas.

JULIA

println(patient_id, " weight in kilograms: ", weight_kg)

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:

JULIA

typeof(60.3)
typeof(patient_id)

OUTPUT

Float64
String

In this example:

  • 60.3 is interpreted as a floating-point number (specifically, a Float64).
  • patient_id contains a sequence of characters, so its type is String.

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:

JULIA

typeof([1, 2, 3])      # Array of integers
typeof(["a", "b", "c"]) # Array of strings

OUTPUT

Vector{Int64}
Vector{String}

We can even do math directly in println:

JULIA

println("weight in pounds: ", 2.2 * weight_kg)

OUTPUT

weight in pounds: 132.66

The above doesn’t change weight_kg:

JULIA

println(weight_kg)

To change the value of the weight_kg variable, we have to assign a new value to weight_kg

JULIA

weight_kg = 65.0
println("weight in kilograms is now: ", weight_kg)

OUTPUT

weight in kilograms is now: 65.0
Challenge

Check Your Understanding

What values do the variables mass and age have after each line?

JULIA

mass = 50.0
age = 56
println(mass * 2.0)
mass = mass * 2.0
age_new = age - 20

OUTPUT

50.0
56
50.0
100.0
56 
Challenge

Sorting Out References

Julia allows multiple assignments in one line. What will this print?

JULIA

first, second = "Hello", "World!"
println(first," ", second)

OUTPUT

Hello World!

(Note: println prints without space by default. We insert a space by adding a string with just one space character " ".)

Challenge

Seeing Data Types

What are the types of the following?

JULIA

planet = "Earth"
apples = 5
distance = 10.5

JULIA

typeof(planet)
typeof(apples)
typeof(distance)

OUTPUT

String
Int64
Float64
Key Points
  • Basic data types in Julia include integers, strings, and floating-point numbers.
  • Use variable = value to assign a value to a variable.
  • Use println(value) to display output.
  • Julia provides many built-in functions, such as typeof.