0% found this document useful (0 votes)
5 views2 pages

04VariablesandConstants Help

The document discusses the concepts of variables and constants in Python programming, explaining that variables can change during program execution while constants remain fixed. It emphasizes the importance of understanding these concepts for students, particularly for exams, and provides guidelines for naming variables. Two common naming conventions, camel case and snake case, are introduced to help maintain clarity and consistency in variable names.

Uploaded by

r.anthony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

04VariablesandConstants Help

The document discusses the concepts of variables and constants in Python programming, explaining that variables can change during program execution while constants remain fixed. It emphasizes the importance of understanding these concepts for students, particularly for exams, and provides guidelines for naming variables. Two common naming conventions, camel case and snake case, are introduced to help maintain clarity and consistency in variable names.

Uploaded by

r.anthony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

04 – Variables and Constants

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.

Look at the following program:

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.

04 – Variables and Constants Page 1


© Nichola Wilkin Ltd 2020
www.nicholawilkin.com
Why do you need to know this?
Python does not really care if a value is a variable or a constant. You don’t need to
define it separately but some other languages are not as forgiving. They require you
to define the variables in a more formal manner and specify if it is a variable or
constant at the beginning. Even though Python does not make the distinction,
students are expected to know the difference between a variable and a constant
for their exams so it is an area you will need to teach them.

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:

• Must make sense

• Must start with a letter (not a number)

• 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.

There are ways to get around this:

• 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.

04 – Variables and Constants Page 2


© Nichola Wilkin Ltd 2020
www.nicholawilkin.com

You might also like