Variables and constants - Answers
Q1. All variable names and constants must begin with either the underscore character _ or a letter. They can then be followed by any number of letters or numbers or the underscore. Keywords (reserved words used by Python), white space and special characters are not allowed. Python is case-sensitive. Flipper is a different variable to flipper.
Q2. The key words in Python are:
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
There are 33 as stated here:
http://docs.python.org/3.0/reference/lexical_analysis.html
Q3. When selecting a variable or constant name, you will get an error if you try to use a keyword.
Q4. A variable name in Python can have more than 12 characters in it.
Q5. A variable name in Python is not allowed to use white space e.g. length of car is an illegal name.
Q6. Three characters from the keyboard that you are not allowed to use in a variable name include $ - & ! ^ etc. Students should have tested out illegal symbols for themselves.
Q7. PI = 3.14 Constants should be written in capital letters.
Q8. MONTHS_IN_YEAR = 12
Q9. You can actually still change a constant value, even if it is in capital letters. (There are workarounds but they are beyond the scope of these worksheets.)
Q10 - Q12. It is worth knowing about the id() function. Sometimes, two objects that appear different are actually the same. The way to check is to use the id() function on each object and see if they really are the same or different. Students should be aware of the id() function and its uses.