Check your understanding of the previous reading: valid and good variable names, and how variables differ from math.

Question 1

Choose which of the following are valid Python variable names. Note that they do not have to be good variable names, just valid.

Select every name that is a valid Python identifier, then check.

  • Not valid. A hyphen is not one of the allowed characters (letters, numbers, and underscores); Python would read this as subtraction.
  • Valid. It begins with an underscore, and every other character is a digit or underscore.
  • Valid. It begins with a letter and only uses letters and an underscore.
  • Valid. It begins with a letter and contains only letters.
  • Not valid. Spaces are not allowed in variable names; use an underscore instead (page_count).
  • Not valid. The quotation marks make this a string literal, not a variable name.
  • Valid. It begins with an underscore, which is allowed.
  • Valid. It begins with an underscore, and digits are allowed everywhere after the first character.
  • Not valid. Names must begin with a letter or underscore, never a digit.
  • Valid. Capital letters are letters too; the naming rules don't care about case.

Question 2

You have a variable that represents your allowance, measured in dollars. Which of the following are good variable names? In this case, "good" means that they are not too long, not too short, and the intent of the variable is unambiguous.

Select every name that is a good choice, then check.

  • Good. Clear, concise, and its meaning is unambiguous in this context.
  • Not good. Far too short to be clear; a reader has no way to guess what d holds.
  • Good. Still concise, and the unit makes the meaning even more precise.
  • Not good. Too vague; "data" could refer to anything and says nothing about allowances.
  • Not good. It describes the variable's type, not what it represents.
  • Not good. Far too long for what it represents; allowance says the same thing more concisely.

Question 3

True or false: a variable is useful because it allows you to solve for an unknown value.

Select one answer, then check.

  • Not quite. That describes a variable in math, not in programming.
  • Correct. In computing you always know a variable's value; you're manipulating it according to your instructions, not solving for an unknown like in math class.

Question 4

True or false: variable names are important because computers understand the meaning of names and change their value accordingly.

Select one answer, then check.

  • Not quite. Computers don't read any meaning into a variable's name at all.
  • Correct. Computers do not understand variable names; a name's meaning only matters to the humans reading the code.