Python Advanced knowledge points

Source: Internet
Author: User

1. Slice: intercept list,tuple values within the specified range: >>l[0,3]

2. Given a list or tuple, we can traverse the list or tuple through a for loop, which we call iteration (iteration). In Python, the iteration is done through for ... in to X in L print (x)

3. When the list is generated, it is the dynamic creation of the list, the elements to be generated X * x to the front, followed by the For loop, you can create a list, very useful, write a few more times, you can quickly become familiar with this syntax.

The For loop can also be followed by an if judgment so that we can filter out only the even squares:

>>> [x * x for x in range (1, one) if x 2 = = 0] [4, 16, 36, 64, 100]

You can also use a two-layer loop to generate a full array:

>>> [M + N for m in ' ABC ' for n ' XYZ '] [' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']

By using the List Builder, you can quickly generate a list that can derive another list from a list, while the code is very concise.

4.              Generator in Python, this side loop calculates the mechanism of creating a list, called the generator: generator, which saves a lot of space g= (x * x for x in range)] for x in G: Print (x)

5. These objects that can directly act on a for loop are called an iterative object: Iterable.      An object that is called by the next () function and constantly returns the next value is called an iterator: Iterator. You can use Isinstance () to determine whether an object is a iterator object: a summary all objects that can be used for a for loop are iterable types;

All objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations;

Collection data types such as list, Dict, str, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER () function.

Python's for loop is essentially implemented by constantly invoking the next () function, for example:

For x in [1, 2, 3, 4, 5]: Pass

6. One of the features of functional programming is that it allows the function itself to be passed as a parameter to another function, and also allows a function to be returned! Visible, ABS (-10) is a function call, and ABS is the function itself. The description variable F is now pointing to the ABS function itself. The direct call to the ABS () function is exactly the same as the call variable F ().

7. Writing higher-order functions is to allow the function's parameters to receive other functions. Map,reduce

The 8.filter () function Python's built-in filter () function is used for filtering sequences.

9. Sorting algorithm Python's built-in sorted () function allows you to sort the list: sorted () is also a higher-order function. The key to sorting with sorted () is to implement a mapping function. >>> Sorted ([36, 5,-12, 9,-21]) [-21,-12, 5, 9, 36] to reverse-order without changing the key function, you can pass in the third parameter reverse=true:

>>> sorted ([' Bob ', ' about ', ' zoo ', ' credits '], Key=str.lower, reverse=true) [' Zoo ', ' credits ', ' Bob ', ' about ']

10. The inner function sum of the closure can refer to the parameters and local variables of the external function lazy_sum, and when Lazy_sum returns the function sum, the relevant parameters and variables are stored in the returned function, which is a very powerful program structure called "Closure (Closure)". Higher-order functions can also return a function as a result value, in addition to the ability to accept functions as parameters.

11. The anonymous function keyword lambda represents an anonymous function, and the x preceding the colon represents the function parameter. >>> list (map (lambda x:x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])) [1, 4, 9, 16, 25, 36, 49, 64, 81

12. The adorner automatically prints the log before and after the function call, but does not want to modify the definition of the now () function, a way to dynamically add functionality during the run of the code, called an "adorner" (Decorator).

13. Partial function Simple Summary The function of functools.partial is to fix some parameters of a function (that is, to set the default value) and return a new function, which is simpler to call the new function.

14. Module in Python, a. py file is called a module. In order to write maintainable code, we grouped many functions into separate files, and in order to avoid conflicting module names, Python introduced a way to organize modules by directory, called packages. Scope in a module, we may define many functions and variables, but some functions and variables we want to use for others, some functions and variables we want to use only within the module. In Python, this is done through the _ prefix.

15 normal function and variable name is public, can be directly referenced, such as: ABC,X123,PI, etc.;

A variable like __xxx__ is a special variable that can be referenced directly, but has a special purpose, such as the __author__,__name__ above is a special variable, and the document annotation defined by the Hello module can also be accessed with a special variable __doc__. Our own variables are generally not used in this variable name;

Functions or variables such as _xxx and __xxx are non-public (private) and should not be referenced directly, such as _ABC,__ABC, etc.

The reason why we say that private functions and variables should not be directly referenced, rather than "cannot" be directly referenced, is because Python does not have a way to completely restrict access to private functions or variables, but from a programming habit should not refer to private functions or variables.

Because Python is cross-platform, nature should also provide a cross-platform, multi-process support. The multiprocessing module is a multi-process module with cross-platform versions. The multiprocessing module provides a process class to represent a processing object

Python Advanced knowledge points

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.