Getting Started with Python knowledge summary

One of the design goals is to make Python code for a high degree of readability. It is to make use of English words and punctuation other languages ​​commonly used design-time, so the code looks clean and beautiful. Unlike other static languages ​​such as C, Pascal written declaration that need to be repeated, nor their syntax as often there are special circumstances and unexpected.

indentation

Python developers intend to give in violation of the rules of procedure indentation can not be compiled in order to force programmers to develop good programming habits. Python language and use indention start and exit the block of statements (Off-side rules) instead of using braces or some keywords. Increase Indent indicates the start of a block of statements, while reducing the indentation, said exit statement block. Indent became part of the syntax. For example if statement:

python3

Getting Started with Python knowledge summary

Under PEP, we must use four spaces to represent each level of indentation (4 spaces clear provisions on how, in the actual preparation can customize the number of spaces, but to satisfy an equal number of spaces between each level of indentation). Use the Tab character and the number of spaces although other can be compiled through, but do not meet coding standards. Support Tab character and other number of spaces is just as compatible with very old Python program and some editing program in question.

Control statements

if statement, run a block of statements when the condition is met. Often used in conjunction with else, elif (equivalent else if).

for statement, through the list, string, dictionaries, and other iterators collection, process each element in the iterator.

while statement, when the condition is true, loop operation block.

try statements, and except, finally deal with the use of unusual circumstances arise during program operation.

class statement that defines the type.

def statements to define the function and the type of process.

The pass statement, indicates that this is empty, do not run any operation.

assert statement, when used for program debugging phase of the test run conditions are met.

After the with statement, Python2.6 syntax definitions, run a block of statements in a scene. For example, run the statement before the block encryption and decryption in the block of statements to run exit.

yield statement in the function uses the iterator that returns one element. Since Python 2.5 version. This statement became an operator.

raise statement, a manufacturing error.

import statement, introducing a module or a package.

from import statement import modules from the package or import an object from the module.

import as statement, the imported object assigned to a variable.

in statements to determine whether an object in a string / list / tuple.

expression

Python expressions written with C / C ++ similar. And only certain wording different.

Similar main arithmetic operators and C / C ++. +, -, *, /, //, *, -,%, respectively, takes a positive or addition, subtraction or negation, multiplication, division, divisible, power, take up, take the remainder. >>, << represents the right and left. &, |, ^ Represent a binary AND, OR, XOR operation. >, <, ==,! =, <=,> = Comparison value for the two expressions, respectively greater than, less than, equal to, not equal, less than equal to, greater than or equal. In these operators inside, ~, |, ^, &, <<, >> integer must be applied.

Python use and, or, not represented logical operations.

It is, is not compare whether two variables are the same object. in, not in an object for determining whether another object.

Python support "list comprehensions" (list comprehension), such as calculating the square and 0-9:

sum(x * x for x in range(10))

285

Python uses lambda represents the anonymous function. Anonymous body of the function can only be an expression. such as:

add=lambda x, y : x + y

add(3,2)

5

Python using y if cond else x denotes the conditional expression. It means that when cond is true, the expression is y, otherwise the expression is x. ? The equivalent of C ++ and Java in the cond y: x.

Python distinguished list (list) and the tuple (tuple) of two types. list wording is [2,3], and the tuple is written (1,2,3). You can change the list of elements, but can not change tuple. In some cases, tuple parentheses may be omitted. tuple assignment for special treatment. Therefore, at the same time it can be assigned to multiple variables, such as:

x, y = 1,2 # simultaneously to x, y assignment, the final result: x = 1, y = 2

In particular, it is possible to exchange the values ​​of two variables used in this form:

x, y = y, x # final result: y = 1, x = 2

Use Python '(single quote) and double quote ( ") to represent the string with Perl, Unix Shell language or Ruby, Groovy other languages ​​are not the same, the same for both symbols effect. Generally, if the string appears in double quotes , use single quotes for strings; double quotes if otherwise not occur, depending on personal preferences appear in the string (backslash) is interpreted as a special character, such as \ n represent a newline character. before expression r plus instructions appear Python string \ do not explain. such an approach commonly used to write a regular expression or Windows file paths.

