04VariablesandConstants Help
04VariablesandConstants Help
Lesson Notes
from the
Teach Python Programming With Confidence Masterclass
By Nichola Wilkin Ltd
Values
So far, we have been referring to values that are stored by the computer as values
but these would be better referred to by their correct name of either variables or
constants.
There are 4 variables used in this program; they are name, num1, num2 and total. If
you look at the code, you will see that the variable is identified first followed by an =
and then a statement about how that variable is to be generated. Some people
find it easier to think of the = as “becomes”. Therefore, if we look at the first line
name becomes whatever the user types in when prompted for their name. Similarly,
num1 becomes whatever integer they type in when prompted and num2 becomes
whatever integer they type in for that question. Finally, total becomes the answer of
num1 + num2.
Variables
Variables are values that are changed as the program is running.
For instance, if you are asking a user to input their name then “name” would be a
variable as it is changing from not having a value to having a value. In the example
given above num1, num2 and total are also variables.
Constants
A constant is different to a variable in that it does not change its value during the
running of the program. Have a look at the following program:
Here we are using the vatRate which is set to 0.2 (20%). This will not change as the
program is running so this would be an example of a constant. The originalPrice, vat
and newPrice would all be variables in this example.
Naming variables
As Python does not make the distinction between a variable or a constant I tend to
refer to them both as variables as it makes explanations easier, so even though this
section is entitled “Naming variables” it could have just as easily been called
“Naming variables and constants”. But for simplicity I will only refer to them both as
variables from this point forward.
When naming variables there are a few rules you must adhere to:
• No mathematical symbols
• No brackets
• No logical symbols (> or =)
• No spaces
• Be consistent
Therefore, when naming variables you need to be descriptive but you cannot use
any spaces.
• Camel case – This is where the first letter is in lowercase but the first letter of
any subsequent word is in upper case and the spaces are removed.
Therefore, first name would become firstName. This is quite a common
method for naming variables and is called camel case as you get a hump in
the middle.
• Snake case – to be honest I’ve only seen this style of naming convention
referred to once as snake case (but it is a style I have seen a lot). However,
nobody really knows what it is called so I will also refer to it as snake case.
Snake case is when the spaces are replaced with the underscore symbol. So,
first name would become first_name.
It doesn’t really matter which style you prefer but it makes sense to keep to one or
the other as it makes it easier to remember what you may have called variables on
longer programs.