Python advanced tutorial

Source: Internet
Author: User
This article mainly introduces the loop object in the python advanced tutorial. The loop object is such an object, which contains a next () method. the purpose of this method is to complete the next result, the main purpose of this lecture is to provide a basic concept for loop objects while reading Python programs.

The circular object does not exist with the birth of Python, but it develops rapidly. especially in the era of Python 3x, circular objects are becoming the standard form of loops.

What is a circular object?

A circular object is such an object. it contains a next () method (_ next _ () method, in python 3x ), the purpose of this method is to proceed to the next result, and after the end of a series of results, the StopIteration error is cited.

When a loop structure (such as for) calls a loop object, it calls the next () method every time it loops until the StopIteration occurs and the for loop receives, you will know that the loop has ended and stop calling next ().

Suppose we have a test.txt file:

The code is as follows:


Run the following python command line:

The code is as follows:

>>> F = open('test.txt ')
>>> F. next ()
>>> F. next ()

Enter f. next () until the StopIteration occurs.

Open () actually returns a circular object containing the next () method. The next () method returns the content of a new row each time, and the StopIteration is cited at the end of the file. In this way, we perform a loop manually.

If it is automatically executed, it is:

The code is as follows:

For line in open('test.txt '):
Print line

Here, the for structure automatically calls the next () method and assigns the return value of this method to line. The loop will end when StopIteration occurs.

Compared with sequences, the benefit of using cyclic objects is that you do not need to generate the elements to be used before the loop starts. The elements used can be generated in a loop. In this way, space is saved, efficiency is improved, and programming is more flexible.


Technically, there is also an intermediate layer between the loop object and the for loop call, which is to convert the loop object into an iterator ). This conversion is implemented by using the iter () function. However, this layer can be ignored logically, so Loop objects and iterators often refer to each other.


The main purpose of generator is to create a user-defined cyclic object.

The writing method of the generator is similar to the function definition, but the return is changed to yield. The generator can have multiple yield instances. When the generator encounters a yield, it will pause running the generator and return the value following yield. When the generator is called again, it will continue running from the paused place until the next yield. The generator itself forms a loop, and each cycle uses a value returned by yield.

The following is a generator:

The code is as follows:

Def gen ():
A = 100
A = a * 8
Yield 1000

The generator has three yield instances. if it is used as a loop generator, it performs three cycles.

The code is as follows:

For I in gen ():
Print I

Consider the following generator:

The code is as follows:

Def gen ():
For I in range (4 ):
Yield I

It can also be written as a Generator Expression (Generator Expression ):

The code is as follows:

G = (x for x in range (4 ))

Generator expressions are a simple way to compile generators. For more information, see.

Table derivation

List comprehension is a quick way to generate tables. Its syntax is simple and has great practical value.

Suppose we generate table L:

The code is as follows:

L = []
For x in range (10 ):
L. append (x ** 2)

The preceding table L is generated, but there is actually a quick writing method, that is, the table deduction method:

The code is as follows:

L = [x ** 2 for x in range (10)]

This is similar to the generator expression, except that brackets are used.

(The mechanism of table derivation is actually to use circular objects. if you are interested, you can refer to them .)

What will be generated by table derivation below the exercise?

The code is as follows:

Xl = [1, 3, 5]
Yl = [9, 12, 13]
L = [x ** 2 for (x, y) in zip (xl, yl) if y> 10]


Loop object
Table derivation

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: 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.