Python advanced-------python2.7 Tutorial Learn "Liao Xuefeng Edition" (iii)

Source: Internet
Author: User

June 9, 2017 17:57:55

Task:

Read the Advanced section

Notes:
1. By mastering Python's data types, statements, and functions, you can basically write a lot of useful programs.
2. In Python, the more code is better, the less the better. The more complex the code is, the better it is, but the simpler the better.
Based on this idea, we introduce the advanced features that are very useful in Python, the functionality that a line of code can implement, and never write 5 lines of code.
3. It is cumbersome to use loops for operations that often take a specified index range, so Python provides a slicing (Slice) operator that can greatly simplify this operation
4. In many programming languages, there are many kinds of interception functions for strings, in fact, the purpose is to slice the string. Python does not have an intercept function for a string, it can be done simply by slicing one operation.
5. With the slicing operation, many local loops are no longer needed. Python's slices are very flexible, and one line of code can implement many lines of work to complete.
6.tuple is also a list, the only difference is that the tuple is immutable. Therefore, a tuple can also be used for slicing operations, but the result of the operation is still a tuple:
7. Given a list or tuple, we can traverse the list or tuple through a for loop, which we call iteration (iteration).
8. In Python, iterations are done through for ... in, and many languages such as C or Java, the iteration list is done by subscript
The For loop of the 9.Python is more abstract than the Java for loop because the for loop of Python can be used not only on a list or a tuple, but also on other objects that can iterate.
10.ist This data type although there is subscript, but many other data types are not subscript, but as long as the object can be iterated, whether or not subscript, can iterate, such as dict can iterate:
1. Because Dict storage is not ordered in list order, the resulting order of the iterations is likely to be different.
2. By default, the Dict iteration is key. If you want to iterate over value, you can use for value in D.itervalues (), if you want to iterate both key and value at the same time, you can use a for-K, V in D.iteritems ().
3. A string is also an iterative object, so it can also be used for a for loop
4. How can I tell if an object is an iterative object? The method is judged by the iterable type of the collections module.
5. The last small question, what if I want to implement a Java-like subscript loop for a list? Python's built-in enumerate function can turn a list into an index-element pair so that both the index and the element itself can be iterated in the For loop:

>>> for I, value in enumerate ([' A ', ' B ', ' C '):
... print I, value
...
0 A
1 B
2 C
6. The above for loop, which also refers to two variables, is very common in python, such as the following code:

>>> for x, y in [(1, 1), (2, 4), (3, 9)]:
.... print x, y
...
1 1
2 4
3 9
7. Any iterator object can be used for a for loop, including our custom data types, and can be used for loops as long as the iteration criteria are met.
8. The list generation, which is the comprehensions, is a very simple and powerful built-in Python build that can be used to create lists.
9.for loops can actually use two or more variables at the same time, such as Dict's Iteritems () can iterate both key and value at the same time:
10. Using the list generation, you can quickly generate a list, you can deduce from a list another list, and the code is very concise.
1. Generator
With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the Generator (Generator).
There are a number of ways to create a generator. The first method is simple, as long as a list-generated [] is changed to (), a generator is created:
The 2.generator (generator) is also an iterative object.
If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator:
3. The most difficult thing to understand is that generator is not the same as the process of executing a function. The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.
4.generator is a very powerful tool, in Python, you can simply change the list generation to generator, or you can implement complex logic generator through functions.
To understand how generator works, it is continuously calculating the next element during the for loop and ending the For loop in the appropriate condition. For the generator of a function, a return statement or execution to the last line of the function body is the end of the generator instruction, and the For loop ends with it.

Summarize:

Slice

Iteration

List-Generated

Generator

are important, but also good understanding, slicing is not equivalent to the string interception function in other languages, iteration is the loop Ah, list generation is very flexible, very convenient, generator is greatly reduce memory, when the user needs to be extracted.

Come on, it's time to look at functional programming!

Python advanced-------python2.7 Tutorial Learn "Liao Xuefeng Edition" (iii)

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.