1.3. Python Variables#
A variable is a portion of memory uses to store a value. The Python syntax for declaring a variable is
variable_name = value
where variable_name
is the name which is used to refer to the variable and value
is the value that is stored in the memory. Lets declare a variable and assign a value to it. In the console enter the following and press the enter key.
In [17]: a = 2
You may be a bit disappointed to see that nothing happens. In fact Python has stored the value of 2 into the memory which can be accessed using the variable name a
. We can retrieve the value of our variable by typing a
into the console and press the enter key.
In [18]: a
Out[18]: 2
So Python has retrieved the value of a
for us. We can change the value of a
simply by assigning another value. In the console enter the following and press the enter key.
In [19]: a = 3
and retrieve the value of a
again
In [20]: a
Out[20]: 3
Lets use variables to calculate the area of a rectangle. Enter the following into the console.
In [21]: width = 4
In [22]: height = 3
In [23]: area_of_rectangle = width * height
In [24]: area_of_rectangle
Out[24]: 12
Here we have created two variables width
and height
to store the width and height of a rectangle. We then create a third variable area_of_rectangle
which stores the area of the rectangle calculated using the values stored in the other two variables. We then retrieve the area of the rectangle.
Python also allows us to declare multiple variables on a single line. Enter the following into the console.
In [25]: base, height = 2, 1
In [26]: area_of_triangle = base * height / 2
In [27]: area_of_triangle
Out[27]: 1.0
Here we have declared two variables base
and height
and set their values to 2 and 1 respectively (this overwrites the previous value of height
). Then we have calculated the area of a triangle and stored it in the variable area_of_triangle
and retrieved it.
1.3.1. Variable names#
The choice of variable name is up to us but it must satisfy the following rules:
a variable name must start with a letter or the underscore (
_
) charactera variable name can only contain alpha-numeric characters and underscores (
a
-z
,A
-Z
and_
)a variable name is case sensitive, i.e.,
age
,Age
andAGE
are three different variablesa variable name cannot by any of the Python keywords
Important
Care must be taken when choosing variable names not to use a name already used by Python or a Python library. For example, if we use a variable name print
then we will not by able to use the print()
command (see below) since this will have been overwritten by our variable.
An example of a variable name that would violate Python’s naming rules is base length
. Enter the following into the console
base length = 2
When you press enter the following is printed to the console
In [28]: base length = 2
Cell In[28], line 1
base length = 2
^
SyntaxError: invalid syntax
Here Python is telling us that our variable name is invalid syntax. We can correct this using the variable name base_length
, enter the following into the console
In [29]: base_length = 2
which Python has happily dealt with. It is good practice to use variable names that are descriptive of what the variable represents, e.g., using length
for the length is easier to read and understand then l
(try to avoid lowercase l
are its easy to confuse then with the characters 1
and I
). The use of underscores _
in place of spaces in variable names is known as pothole case or snake case is common in Python as it makes code easier to read.
1.3.2. Exercise#
The following formula converts a temperature from degree Fahrenheit to degrees Centigrade
Using appropriate variable names, convert the following temperatures from degrees Fahrenheit to degrees centigrade:
37.8\(^\circ\)C
100\(^\circ\)C
\(-\)40\(^\circ\)C