Skip to content

Style guidelines for identifiers (Python 3)

Derek Lewis edited this page Jul 20, 2023 · 2 revisions

Style guidelines for identifiers

A good practice when naming variables is to use all lowercase letters and to place underscores between words. This lowercase and underscore convention for naming variables originates from the Python style guide, PEP 8. PEP 8 (PEP is an acronym for Python Enhancement Proposal) is a document that outlines the basics of how to write Python code neatly and consistently. Code is read more often than written, so having a consistent variable naming scheme helps to ensure that programmers can understand each other's code.

Programmers should create meaningful names that describe an item's purpose. If a variable will store a person's age, then a name like "age" is better than "a". A good practice when dealing with scientific or engineering names is to append the unit of measure, for example, instead of temperature, use temperature_celsius. Abbreviations should only be used if widely understandable, as in tv_model or ios_app. While meaningful names are important, very long variable names, such as "average_age_of_a_UCLA_graduate_student," can make subsequent statements too long and thus hard to read, so programmers find a balance between meaningful names and short names. Below are some examples of names that perhaps are less meaningful and more meaningful.

Table 2.2.1: Use meaningful variable names.

Purpose Less meaningful names More meaningful names
The number of students attending UCLA ucla
num
nu
num_students_ucla
qty_ucla_student_body_pop
The size of a television set measured as its diagonal length sz_tv
size
diagonal_tv_size_inches
The word for the ratio of a circle's circumference/diameter p pi
The number of jelly beans in a jar, as guessed by a user guess
num
njb
num_guessed_jelly_beans
user_guess_jelly_beans

Feedback?

A list of reserved keywords in the language are shown below:

Table 2.2.2: Python 3 reserved keywords.

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

Feedback?

Source: http://docs.python.org/3/reference/lexical_analysis.html