This is the 9th day of my participation in Gwen Challenge
The introduction
Confusing or incorrect naming not only makes the code hard to understand, but worse, it can mislead our thinking and lead to a completely wrong understanding of the code. On the contrary, good naming, can make our code is very easy to understand, but also to the reader the correct expression of things and the nature of logic, so that the maintainability of the code is greatly enhanced, read the named article is very smooth, there will be a feeling of enjoyment.
Identifiers and keywords
identifier
Identifiers are variable and function names defined by programmers
The name needs to have the effect of knowing the meaning of the name, as shown below:
- Identifiers can consist of letters, underscores, and numbers
- You can’t start with a number
- Identifiers are case-sensitive
In Python 3, Chinese variable names are allowed, and non-ASCII identifiers are allowed.
In [1] : name ='hui'
In [2] : gender ='male'
In [3]: name + gender Out[3] :'hui men'
Copy the code
However, it is generally not recommended to use Chinese names.
The keyword
- The keywordIs in the
Python
An identifier that is already used internally - Keywords have special functions and meanings
You can run the following command to view the keywords and numbers in Python
In [110] :import keyword
In [111] :len(keyword.kwlist)
Out[111] :35
In [112]: keyword.kwlist
Out[112] : ['False'.'None'.'True'.'and'.'as'.'assert'. It is too long to omit...'while'.'with'.'yield']
In [112] :Copy the code
import
xxxYou can import oneToolkits/librariesIn thePython
Different toolkits/libraries provide different functionalitylen()
Functions are usually usedReturns string, list, dictionary, tuple, and so on.
Define variables, functions, classes, and never have the same name as a keyword
In [1]: book = 'Getting Started with Python'
In [3]: book
Out[3] :'Getting Started with Python'
In [4] :type(book)
Out[4] :str
In [5] :type = 'Type of Education'
In [6] :type
Out[6] :'Type of Education'
In [7] :type(book) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-7-5cf23ed0398b> in <module>
----> 1 type(book)
TypeError: 'str' object is not callable
In [8] :Copy the code
Type is used to view the data type of a variable, but it is invalid to define a variable with the same name as the keyword type. Do not define a variable with the same name as the keyword.
Naming conventions for variables
The naming convention can be considered a convention and is not absolute or mandatory for the purpose of increasing code identification and readability
Underline nomenclature
-
When defining variables, leave a space to the left and right of = for formatting purposes
-
In Python, when a variable name needs to consist of two or more words, it can be named as follows
-
Use lowercase letters for every word
-
Words are connected by _ underscores
-
For example, first_name, last_name, QQ_number, qq_password
-
Note that identifiers in Python are case sensitive
Hump nomenclature
- When a variable name is made up of two or more words, it can also be named using hump nomenclature
- Small hump nomenclature
- The first word begins with a lowercase letter, followed by a capital letter
- Such as:
firstName
,lastName
- Big camel case nomenclature
- Every word begins with a capital letter
- Such as:
FirstName
,LastName
,CamelCase
Other languages, such as Java and C, generally use the camel name. In Python, it is recommended to use the underscore name, which complies with PEP8 specifications.
The tail language
✍ Code writes the world and makes life more interesting. ❤ ️
✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️
✍ code word is not easy, but also hope you heroes support. ❤ ️