Naming Convention
Variable Naming Conventions
A variable can have a short name (like x
x
and y
y
) or a more descriptive name (like age
age
, carname
carname
, total_volume
total_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
l
l
,O
O
,I
I
, etc. as variable names - Avoid using words with double meaning like
list
list
,str
str
,dict
dict
, etc. as variable names - Avoid using words with different spellings like
colour
colour
andcolor
color
,centre
centre
andcenter
center
, etc. as variable names
Example:
# Valid variable names
name = "John"
my_name = "John"
_my_name = "John"
myName = "John"
MYNAME = "John"
myname = "John"
myName2 = "John"
my2name = "John"
# 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:
# Valid constant names
PI = 3.14
GRAVITY = 9.8
PLANET = "Earth"
# 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.py
threading.py
), to retain backwards compatibility.
Example:
# Valid function names
def my_function():
pass
def myFunction():
pass
# Valid function names
def my_function():
pass
def myFunction():
pass
Class Naming Conventions
Class names should normally use the CapWords convention.
Example:
# Valid class names
class MyClass:
pass
# 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: firstName
firstName
, lastName
lastName
, getFirstName()
getFirstName()
, setFirstName()
setFirstName()
, etc.
# Camel Case
firstName = "John"
lastName = "Doe"
def getFirstName():
pass
def setFirstName():
pass
# Camel Case
firstName = "John"
lastName = "Doe"
def getFirstName():
pass
def setFirstName():
pass
Pascal Case
Starts each word with a capital letter. For example: FirstName
FirstName
, LastName
LastName
, GetFirstName()
GetFirstName()
, SetFirstName()
SetFirstName()
, etc.
# Pascal Case
FirstName = "John"
LastName = "Doe"
def GetFirstName():
pass
def SetFirstName():
pass
# Pascal Case
FirstName = "John"
LastName = "Doe"
def GetFirstName():
pass
def SetFirstName():
pass
Snake Case
Uses underscores (_
_
) between words. For example: first_name
first_name
, last_name
last_name
, get_first_name()
get_first_name()
, set_first_name()
set_first_name()
, etc.
# Snake Case
first_name = "John"
last_name = "Doe"
def get_first_name():
pass
def set_first_name():
pass
# 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