Articles

2.2 Assignment Statement and Variables - Mathematics


Assignments

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

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

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, typealpha, 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

then you can think of the whiteboard looking like:

variablevalue
x6
y-1
z8.5

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

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

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

variablevalue
x6
y4
z8.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.


Watch the video: Coursera: Programming For Everybody Assignment solution Python for Everybody Exercise sol. (October 2021).