1.2. Variables and Assignments#

A variable is a name used to store a value. Think of it as a labeled box where you can keep a piece of information.

1.2.1. Variable Naming Rules#

  • Characters: Variable names can contain most Unicode characters, but they cannot begin with a number.

  • Case-Sensitivity: Names are case-sensitive, so mass and Mass are treated as two different variables.

  • Style: Good variable names are descriptive and clearly state what the variable represents (e.g., user_age is better than x).

An assignment statement uses the = operator to create a variable and assign it a value. It has the form variable_name = expression.

my_number = 1.2    # Assigns the value 1.2 to the variable `my_number`.
1.2
pi                 # Julia has many pre-defined variables, like π.
π = 3.1415926535897...
My_number          # This will cause an error because `My_number` is not the same as `my_number`.
UndefVarError: `My_number` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
δ = 0.001          # Unicode characters are valid (type \delta then press Tab).
0.001
my_number_2 = (my_number + 1) * δ     # You can use existing variables in expressions.
0.0022

1.2.2. Updating Operators#

It’s common to perform an operation on a variable and assign the result back to itself (e.g., x = x + 1). Julia provides convenient updating operators as a shortcut for this:

Operator

Example

Equivalent To

+=

x += 1

x = x + 1

-=

x -= 1

x = x - 1

*=

x *= 2

x = x * 2

/=

x /= 2

x = x / 2

^=

x ^= 3

x = x ^ 3

%=

x %= 2

x = x % 2

counter = 1
1
counter += 1              # Increment the counter by 1.
2
counter ^= 4              # Raise the counter to the power of 4.
16
angle_degrees = 60
60
angle_degrees *= π / 180  # Convert the angle to radians.
1.0471975511965976

1.2.3. Numerical Literal Coefficients#

In Julia, you can place a numeric literal directly before a variable to imply multiplication. This feature makes code look more like standard mathematical notation, which can improve readability.

x = 3
println(2x^2 - 3x + 1)       # Equivalent to 2*x^2 - 3*x + 1
println(1.5x^2 - 0.5x + 1)   # Works with floating-point numbers too
println(2(x-1)^2 - 3(x-1) + 1) # Also works with parenthesized expressions
10
13.0
3

This notation has higher precedence than other multiplication and division operators, which can sometimes lead to surprising results:

println(360 / 2*π)  # Evaluates as (360 / 2) * π = 180π
println(360 / 2π)   # Evaluates as 360 / (2 * π) = 180/π
565.4866776461628
57.29577951308232

When used with exponentiation (^), numeric literal coefficients behave like unary operators:

println(2^2x)       # Interpreted as 2^(2*x), not (2^2)*x
println(2x^2)       # Interpreted as 2*(x^2), not (2*x)^2
64
18

1.2.4. Comments#

Good code is well-commented code. Comments are notes for human readers that are ignored by the computer.

  • Single-line comments start with #. Everything from the # to the end of the line is ignored.

  • Multi-line comments are enclosed between #= and =#.

Comments should be descriptive without stating the obvious. Use them to:

  • Provide a high-level description of a script or program.

  • Explain the purpose of important variables and constants.

  • Clarify the goal of a complex block of code.

1.2.4.1. Example of Good Commenting#

The comments in the following code make its purpose clear and easy to understand.

#=
  This script calculates the volume of a hollow sphere
  by subtracting the volume of an inner sphere from an outer one.
=#

radius_outer = 100     # Radius of the outer sphere (in cm)
radius_inner = 50      # Radius of the inner sphere (in cm)

# Calculate the volume of each sphere using V = (4/3)πr³
vol_outer = 4/3 * π * radius_outer^3
vol_inner = 4/3 * π * radius_inner^3

# The final volume is the difference between the two
vol_hollow = vol_outer - vol_inner
3.665191429188092e6