One hour Python per day (10.11)

Source: Internet
Author: User
Tags first string

3.1 The assignment of a Python variable

The equal sign (=) is the primary assignment symbol: Anint = astring = ' Hanxingzhi '

However, it is important to note that the assignment is not assigning a value to a variable.

3.1.1 Chain assignment and value-added assignment

x = y = 1 (multiple Assignment)

y = x = x + 1 (chained Assignment)

x = x + 1 ======> x + = 1 This is the same as the value-added assignment effect used by C #, but it is not possible to use the + + and--pre-and post-increment self-decrement operations in Python.

multivariate assignment : I have to say that Python has multiple assignments that I don't know about.

' Hanxingzhi '>>> x1>>> y3>>> z'Hanxingzhi '

We'd better use tuples in real programming to use multivariate assignments

>>> (x, y, Z) = (hanxingzhi')>>> 1>>>  y2>>> z'hanxingzhi'

Application of Multivariate assignment:

In Python we can use multivariate assignments to exchange values of two variables without using intermediate variables.

>>> (x, y) = 1,2>>> x, y = y,x>>> (x, y) (2, 1)

Those identifiers for 3.2 python

The most I feel about Python identifiers is the use of underscores:

1. _xxx do not import ' from mdule import * '

2. _xxx_ system-defined name

3. Private variables in the _xxx class

Note: Because underscores have special meanings for compilers and are symbols used by built-in identifiers, it is possible to use fewer underscores as identifiers when programming. Generally speaking, _xxx is defined as private.

3.2.1python BASIC Programming Style

Note: Use # to Annotate

Documentation: Python provides a mechanism for dynamically retrieving document strings by _DOC_ special variables. In a module, a class declaration, or a function declaration, the first string that is not assigned can be accessed using OBJ._DOC_, where obj is the name of a module, class function.

Indent: 4 spaces should be indented.

3.2.2 Module structure and layout

# (1) Start line (Unix) = = Only the starting line is used in a UNIX environment, and the starting line can only enter the script name to execute the script. Do not invoke the interpreter directly.

# (2) Module document = = A simple introduction to the function of the module, and the meaning of the important local variables, outside the module can be accessed through MODULE._DOC_ access to the content.

# (3) Module import = = Imports All modules required for the current code, each module is imported only once, and the module import code inside the function will not execute unless the function is running.

# (4) Variable definition = = defines a global variable, all functions of this module can be used. But from a good programming style, unless you try to use local variables instead of global variables.

# (5) class definition statement = = All classes are defined here, and the class statement is executed when the module is imported. The document variable for the class is CLASS._DOC_

# (6) function definition statement = = function defined here grams can be accessed externally through module.function (). The document variable for the function is FUNCTION._DOC_

# (7) Main function = = Of course the main function can write the test code.

One hour Python per day (10.11)

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.