Python Study the third day

Source: Internet
Author: User
Tags function definition generator generator iterable

List-generated lists comprehensions
[element to be generated x for loop x if judgment]
[element to be generated x, Y for loop × for loop y]
[key+ ' = ' + ' value ' for Key,value in Dict.items ()]

Isinstance (X,type); Judging variable types
Pass statement to occupy a bit and no egg to use

The generator generator one side of the loop while the mechanism of the computation generator saved is the algorithm
L = [x for x in range (0,11) if x%2 = = 0]
Change the list-generated "[]" to "()" that is the generator
L = (x for x in range (0,11) if x%2 ==0);
Generator view data needs to use next (x); You can get one data per execution or use a for iteration

If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator.

function differs from generator function
The function is executed sequentially to the last line or returned in the event of a return
The generator function executes at each call to next () when it encounters a yield statement that returns execution from the yield statement that was last returned

An iterator iterator an object that can be called by the next () function and continually returns the next value is called an iterator.
The ITER () function turns list,dict,str into iterator objects

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 Study the third day

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.