With the old Ziko python assignment, simple and not simple _python

Source: Internet
Author: User
Tags reserved stdin in python

Variable naming

In the "first-ever-powerful function", there is a section devoted to the "name of the Knowledge", which is about the problem of variable names, this warm know the new principles, here to review:

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

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

To explain the reserved word, there are some words in python that don't allow the user to use the variable name. What do they have? (Python2 and Python3 are rarely different, but they are the same overall)

Copy Code code as follows:

and Assert break class continue def del elif else except exec finally to from global if import in are 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, can also be this:

Copy Code code as follows:
>>> not = 3
File "<stdin>", line 1
not = 3
^
Syntaxerror:invalid syntax

>>> pass = "Hello,world"
File "<stdin>", 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, you usually do this to make your program more readable:
• The name has a certain meaning. For example, write: n = "Qiwsir", you might as well write: name = "Qiwsir" better.
• Name not to 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 this is a good time to change the name, such as directly with accounts.
• Name to make a meaningful distinction, sometimes you may use the name of A1,A2, it is best not to do so, in a separate way, by literal to see a certain distinction to better.
• The best name can be read out, do not make their own English words, also do not use what is written, especially in your country, but also like to use Hanyu Pinyin abbreviation as the name, more trouble, than the whole fight it. It is best to use a complete word or a recognized abbreviation that does not cause ambiguity.
• A single letter and number is less used, not only that you are too lazy, but also because in a piece of code may have a lot of individual letters and numbers, the search for trouble, others also do not know that your I and his understanding of I is not a meaning.

In short, take a name, pay attention to a lot. Anyway, remember a standard: clear

Assignment statement

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

Statement format: Variable name = Object

The nature 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 arguments. such as module import, function and class definitions, for loop variables and function parameters are implicit assignment operations. There's a way to get behind this stuff.

Copy Code code as follows:

>>> name = "Qiwsir"

>>> name, website = "Qiwsir", "Qiwsir.github.io" #多个变量, assigned sequentially
>>> Name
' Qiwsir '
>>> website
' Qiwsir.github.io '

>>> name, website = "Qiwsir" #有几个变量, on the corresponding several objects, not less, can not be more
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:too Many values to unpack

If you do this, you get the same number on both sides:

Copy Code code as follows:

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

This is equivalent to splitting the good into one letter and assigning it to the variable on the left.

Copy Code code 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, a lot of the style of assignment, the core is the variable and an object corresponding. Object that can be used above the way that maybe that's the

Copy Code code as follows:

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

Enhanced Assignment

This thing listens to names that are better than assigned.

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 = x/y

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

Looking at the example below, there is a list that wants another listing, each of which is 2 larger than the original list. Can be implemented in the following ways:

Copy Code code 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 write I +=2, try it:

Copy Code code 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 compute than I = i+2, the latter to the right also copy an I.

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

Copy Code code as follows:

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

Related Article

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.