A variable name must:
start with a letter or the underscore character
not start with a number
contain only alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variables breaking these rules will cause your code to error.
A good variable name should:
represent what something is
not be excessively long
not be one-letter or over-abbreviated
not be easily confused with another variable
Remember, a programmer spends more time reading code than writing code. Good variable naming makes code easier to read, share, and debug.
For example, first_name would be a good variable name, while the following might not be:
var3Â (doesn't indicate what data it holds)
player_one_first_name (this might be fine, but p1_first_name is shorter while retaining meaning)
f or fn (too short to show meaning)
name (confused with last name, if there is one)
Knowing the variable naming rules are important, but when working with others (which you almost always are, when programming) it's very important to stick to the conventions of your language (or sometimes, workplace).
It's incredibly frustrating when different programmers use different conventions. For example, imagine all the ways we could name a variable to store first name:
FirstName (PascalCase or UpperCamelCase)
firstName (camelCase)
first_name (snake_case)
FIRST_NAME (MACRO_CASE)
firstname or FIRSTNAME (these are particularly bad and hard to read)
Keeping conventions constant allows you to not have to worry about accidentally not matching case, or having/missing an underscore between multiple words. This can be a lifesaver in a program with hundreds of variables.
Python generally uses snake_case for variables and functions. (All lower case, with additional words separated by underscore.)