Python supports cutting list (list slices), you can get part of the complete list. Cutting operation types are supported str, bytes, list, tuple and so on. Its syntax is ... [left: right] or ... [left: right: stride]. Nums variable values ​​are assumed [1, 3, 5, 7, 8, 13, 20], then the following several statements are true:

nums [2: 5] == [5, 7, 8] for the cutting element 2 from the index to the index of the element 5, but does not include an element labeled 5.

nums [1:] == [3, 5, 7, 8, 13, 20] to the last cutting element.

nums [: - 3] == [1, 3, 5, 7] from the beginning of the element has been cut up to the last third element.

nums [:] == [1, 3, 5, 7, 8, 13, 20] returns all elements. Changes in the new list will not affect the nums.

nums [1: 5: 2] == [3, 7] from the cutting element at index 1 to index the element 5 but does not contain the index of the element 5 is, in steps of 2.

function

Python support recursive function, the default parameter values, variable parameters, but does not support function overloading. To enhance readability of the code, the function can be written in the "Document string" (Documentation Strings, or simply have docstrings), used to explain the role of the function, the type of the meanings of the parameters, return type and value range. You can use the built-in function help () to print out using the Help function. such as:

Getting Started with Python knowledge summary

Methods of objects

A method is a function of the object bound to the object. Call the object method syntax is ××× tance.method (arguments). It is equivalent to calling Class.method (××× tance, arguments). When defining an object method, must explicitly declare a first parameter, the parameter names are generally used self, internal data access objects. self here is the equivalent of C ++, Java inside of this variable, but we can also use any other legal parameter name, such as this and mine and other, self and C ++, Java inside this is not exactly the same, it can be seen as a habitual usage, we pass any other legal name will do, such as:

Getting Started with Python knowledge summary

To know some Python " " start and " " Special method names ending, they are used to implement operator overloading and achieve a variety of special features.

Types of

Python dynamic type system. At compile time, Python does not check whether an object has a method or property is called, but until runtime, only to make checks. You may throw an exception when the operands. However, although the Python dynamic type system, it is also strongly typed. Python prohibition operation is not clearly defined, such as digital plus strings.

Like other object-oriented languages, Python allows the programmer-defined type. An object configured to call type just like a function, such as, for Fish the type previously defined, using Fish (). Itself is a special type of type object type (type type type object itself), this particular design allows for reflection type programming.

Python's built-rich data types. Compared with the Java, C ++, data types effectively reduce the length of the code. The following list briefly describes the Python built-in data types (for Python 3.x):

1240

In addition to various data types, Python language is also represented by a function type, module, type itself, an object method, Python code compiled, run-time information and the like. Therefore, Python has a strong dynamic.

computation

Python using C, Java similar operators, supports integer and floating-point math operations. It also supports complex arithmetic with infinite number of digits (actually limited by the capacity of the computer) integer arithmetic. In addition to the absolute value function ABS (), the most mathematical functions and are within cmath math module. The former is for real operations, while the latter complex arithmetic. Need to import them when in use, such as:

import math

print (math.sin (math.pi / 2))

1.0

score calculating means for supporting fractions; means for decimal floating-point operations to support precision.

Python-defined value modulo operation a% b is in the open interval [0, b) within, if b is negative, the open zone to the (b, 0]. This is a very common way to define, but in fact it depends on the divisible in order to make the definition of the equation:. b * (a // b) + a% b = a constant true value required for the operation divides the negative direction of the infinitesimal 7 // 3 such a result is 2, and (-7) /. result / 3 is -3. the algorithm and many other programming languages ​​is not the same, need to pay attention, they will value the integer division in the direction of zero.

Python allows mathematics as usual wording as attached to write two comparative run character. For example a <b <c and a <b and b <c are equivalent. Results of C ++ and Python is not the same, first it first calculates a <b, 0 or 1 to obtain one of two values ​​depending on the size of both, and then compared with c.

Guess you like

Origin blog.51cto.com/14400687/2409262