8/26/26About 1 minAbout 176 words
Concept: Naming Variables
When naming variables in Python, there are some important rules you should keep in mind:
- Variable names can only start with a letter or an underscore (
_), not a number. - Variable names can only contain alphanumeric characters (
a-z,A-Z,0-9) and underscores (_). - Variable names are case-sensitive;
age,Age, andAGEare all considered unique. - Variable names cannot be one of Python's reserved keywords such as
if,class, ordef.
While the following are the hard rules, there are also some soft rules that are widely accepted in the Python community:
- Variables names should be in lowercase, with separate words separated by an underscore. This is called snake case.
- Use descriptive names for variables. For example, if you want to save a user's age as a variable,
user_ageis better thanageor an abbreviation likeua. - Avoid using single-character variable names except for iteration variables or the parameter of simple lambda functions.