Python notes: #008#Name of variables

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:

Looking for Lanxiang in Shandong, China

  • 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 Pythoninternally
  • 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 Pythonview 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"

  • PythonDifferent 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

Identifiers are case sensitive

  1. When defining variables, in order to ensure the code format, =one space should be reserved on the left and right of the
  2. PythonIn , if the variable name needs to consist of two or more words , it can be named as follows
    1. Use lowercase letters for each word
    2. 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

[ CamelCase ]]( http://img.blog.csdn.net/20180209191759540?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ29uZ2x1Y2s5Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606288&siteId=291194637