Python 12th Day

Source: Internet
Author: User

Second module learning: Generators, iterators, built-in functions

Generator features: Only when the corresponding data is generated at the time of invocation, the speed of running is fast!

Example:

1 defFil (max):#Fibonacci number2n,a,b=0,0,1#Defining initial Data3     4      whilen<Max:5         yieldb6A,b=b,a+b#Enter the second assignment7N+=18     9         Ten     return '---deno----' One  AA=fil (10) -  whileTrue: -     Try: the         Print(A.__next__())#Print return value -     exceptStopiteration as E:#Exception return -         Print('XXXXXX', E.value) -          Break

Yield Generator breakpoint cache can be assigned to variables

. Send () can transmit values for yield, data

Example:

1 #using generators to form multiple concurrency2 #3name_1='point of Sale a'4Name_2='Point of Sale b'5Name_sc='Dafeng Factory'6yf='clothes'7 8 defA (name):9     Print("product%s ..... Ready to finish ..."%name)Ten      whileTrue: OneDoc=yield#Generator Breakpoint Cache A         Print('%s in Sales \033[32;1m%s\033[0m'% (Name,doc))#incoming print out -  - #c=a (' Sales point A ') the #c.__next__ () #首次运行加载 Doc - #yf= ' clothes ' - #c.send (YF) - #c.__next__ () + #c.__next__ () - #c.__next__ () + defB (name): A     Print("product%s ..... Ready to finish ..."%name) at      whileTrue: -Doc=yield#Generator Breakpoint Cache -         Print('%s in Sales \033[32;1m%s\033[0m'% (Name,doc))#incoming print out -  - defSC (name):#Production Home -      inC=A (name_1) -D=B (name_2) toC.__next__()#First Load +D.__next__() -     Print('The%s started production ......'%name) the      forXinchRange (10): *         Print('%s Part%s was produced'% ((x+1), YF)) $ c.send (YF)Panax Notoginseng d.send (YF) -  theSC (NAME_SC)

Iterators: Iterator

An iterative object that can return the next value, which can be called an iterator

Iteration object: iterable

Objects that can be used for looping, such as lists, dictionaries, strings, etc., can be called iterative objects!

ITER () can convert an iterative object to an iterator

Generators are part of an iterator

Built-in functions:

1 ABC (i)# Returns an absolute value of 2 All ()# returns true 3 any () when the iterated object value is True    Returns True when the value of an iterator object has a true 4 ASCII ()# can convert the list to an ASCII table corresponding to the string output 5 bin () # decimal to binary system ..... .........

Python 12th Day

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.