Dave Python Exercise 2

Source: Internet
Author: User
Tags python script

# Coding = GBK </P> <p >############## Part 1: syntax and statement #################< br/> # Python statements have some basic rules and special characters: <br/> # the pound sign (#) indicates that the subsequent characters are Python comments <br/> # line feed (\ n) is the standard line separator (usually one statement line) <br/> # backslash (\) to continue the previous line <br/> # semicolon (;) to connect the two statements to a row <br/> # colon (:) separate the header and body of a code block <br/> # Statement (code block) using indentation blocks <br/> # separate different code blocks with different indentation depths <br/> # organize Python files in the form of modules </P> <p> # Python statement, it is generally separated by line breaks, that is, a line of a statement. A long line of statements can be broken down into several rows by backslash (\) <br/> # If (10> 1) and \ <br/> # (10> 5 ): <br/> # print ('this is true! ') <Br/> # --> This is true! </P> <p> # Note: In two cases, a statement can be used to span rows without a backslash. When the close operator is used, a single statement can span multiple rows. <br/> # For example, multiple rows can be written when parentheses, braces, and curly braces are included. In addition, the strings enclosed by three quotation marks can also be written across rows </P> <p> # print (''' the import thing in life is to have a great <br/> # aim and the determination to attaion it! ''') <Br/> # --> the import thing in life is to have a great <br/> # aim and the determination to attaion it! </P> <p> # mystr = ''' the import thing in life is to have a great <br/> # aim and the determination to attaion it! ''' <Br/> # print (mystr) <br/> # --> the import thing in life is to have a great <br/> # aim and the determination to attaion it! </P> <p> # multiple statements form a code group (:): <br/> # A group of statements with the same indentation form a code block, which we call a code group. <Br/> # composite statement like if, while, def, and Class. The first line starts with a keyword and ends with a colon, one or more lines of code after this row constitute a code group. <Br/> # the first line and the code group following it are called a clause ). </P> <p> # code groups are separated by different indentation <br/> # python uses indentation to separate code groups. Code hierarchies are embodied by spaces or tab indentation in the same depth. <Br/> # The code lines in the same code group must be strictly left aligned (there are as many spaces or as many tabs on the left). <br/> # If you do not strictly abide by this rule, code in the same group may be treated as another group, or even cause syntax errors. </P> <p> # core style: Indent four spaces to avoid tabs <br/> # Avoid tabs because tabs in different text editors indicate different width of white spaces, if your code is to be used across platforms or be read and written by different editors, it is recommended that you do not use tabs </P> <p> # write multiple statements in the same row of books (;) <br/> # semicolon (;) allows you to write multiple statements on the same line, and separate them with semicolons. These statements cannot start a new code block in this line. <Br/> # Writing multiple statements on the same line greatly reduces the readability of the Code. This is not recommended for python. </P> <p> # module <br/> # Every Python script file can be regarded as a module. The module exists as a disk file. When a module <br/> # becomes too large and has too many functions, you should consider splitting some code to create another module. The <br/> # code in the module can be a script executed directly or a bunch of code similar to the library function, this can be called by other modules <br/> # import. The module can contain directly Running code blocks, class definitions, function definitions, or combinations of these. </P> <p >################ Part 2: variable assignment #################</P> <p> # The assignment does not directly assign a value to a variable, in python, objects are passed through references. When assigning a value, whether the object is newly created or an existing one, the reference (not a value) of the object is assigned to the variable. </P> <p> # Starting from Python 2.0, an equal sign can be combined with an arithmetic operator to assign the calculation result to the variable on the left. This is called an incremental value assignment. <Br/> # x = x + 1 <br/> # --> X + = 1 </P> <p> # + =-= * =/= % = * * = <<=>>=<=^=|=< br/> # Python does not support pre-and post-auto-increment/auto-subtraction operations similar to X ++ or -- X.. </P> <p> # multiple values <br/> # x = y = z = 1 <br/> # print (x, y, z) <br/> # --> 1 1 1 <br/> # an integer object with a value of 1 is created, the same reference of this object is assigned to x, y, and z </P> <p> # "multivariate" values <br/> # x, y, z = 1, 2, 'Dave come from Anqing! '<Br/> # print (x, y, z) <br/> # --> 1 2 Dave come from Anqing! </P> <p> # swap the values of two variables <br/> # X, Y = <br/> # print ('first: ', x, y) <br/> # X, Y = Y, x <br/> # print ('second: ', x, y) <br/> # --> <br/> # First: 1 2 <br/> # second: 2 1 </P> <p >############### Part 3: ##################< br/> # an identifier is a set of valid strings allowed as names in computer languages. Some of them are keywords that constitute the identifier of the language <br/>. Such an identifier cannot be used as its identifier, otherwise it will cause a syntax error (syntaxerror exception ). </P> <p> # Python also has a set of built-in identifiers. Although they are not reserved words, these special names are not recommended. </P> <p> # valid Python identifiers <br/> # The string rules of Python identifiers are similar to those of other advanced languages written in C: <br/> # The first character must be a letter or underscore (_) <br/> # The remaining characters can be letters, numbers, or underscores <br/> # case sensitive <br/> # The identifier cannot start with a number, other symbols are not allowed. </P> <p> # built-in <br/> # Python also has a set of "Built-in" names that can be used in any level-1 Code. These names can be set or used by the interpreter. Although built-in is not a keyword, it should be treated as a "system reserved word". <br/> # It is not used. However, in some cases, it is required to overwrite (I .e. re-define and replace) them. Python does not support reload identifiers, so there is only one name bound at any time. <Br/> # built-in is a member of the _ builtins _ module. It is provided at the beginning of the program or in the interactive interpreter> before the prompt, automatically imported by the interpreter. <Br/> # consider them as global variables applicable to any level-1 Python code. </P> <p> # Special underline identifier <br/> # Use underline as the prefix and suffix of a variable to specify special variables. Summary of special usage of underline in Python: <br/> # _ XXX do not use 'from module import * 'to import <br/> # _ XXX _ system definition name <br/> # _ private variable in the xxx class name </P> <p> # core style: avoid using underscores as the start of variable names <br/> # Because underlines have special meanings for the interpreter and are symbols used by built-in identifiers, we recommend that programmers avoid <br/> # using underlines as the start of variable names. Generally, variable name_xxx is considered as "private" and can be used outside the module or class <br/>. When the variable is private, it is a good habit to use _ XXX to represent the variable. Because the variable name _ XXX _ has a special meaning for <br/> # python, this naming style should be avoided for common variables. </P> <p> # document <br/> # Python also provides a mechanism for dynamically obtaining document strings through the _ Doc _ special variable. In the module, class <br/> # declaration, or function declaration, the first string without a value can use the attribute obj. _ Doc _, where OBJ <br/> # is the name of a module, class, or function. This can also be run at runtime. </P> <p> # module structure and layout <br/> # using modules to Reasonably organize your Python code is a simple and natural method. You should establish a unified and easy-to-read <br/> # structure and apply it to each file. The following is a very reasonable layout: <br/> # (1) start line (UNIX) <br/> # (2) module documentation <br/> # (3) module import <br/> # (4) variable definition <br/> # (5) class definition <br/> # (6) Function Definition <br/> # (7) main Program </P> <p> # (1) start line <br/> # the start line is usually used only in Unix-like environments, with the start line, you can enter only the script name to execute the script, without directly calling the interpreter. <Br/> # (2) module documentation <br/> # briefly introduces the functions of the module and meanings of important global variables. _ Doc _ access these contents. <Br/> # (3) module import <br/> # import all the modules required by the code of the current module. Each module is imported only once (when the current module is loaded ); function <br/> # The internal module import code is not executed unless the function is being executed. <Br/> # (4) variable definition <br/> # The variables defined here are global variables. All functions in this module can be directly used. From a good programming style perspective, <br/> # Unless necessary, use local variables instead of global variables as much as possible, your code is not only easy to maintain <br/> #, but also improves performance and saves memory. <Br/> # (5) Class Definition Statement <br/> # All classes must be defined here. When a module is imported, the class statement is executed and the class is defined. The document variable of the class is class. _ Doc __. <Br/> # (6) Function Definition Statement <br/> # The function defined here can be defined through module. function () is accessed externally. When a module is imported, the def statement <br/> # is executed, and the function is defined. The variable in the function documentation is function. _ Doc __. <Br/> # (7) Main Program <br/> # This code is executed no matter whether the module is imported by another module or directly executed as a script. Generally, there is not much functional code here, but different functions are called according to the execution mode. </P> <p> # recommended code style: the main program calls the main () function <br/> # If _ name __= = Main (): <br/> # Dave () <br/> # Check the value of the _ name _ variable and then execute the corresponding call. The code in the main program usually includes variable assignment, class definition, and function definition. <br/> # Then, check _ name _ to determine whether to call another function (usually main () function) to complete the functions of this module. The main program usually does these things. <Br/> # No matter what name you use, emphasize that this is a good place to place the test code. </P> <p> # many projects are a main program that imports all required modules. Therefore, remember that most of the module creation <br/> # aims to be called by others rather than being executed independently. We may also create a python library-style <br/> # module. This module is created to be called by other modules. In short, there is only one module, that is, the module that contains the main process <br/> # is directly executed, or executed by the user through the command line, or as a batch, it can be scheduled by a Unix cron task <br/> # Or called by a Web server or GUI. </P> <p> # Note: all modules have the ability to execute code. The highest level of Python statements-that is, code lines without indentation are executed when the module is imported, whether or not they need to be executed. <Br/> # because of this "feature", it is safer to write code in addition to the code that really needs to be executed, and almost all functional code is included in the function. Again, only the main program module contains a large number of top-level executable code. <br/> # only a few top-level executable code should be available for all other imported modules, all functional code should be encapsulated in functions or classes. </P> <p> # core notes: __name _ indicates how the module should be loaded <br/> # because the main program code runs no matter whether the module is imported or directly executed, we must know how the module determines the running direction. <Br/> # One application may need to import a module of another application to reuse some useful code. In this case, you only want to access the code in its <br/> # application, rather than running the application. <Br/> # a problem occurs. "Is there a way for python to check whether the module is imported or directly executed at runtime ?" Answer: _ name _ system variable. <Br/> # If the module is imported, the value of _ name _ is the module name. <br/> # If the module is directly executed, the value of _ name _ is '_ main _' </P> <p >################ Part 4: memory Management #################</P> <p> # variable definition <br/> # most compiled languages, variables must be declared before use. The C language is more demanding: The variable Declaration must be at the beginning of the code block and before any other statements. <Br/> # in Python, explicit variable declaration statements are not required. variables are automatically declared when they are assigned for the first time. Like most other languages, variables can only be used after they are created and assigned values. </P> <p> # dynamic type <br/> # Not only does the variable name need to be declared beforehand, but also does not need to be declared as a Type in Python. In python, the object type and memory usage are determined at runtime. <Br/> # although the code is compiled into bytecode, python is still an interpreted language. When a value is created, the interpreter determines the type of the new object based on the syntax and the right operand. <Br/> # after an object is created, the application of this object is assigned a variable on the left. </P> <p> # Memory Allocation <br/> # As a responsible programmer, we know that when allocating memory to variables, we borrow system resources. After we use up, the borrowed system resources should be released. <Br/> # The Python interpreter undertakes the complex tasks of memory management, which greatly simplifies the compilation of applications. You only need to care about the problem you want to solve. As for the underlying issues, you can leave them to the python interpreter. </P> <p> # reference count <br/> # To keep track of objects in the memory, python uses the reference count simple technique. That is to say, in Python, <br/> # records the references of all objects in use. An internal trace variable is called a reference counter. How many references each object has, referred to as reference count. <Br/> # when an object is created, a reference count is created. When this object is no longer needed, that is, <br/> # When the reference count of this object is 0, it is reclaimed. (Strictly speaking, this is not 100% correct, but now you can think so) </P> <p> # Add reference count <br/> # when an object is created and assigned a value to a variable, the reference count of this object is set to 1. <Br/> # x = 3.14 <br/> # Y = x <br/> # statement X = 3.14 creates a floating point object and assigns its reference to X. X is the first reference. Therefore, the reference count of the <br/> # object is set to 1. Statement y = X creates an alias y pointing to the same object (see Figure 3-2 ). <Br/> # In fact, I didn't create a new object for Y. Instead, I increased the reference count of this object once (changed to 2 ). This is one of the ways in which the object <br/> # reference count is increased. Other methods can also increase the reference count of an object. For example, the object is called by a function as the parameter <br/> # or the object is added to a container object. </P> <p> # In short, the reference count of the object is <br/> # The object is created <br/> # x = 3.14 <br/> # Or another alias is created <br/> # Y = x <br/> # pass the parameter to the function (new local reference) <br/> # foobar (x) <br/> # or become an element of a container object <br/> # mylist = [123, X, 'xyz'] </P> <p> # reduce the reference count <br/> # When an object's reference is destroyed, the reference count decreases. The most obvious example is that when the reference leaves its scope, <br/> # This situation occurs most often when the function stops running, all local variables are automatically destroyed, the reference count of the object decreases with <br/>. When a variable is assigned to another object, the reference count of the original object is automatically reduced by 1: </P> <p> # Foo = 'xyz' <br/> # bar = Foo <br/> # Foo = 123 <br/> # When the string object "XYZ" when created and assigned to Foo, its reference count is 1. when an alias bar is added, the reference count is changed to 2. <br/> # However, when foo is re-assigned to the integer object 123, the reference count of the XYZ object is automatically reduced by 1 and then changed to 1. </P> <p> # other ways to reduce the reference count of an object include using the del statement to delete a variable (see the next section ), or <br/> # when an object is removed from a window object (or the reference count of the container object itself is changed to 0 ). <Br/> # To sum up, the reference count of an object is reduced in the following cases: </P> <p> # A local reference is out of its scope. For example, when the function ends, foobar () (see the example above. <Br/> # The object alias is explicitly destroyed. <Br/> # del y # Or del X <br/> # an alias of an object is assigned to another object. <br/> # x = 123 <br/> # object removed from a window object <br/> # mylist. remove (X) <br/> # The Window object itself is destroyed <br/> # del mylist # or goes out-of-scope </P> <p> # del statement <br/> # the Del statement deletes a reference of an object, its syntax is: <br/> # del obj1 [, obj2 [,... objn] <br/> # For example, if del y is executed in the preceding example, two results are generated: <br/> # Delete y from the current namespace <br/> # subtract one from the reference count of x <br/> # Next step, if del X is executed, the last reference of the object is deleted, that is, the reference count of the object is reduced to <br/> #0, this will cause the object to be "inaccessible" or "inaccessible ". From this moment on, this object becomes the recycle object of the garbage collection <br/> # mechanism. Note that any tracing or debugging program will add an additional reference to an object, which will delay the <br/> # time when the object is recycled. </P> <p> # garbage collection <br/> # memory that is no longer in use is released by a mechanism called garbage collection. As mentioned above, although the interpreter tracks the reference count of the object <br/> #, the garbage collector is responsible for releasing the memory. The garbage collector is an independent code used to search for objects with a reference count of <br/> #0. It is also responsible for checking the objects that should be destroyed even if the reference count is greater than 0. In specific cases, <br/> # causes cyclic reference. <Br/> # A circular reference occurs when at least two objects reference each other, that is, all references disappear. <br/> # Some references still exist, this indicates that reference counting alone is not enough. The Python garbage collector is actually a reference meter <br/> # calculator and a circular garbage collector. When the reference count of an object changes to 0, the interpreter pauses and releases the object <br/> # and other objects that only have this object accessible (reachable. As a supplement to the reference count, the garbage collector will also pay attention to the objects that are <br/> # allocated with a large amount (and those that are not destroyed by the reference count. In this case, the interpreter suspends <br/> # And tries to clear all unreferenced loops. </P> <p> # core tips: replace module variables with local variables <br/> # example: <br/> # import OS <br/> # ls = OS. linesep <br/> # similar to OS. linesep requires the interpreter to perform two queries: <br/> # (1) Search for the OS to make sure it is a module. <br/> # (2) find the linesep variable in this module. <Br/> # because the module is also a global variable, we consume more system resources. If you frequently use an attribute like this in a function, <br/> # we recommend that you obtain a local variable alias for this attribute. Variable Search speed will be much faster-always search for local variables before searching for global variables. <Br/> # this is also a technique that allows your program to run faster: replace frequently used module attributes with a local reference. Code runs faster without having to <br/> # variable names that are always so long. <Br/>

Bytes -------------------------------------------------------------------------------------------------------

Blog: http://blog.csdn.net/tianlesoftware

WEAVER: http://weibo.com/tianlesoftware

Email: dvd.dba@gmail.com

Dba1 group: 62697716 (full); dba2 group: 62697977 (full) dba3 group: 62697850 (full)

Super DBA group: 63306533 (full); dba4 group: 83829929 (full) dba5 group: 142216823 (full)

Dba6 group: 158654907 (full) chat group: 40132017 (full) chat group 2: 69087192 (full)

-- Add the group to describe the relationship between Oracle tablespace and data files in the remarks section. Otherwise, the application is rejected.

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.