Skip to content

Naming Convention

Variable Naming Conventions

A variable can have a short name (like xx and yy) or a more descriptive name (like ageage, carnamecarname, total_volumetotal_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • The reserved words (keywords) cannot be used naming the variable
  • Variable names should be short but descriptive
  • Use of underscores (__) is recommended to improve readability
  • Avoid using special characters like !!, @@, ##, $$, %%, etc. in variable names
  • Avoid using built-in function names as variable names
  • Avoid using single characters like ll, OO, II, etc. as variable names
  • Avoid using words with double meaning like listlist, strstr, dictdict, etc. as variable names
  • Avoid using words with different spellings like colourcolour and colorcolor, centrecentre and centercenter , etc. as variable names

Example:

variable.py
# Valid variable names
name = "John"
my_name = "John"
_my_name = "John"
myName = "John"
MYNAME = "John"
myname = "John"
myName2 = "John"
my2name = "John"
variable.py
# Valid variable names
name = "John"
my_name = "John"
_my_name = "John"
myName = "John"
MYNAME = "John"
myname = "John"
myName2 = "John"
my2name = "John"

B

Constant Naming Conventions

Constants are usually declared and assigned in a module. Python does not have built-in constant types, but Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed after it is initialized.

Example:

constant.py
# Valid constant names
PI = 3.14
GRAVITY = 9.8
PLANET = "Earth"
constant.py
# Valid constant names
PI = 3.14
GRAVITY = 9.8
PLANET = "Earth"

Function Naming Conventions

Function names should be lowercase, with words separated by underscores as necessary to improve readability. Mixed case is allowed only in contexts where that’s already the prevailing style (e.g. threading.pythreading.py), to retain backwards compatibility.

Example:

function.py
# Valid function names
def my_function():
    pass
 
def myFunction():
    pass
function.py
# Valid function names
def my_function():
    pass
 
def myFunction():
    pass

Class Naming Conventions

Class names should normally use the CapWords convention.

Example:

class.py
# Valid class names
class MyClass:
    pass
class.py
# Valid class names
class MyClass:
    pass

Naming Convention Standards

Python has a set of naming conventions for different types of identifiers. These conventions are defined in PEP 8 — Style Guide for Python Code, which is the style guide that most Python projects follow.

There are different naming styles for different types of identifiers. The following table summarizes the naming conventions for different types of identifiers:

Camel Case

Starts each word with a capital letter except the first word. For example: firstNamefirstName, lastNamelastName, getFirstName()getFirstName(), setFirstName()setFirstName(), etc.

variable.py
# Camel Case
firstName = "John"
lastName = "Doe"
def getFirstName():
    pass
def setFirstName():
    pass
variable.py
# Camel Case
firstName = "John"
lastName = "Doe"
def getFirstName():
    pass
def setFirstName():
    pass

Pascal Case

Starts each word with a capital letter. For example: FirstNameFirstName, LastNameLastName, GetFirstName()GetFirstName(), SetFirstName()SetFirstName(), etc.

variable.py
# Pascal Case
FirstName = "John"
LastName = "Doe"
def GetFirstName():
    pass
def SetFirstName():
    pass
variable.py
# Pascal Case
FirstName = "John"
LastName = "Doe"
def GetFirstName():
    pass
def SetFirstName():
    pass

Snake Case

Uses underscores (__) between words. For example: first_namefirst_name, last_namelast_name, get_first_name()get_first_name(), set_first_name()set_first_name(), etc.

variable.py
# Snake Case
first_name = "John"
last_name = "Doe"
def get_first_name():
    pass
def set_first_name():
    pass
variable.py
# Snake Case
first_name = "John"
last_name = "Doe"
def get_first_name():
    pass
def set_first_name():
    pass

Was this page helpful?

Let us know how we did