VariablesVariables

All you have done in the previous chapters was great! But what if we want to use some of the numbers/results in the future? How can we save them?

The answer is simple - you need to use variables. Variable is like local storage for a specific value in computer memory. If we want to use them in the future, we need to refer to them. Therefore we need to name them somehow.

There are multiple rules to follow while naming a variable. These are:

  • variable's names can not start with a number or dot followed by a number;
  • variable's names can not have a % sign;
  • variable's names can not start with underline _ ;
  • variable's names can start with a dot only if not followed by a number; Note that these rules are not universal for all programming languages.

A good practice is to invest a particular meaning into a variable's name. For example, if you have the value 2020 representing a year, there is more sense in naming such a variable as year, yr, but not as a, b or something like that. This is not compulsory but recommended - this will ease the readability of your code for both yourself and other people.

To assign a value to a variable, use the = sign right to the variable name and left to value. For example, to assign 2020 to the variable year, we can use year = 2020.

Task

Let's return to the deposit task. This time let's save all the data within certain variables by using the = sign.

  1. Save the initial amount of money (2000) within the initial_money variable.
  2. Save 13 within the interest_rate variable.
  3. Save 4 within the n_years variable.
  4. Output all three values using cat() function in the same order as creation (initial_money, interest_rate, n_years).

Everything was clear?

Section 1. Chapter 7
toggle bottom row