First knowledge of Python (5)-Constants, variables

Source: Internet
Author: User

Concept:

Variable: That is, the value is the amount of allowable change during the program's operation.
Constant: That is, when the program is running, its value is not allowed to change the amount.

Variables must indicate that the purpose of the description is to let the program know the variable class and assign a corresponding number of storage units to the variable.

Variables must be described first and then used. The variable name cannot be a system reserved word.

Variable naming rules:

1. The length of the variable name is unrestricted, but the character must be a letter, a number, or an underscore (_), not a space, hyphen, punctuation, quotation mark, or other character.
2. The first character of a variable name cannot be a number, but must be a letter or an underscore.
3. Python is case-sensitive, such as name and name, which are two different variables.
4. You cannot use the Python keyword (also known as a reserved word) as the variable name, according to Python3 's official document, the keywords are:

False      Class      finally    is         return
None continue for Lambda try
True def from nonlocal while
and del global not with
As elif if or yield
Assert Else import Pass
Break except in raise

If there is missing, welcome the Great god comments, I will be the first time to thank and Add.

For an assignment statement such as X=EXPR, you can read this: Let x point to the value of the expression expr. Python automatically deletes a value that does not have any variables pointing to it. In general, Python keeps track of all values and automatically removes values that no longer have variables pointing to, which is called garbage collection, so Python programmers rarely need to worry about deleting values.
An assignment statement does not copy the value pointed to, but simply marks and re-marks the existing value. Therefore, the efficiency of the assignment statement is very high, no matter how large or complex the object is pointing to.
In Python, an important feature of numbers and strings is immutable, that is, they cannot be modified in any way. In the case of a change in numbers or strings, Python is actually creating a copy of the modified version.

>>> s=‘apple‘>>> s+‘s‘‘apples‘>>> s‘apple‘>>> 5=1SyntaxError: can‘t assign to literal

In Python, there is a handy technique that allows you to assign values to multiple variables at the same time: (Multiple assignments)

>>> x,y,z=1,‘two‘,3.0>>> x1>>> y‘two‘>>> z3.0>>> x,y,z(1, ‘two‘, 3.0)

As the last statement demonstrates, you can also display multiple values on a single line by grouping them as tuples. Tuples always end with a left parenthesis (beginning with a right parenthesis).
A useful use of multiple assignments is to exchange the values of two variables:

a,b=5,9>>> a,b(5, 9)>>> a,b=b,a>>> a,b(9, 5)

The meaning of statement a,b=b,a is to assign a value to variables A and b at the same time.

Part of this article source: http://blog.csdn.net/ckhmxhfhzt/article/details/78053793

First knowledge of Python (5)-Constants, variables

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.