Python identifier concept and specification

There are many things that can be named in python, such as the variables we learned before
and the function classes we will touch later, etc., etc.

And the names we give these are collectively called identifiers

And the naming of identifiers in python is also limited

There are three main types
: 1. Content limitation;
2. Uppercase and lowercase impression
; 3. Keywords cannot be used

As far as the content is limited,
it is only allowed to use (English and Chinese numbers are underlined). Secondly, it is better not to use Chinese. In the industry, it is very low-level to define variables in Chinese. Many languages ​​​​support Chinese naming, but after all, the main system of programming is still in English. He supports Chinese. It is still limited to use Chinese, in addition to not being good-looking, it will also bring unnecessary troubles to myself at some times.
Numbers cannot be used at the beginning. The main keyword Chinese is (not recommended) Numbers are (not supported)

Case inscription features We can write a piece of code like this

Amint = "张三";
amint = "你好";
print(Amint)
print(amint)

The results of the operation are as follows.
insert image description here
The two variables are only different in the case of the beginning a, but they are still distinguished by python, which shows that python is very impressed in this respect.

Then you can’t use keywords
because any language has some built-in attributes and functions. The so-called built-in means that the official website has defined it for you, and you can call it directly. But just like the user name, this name has been occupied by other function variables. You can’t use it any more
. At this time, some people may want to memorize keywords. I can only say that there are so many that I can’t remember them all, and I have been writing code for a long time. I think the probability of touching keywords is relatively low. I will encounter the system. I will give you an error message and just change your name when the time comes.

And the keywords are also capitalized, which is very interesting. You can avoid keywords through different capitalization. It is also a fun thing in python.

Here we can challenge the system keywords.
insert image description here
Here your system will be marked red, and then we will run a wave forcibly.

insert image description hereThe system throws an exception

Then let’s catch a bug
and change def to Def.
The system is normal.
insert image description here
In fact, this is not a bug. Because the second point of python has already said that the upper and lower case inscriptions are distinguished by upper and lower case. The identifier itself is a behavior that is allowed in plain text in python. but personally not recommended

Because at present we have only touched the variable, we will first look at the naming convention of the variable

The first one
is to see the name and know the meaning.
This has certain requirements for English, which means that you use this simple name, and others will know what your identifier is for at a glance. For example, username, others can see it as a username.

The second
underscore nomenclature
uses underscores to separate when there are multiple names in the name. For example, we can change it to user_name
above.

The third
English letter is all lowercase.
As the name suggests, use English as much as possible in lowercase. Don’t use the second feature of python to be opportunistic.

Of course, the specification is a suggestion to make your code more formal and beautiful, but if you don’t follow it, it won’t run directly

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/130279138