With old Ziko python assignment, simple is not easy

Source: Internet
Author: User
Variable naming

In the article "The first knowledge always powerful function", there is a section devoted to "knowledge of the name of the word", that is, the problem of variable names, Ben Wen know the new principle, here to review:

Name format: (underscore or letter) + (any number of letters, numbers or underscores)

Attention:
1. Case-sensitive
2. Prohibit the use of reserved words
3. Abide by the usual habit
• variable names (_x) that begin with a single underscore are not imported by the From module import * statement.
• An underlined variable name (X) is a system-defined variable name that has a special meaning for the interpreter.
• A variable name (__x) that starts with two underscores but does not end with two underscores is a class-local (compressed) variable.
• When running through interactive mode, only a single underscore variable (_) saves the final expression result.

You need to explain the reserved word, which is that Python retains some words that cannot be used by the user as a variable name. What do they have? (Python2 and Python3 rarely differ, but in general)

The code is as follows:


and Assert break class continue def del elif else except exec finally for from global if import in was lambda not or pass p Rint raise return try while yield

Do you have to remember? Of course it's not necessary. On the one hand, can be found on the Internet, in addition, it can be:

The code is as follows:

>>> not = 3
File " ", line 1
not = 3
^
Syntaxerror:invalid syntax

>>> pass = "Hello,world"
File " ", line 1
pass = "Hello,world"
^
Syntaxerror:invalid syntax

In the interactive mode of the lab, using reserved words to do variables, the error. Of course, it's time to change the name.

The above principles are the basic principles. In practical programming, this is usually done in order to make the program more readable:
• The name has a certain meaning. For example, write: n = "Qiwsir", rather than write: name = "Qiwsir" better.
• Names do not mislead others. For example, using Account_list to refer to a group of accounts, it will be misunderstood as a list type of data, in fact, may or may not be. So the best time to change the name, such as direct use of accounts.
• Name to make sense of the distinction, sometimes you may use the name of the A1,A2, it is best not to do so, in a different way, by literally can see a certain distinction to better.
• The best name can be read out, do not make English words, do not mess with what is written, especially in your country, but also like to use Hanyu Pinyin abbreviation to do as the name, more trouble, it is better to spell it all. It is best to use complete words or recognized abbreviations that do not cause ambiguity.
• Less use of a single letter and number, not only is it that you are too lazy, but also because there may be a lot of individual letters and numbers in a piece of code, for the search to cause trouble, others also do not know that your I and his understanding of I is not a meaning.

In short, take the name, pay attention to a lot. In any case, remember a standard: clear

Assignment statements

For assignment statements, crossing is no stranger. Any variable, in Python, is assigned first if you want to use it.

Statement format: Variable name = Object

The essence of the assignment is also analyzed in the previous section.

There is also an assignment, called an implicit assignment, through the import, from, Del, class, for, and function parameters. such as module import, function and class definition, for loop variables and function parameters are implicit assignment operations. There will be a slow path behind this.

The code is as follows:


>>> name = "Qiwsir"

>>> name, website = "Qiwsir", "Qiwsir.github.io" #多个变量, in order to assign values sequentially
>>> Name
' Qiwsir '
>>> website
' Qiwsir.github.io '

>>> name, website = "Qiwsir" #有几个变量, corresponding to a few objects, not less, nor more
Traceback (most recent):
File " ", line 1, in
Valueerror:too Many values to unpack

If you assign this value, you also have the same number of sides:

The code is as follows:


>>> one,two,three,four = "good"
>>> One
' G '
>>>
' O '
>>> Three
' O '
>>> Four
' d '

This is equivalent to splitting the good into a single letter, and then assigning a value to the left variable.

The code is as follows:


>>> [Name,site] = ["Qiwsir", "Qiwsir.github.io"]
>>> Name
' Qiwsir '
>>> site
' Qiwsir.github.io '
>>> Name,site = ("Qiwsir", "Qiwsir.github.io")
>>> Name
' Qiwsir '
>>> site
' Qiwsir.github.io '

That's OK.

In fact, there are many styles of assignment, and the core is to match the variable with an object. Object that can be used in the way above, maybe this is the

The code is as follows:


>>> site = "Qiwsir.github.io"
>>> name, main = Site.split (".") [0], Site.split (".") [1] #还记得str. Split ( ) this stuff? Forget it, Google a bit.
>>> Name
' Qiwsir '
>>> Main
' GitHub '

Enhanced Assignment

This thing listens to the name is stronger than assigns the value.

In Python, the following methods are called Enhanced assignments:

Enhanced Assignment Statements equivalent to statement
X+=y x = X+y
X-=y x = XY
X*=y x = X*y
X/=y × = x/y

Other similar structures: x&=y x|=y x^=y x%=y x>>=y x<<=y x**=y x//=y

Look at the following example, there is a list that wants to get a second listing, where each number is 2 larger than the original list. Can be implemented in the following ways:

The code is as follows:


>>> number
[1, 2, 3, 4, 5]
>>> number2 = []
>>> for I in number:
... i = i+2
..... number2.append (i)
...
>>> number2
[3, 4, 5, 6, 7]

If you use the above enhancement assignment, I = i+2 can be written as I +=2, try it:

The code is as follows:


>>> number
[1, 2, 3, 4, 5]
>>> number2 = []
>>> for I in number:
... I +=2
..... number2.append (i)
...
>>> number2
[3, 4, 5, 6, 7]

This is the enhanced assignment. Why use enhanced assignment? Because I +=2, it is faster to calculate than I = i+2, and the latter is to copy an I on the right.

The above example can also be modified, do not forget the list of the powerful function of parsing it.

The code is as follows:


>>> [i+2 for I in number]
[3, 4, 5, 6, 7]

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.