Skip to main content
Mathematics LibreTexts

2.2 Assignment Statement and Variables

  • Page ID
    53705
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Assignments

    Anything can be stored as a variable using the single equal sign like

    x=6

    This is an assignment operator, which creates the number 6 and stores it under the namex.

    And now that the variable is stored, we can use it in calculations. For example

    x+3
    9

     

    Variables in julia, much like other languages are primarily sequences of alphanumeric characters as well as an underscore . Primarily, a variable needs to start with a alphabetic character or and after the first character can contain numbers.

    Julia also allows many unicode symbols in variable names, however not everything. For example, all of the greek letters are allowed, so \(\alpha=45\) is valid.

    To get a greek letter in Jupyter or the REPL, type \alpha, hit the TAB key and it will be turned into an \(\alpha\).

    Storing Variables in a Virtual Whiteboard

    The details of storing variables in computer hardware isn’t necessary, however, thinking of storing as writing variables and values on a whiteboard is a helpful paradigm. Imagine a whiteboard with a column of variable names and a column of values. For example, if we have

    x=6
    y=-1
    z=8.5
    8.5

     then you can think of the whiteboard looking like:

    variable value
    x 6
    y -1
    z 8.5

     

    If we evaluate any expression containing any of these variables, then the value looked up substituted into the expression.  For example, 

    2*y
    -2

    looks up the value of y (which is -1) and substitutes that value in and multiplies the result by 2.  As you can see the result is -2.

    If we change one of the values, like

    y = y+5

    this means that the right hand side is evaluated by looking up the value of y and the result is 4.  Then the 4 is placed into the whiteboard, which will now look like:

    variable value
    x 6
    y 4
    z 8.5

    If you are thinking of how a piece of code works, often you will need to get to the point of writing down a version of the whiteboard.


    2.2 Assignment Statement and Variables is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?