Variables

Introduction to Julia

James Fulton

Climate informatics researcher

Assigning variables

# Store the number 3 in the variable x
x = 3

# Print the variable println(x)
3
double_x = 2*x
println(double_x)
6
Introduction to Julia

Calculating with variables

# Distance run in meters
distance = 5000

# Time taken in minutes
time = 32.3

# Convert distance to miles and time to hours distance_miles = distance/1609
time_hours = time/60
# Print speed in miles per hour println(distance_miles/time_hours)
5.772483341575157
Introduction to Julia

Calculating with variables

# Distance in meters
distance = 5000

# Time taken in minutes

time = 30.1
# Convert distance to miles and time to hours distance_miles = distance/1609 time_hours = time/60 # Print speed in miles per hour println(distance_miles/time_hours)
6.194392423019188
Introduction to Julia

Naming variables

Variable names in previous example:

distance, time, distance_miles, time_hours

  • Must begin with letter [a-z] or [A-Z]
  • Or other unicode characters e.g. $\phi$, $\beta$, $\sigma$
  • Names of variables are in lower case
  • Words separated using by underscores _
  • Underscores can be omitted if still readable
    • e.g. time_hours $\rightarrow$ timehours
  • Can use numbers after first letter
    • x_times_2 is valid
    • 2_times_x is not valid
Introduction to Julia

More operations using variables

Operator Operation Example Result
+ Add 1+2 3
- Subtract 4-2 2
* Multiply 5*3 15
/ Divide 20/4 5
^ Power 2^3 8
  • 2^3 is the same as 2*2*2
speed = (distance/1609)/(time/60)
println(speed)
5.772483341575157
speed = distance/1609/time/60
println(speed)
0.0016034675948819882
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...