Names ¶
Here are some guidelines for naming variables:
-
Snake case is the most widely used convention for naming variables in Python.
It involves using lowercase letters and underscores to separate words.
For example:
my_variable
,user_name
,total_count
. -
Choose variable names that are descriptive and convey the purpose or content of the variable.
This makes the code more self-explanatory.
For instance, use
customer_name
instead ofcn
orx
. -
Try to avoid single-letter variable names.
Exceptions include common conventions like
i
,j
, andk
for some numbers. -
If you have a variable that is meant to be a constant (i.e., its value should never change), use all uppercase letters with underscores separating words.
For example:
MAX_SIZE
,PI
.
PEP 8 is the recommended style guide for Python code. There are some variations, but having everyone stick to a consistent style helps new developers get up to speed quicker. There are some formatters, like black , that are widely used because the automatically format your code consistently.
Case matters ¶
Python is a case-sensitive programming language, which means that it distinguishes between uppercase and lowercase letters. This applies not only to variable names but also to function names, class names, and other identifiers in your code.
In [8]:
Copied!
my_variable = 10
My_Variable = 20
print(my_variable)
print(My_Variable)
my_variable = 10
My_Variable = 20
print(my_variable)
print(My_Variable)
10 20