Get startedGet started for free

Variables

1. Variables

In the previous exercises you were calculating sums and printing the answers all at once in a single line of code.

2. Assigning variables

In order to carry out more complex tasks, we need to use variables and split our calculations across multiple lines. Variables allow us to store values under a name. In this example, we store the value of three under the variable name x. After this, we can print x and see the value assigned to it. We can also perform further calculations with x, such as multiplying it by two and printing the results of this.

3. Calculating with variables

Using variables makes our programs neater and more reusable. Say we wanted to write a script to find our average speed when going for a run. If we are training to get faster, we might want to use the script repeatedly to track our progress. In this script, we assign values to a variable named distance, which stores the distance we ran in meters, and a variable named time, which holds the number of minutes we took to complete the run. We want to calculate our average speed over the run in miles per hour, so we convert the distance to miles and the time to hours and assign these to new variables. Finally, we print our speed for this run. Keeping all of this in a script means we can calculate our new speed when we have new run times. Perhaps after a few weeks, we have gotten a bit faster, and now we can complete the same distance in less time.

4. Calculating with variables

We can return to the script, update the time taken and rerun it to find our new average speed.

5. Naming variables

In the previous script, we named our variables using Julia's best practices. In Julia, variables should all start with a letter, although we can use Unicode symbols too. When the variable name is a word, the letters should all be lowercase, and when the name is multiple words, we should separate them with an underscore. We can leave out the underscores if the name is still readable. Variable names can also use numbers after the first letter, but they cannot begin with a number.

6. More operations using variables

Using variables allows us to run more complex operations. In the previous exercises, we used the addition and subtraction operators. In this lesson, we have used multiply and divide. Another primary operator we should know is the power operator, which raises a number to a power. Sometimes we want to use a few of these operations in a single expression. The operations are resolved in the same as the order used in mathematics. However, we can use brackets to control the order in which the operations are resolved. For example, we could simplify our script to calculate our running speed to this expression. We divide the distance by one-thousand-six-hundred-and-nine and divide this by the time divided by sixty. The expressions inside the brackets are computed first, giving the same answer as before. If we didn't use the brackets in this expression, we would divide the distance by each of the three other quantities, which wouldn't give the correct answer.

7. Let's practice!

Now, let's practice using variables in the exercises.