In the previous exercise, we looked at how to define a variable. Now, we are going to look at how we can use a variable to perform a simple calculation.
Let's say we would like to calculate the perimeter of a rectangle given
two variables: height
and width
. We would do that as follows:
height = 8 width = 24 perimeter = 2 * (height + width)
To refer to a variable, we can simply use its name. When running the code,
Python will replace the name of the variable by its value, so it will
calculate the variable perimeter
as 2 * (8 + 24)
.
In this example, we are using the +
and *
operators to add and
multiply values, respectively. Later we will look into operators in more
depth.
Create the variables height
and width
with their values set to
8 and 24, as above. Then, create another variable called area
, which is
calculated as the product between the height
and width
variables.