variable naming
Target
- Identifiers and Keywords
- variable naming conventions
0.1 Identifiers and Keywords
1.1 Identifiers
The identifier is the variable name , function name defined by the programmer
The name needs to have the effect of knowing the name, as shown in the following figure:
- Identifiers can consist of letters , underscores and numbers
- cannot start with a number
- Cannot have the same name as a keyword
Think: Which of the following identifiers are correct and which are incorrect and why?
fromNo12
from#12
my_Boolean
my-Boolean
Obj2
2ndObj
myInt
My_tExt
_test
test!32
haha(da)tt
jack_rose
jack&rose
GUI
G.U.I
1.2 Keywords
- A keyword is an identifier that has been used
Python
internally - Keywords have special functions and meanings
- The developer is not allowed to define an identifier with the same name as a keyword
Use the following command to Python
view the keywords in
In [1]: import keyword
In [2]: print(keyword.kwlist)
Tip: The learning and use of keywords will be introduced in the following courses
import
Keywords can import a "toolkit"
Python
Different toolkits in , with different tools
02. Naming rules for variables
Naming rules can be regarded as a convention , not absolute or mandatory
The purpose is to increase the identification and readability of the code
Note Python
that identifiers in are case-sensitive
- When defining variables, in order to ensure the code format,
=
one space should be reserved on the left and right of the Python
In , if the variable name needs to consist of two or more words , it can be named as follows- Use lowercase letters for each word
- Use underscores to connect
_
words
- For example:
first_name
,last_name
,qq_number
,qq_password
CamelCase
- When the variable name is composed of two or more words, it can also be named using camel case
- little camel case
- The first word starts with a lowercase letter, and the first letter of subsequent words is uppercase
- For example:
firstName
,lastName
- big camel case
- Capital letters are used for the first letter of each word
- For example:
FirstName
,LastName
,CamelCase