Lesson 1 (Variables)

Variables

In programming, variables are placeholders for information you would like to store and use later. Variables are very common in algebra; for example, if we use x as a variable to store the value 3 (x = 3), then later we can recall this value 3 simply by recalling the variable x. We can also use variables to perform calculations, e.g. 2 * x will return the value 6. Finally, we can replace the value of a variable. That is, if we set x = 4, then the same calculation, 2 * x will return the value 8:

Notice that it would be impractical to name all variables x or y. Rather, it is convention to give informative names to your variables, so that the name of the variable conveys some information about the value it holds. For example, in the temperature calculation example, we could have created and calculated the answer with a variable tempInF = 75.

Now it's your turn!

  1. Imagine today is your 9th birthday. Create an informative variable to store your age (in years) called ageInYears. Calculate and print your age in days by multiplying ageInYears by the number of days in the year (365).

  2. Now substitute your actual age for the variable ageInYears and calculate your age in days.