Python core Programming-Chapter III-Personal notes

Source: Internet
Author: User

1. Statements and syntax

(1) The backslash "\" indicates that the statement continues. Python's good programming habit is to use a backslash wrap to continue the statement if it is not more than 80 characters in a row and a line of characters is too large.

PS: Use parentheses, brackets, curly braces to write across lines without backslashes; A string of three quotes can also be written across lines

(2) semicolon ";" You can write multiple statements on the same line. Although Python supports this, it is generally not recommended for code readability

2. Assigning values to variables

(1) In Python, objects are passed by reference, rather than assigning values directly to objects such as:

>>> a = 123>>> B = a>>> a123>>> b123>>> a = 456&G T;>> a456>>> b

Statement A = 123, which is done by creating integer 123 and variable A, and pointing a to the integral type;

Statement B = A, which is done by creating a variable B and pointing B to the integral type of a point 123

When you re-assign a, the point of a changes, and the point of B does not change, so after executing a = 456, the value of a is the value of 456,b is still 123

(2) Multiple assignment shape such as x = y = z = 1, an object is assigned to multiple variables

(3) Multivariate assignment This assignment means that both sides of the equals sign are tuples

' AB '>>> x1>>> y2>>> z'ab'

1, 2, ' AB ' three objects were assigned to X, Y, Z

In order to improve the readability of the code, we finally add parentheses to the tuples on both sides.

' AB ' )>>> x1>>> y2>>> z'ab'

Multivariate assignment can be conveniently used for variable exchange

>>> x = 123>>> y = 456>>> x123>>> y456>>> (x, y) =
   
     (y, x)>>>
     x456>>>
     y123
   

3. Identifiers

① identifiers can contain only letters, numbers, underscores, and the first character cannot be a number

② must avoid Python keywords and built-in characters when defining characters. Python keywords such as:

③python the use of underscores as variable prefixes and suffixes to specify special variables, which have special meaning for the interpreter, it is recommended to avoid using underscores as the beginning of the variable name character

4.python Basic Style

(1) Overview

① annotations are necessary, but care must be taken not to over-annotate, as

x = 5    # assigns value to x 5

The comment here is unnecessary, because it's nonsense.

② A good indentation style is four spaces, avoid using tab

③ uses a short and meaningful identifier name

(2) module structure and layout

① A typical Python module structure,

② if necessary, try to replace the global variable with local variables

③ In addition to the code that really needs to be executed, the code that needs to be executed when the module is imported, almost so that the function code should be encapsulated in a function or class waiting for the main program to call

④python detection module is imported or directly run, with __name__ system variables:

If the module is imported, the value of __name__ is the module name

If the module is executed directly, the value of __name__ is "__main__"

5. Memory Management

(1) Variable definition

①python, a variable does not need to declare a name and type before it is used, and is automatically declared when the variable is first assigned

The ② variable still needs to be created and assigned before it can be used

The ③ variable is assigned a value. can be accessed directly by variable name

(2) Reference count

Python uses reference counting to keep track of objects in memory. An internal tracking variable, called a reference counter, each object has a number of references, referred to as the reference count. The reference count for the object is garbage collected 0 o'clock.

① increasing the reference count

When an object is created, a reference count is created, and when the object is assigned to a variable, the reference count is counted as 1.

When the same object is assigned to another variable, or is passed as a parameter to a function, or as an element of a container object, the object's reference count increases by 1.

x = 123#  object is created and assigned value, reference count is 1y = x# object is referenced to other variable, reference count plus 1float (x)#  passed as parameter to function, reference count plus 1list1 = ['ab','C' , X] # becomes an element of the container, the reference count plus 1

② Reducing reference count

The reference count reduction includes the following scenarios:

A local reference leaves the scope, which is most often seen at the end of the function so that the local variables are destroyed, and the object's reference count decreases;

object's alias was destroyed by Del

del y # or del x

An alias of an object is assigned to another variable

x = 456

object is removed from a container object

List1.remove (x)

The container object itself is destroyed

Del List1

6. First Python program

(1) Try-except-else structure

Structure:

Try :     Pass # a try child block is a block    of code that wants to detect errors except ioerror,e:     Pass    # except child blocks to handle errors and execute this code block when an error occurs Else :     Pass # ELSE code block executes when the try child block is detected correctly

Python core Programming-Chapter III-Personal notes

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.