The road to wudao-the six major days of the refining stage (the year has passed, and the upgrade is resumed ).

Source: Internet
Author: User

The road to wudao-the six major days of the refining stage (the year has passed, and the upgrade is resumed ).

Generator: The data generated during the call (inert computing, coroutine: retained the last entry status, each entry of the program, equivalent to the last call status, asynchronous ), concurrent effects under a single thread (switch between different roles ).

(3.x) range (), the list generation formula [I * I for I in range (20)],

File Reading:

For line in f: (here f is the file handle)

Print (line)

Remember the important method: __next _ (calculate the next state value), send (value) (pass the value to the yeild of the generator)

For example ):

Def seq (num ):

# Calculate several Fibonacci numbers

Count, x, y = 0, 0, 1

While count <num:

Yield y # Add what to the generator

X, y = y, x + y

Count = count + 1

Return 'end'

G = seq (10)

G. _ next _ ()-> 1

G. _ next _ ()-> 1

G. _ next _ ()-> 2

...

An exception occurs when the number of num is exceeded.

Capture exceptions:

Try:

# Program section

Except t StopIteration (exception type, which indicates that there are no more values ):

Print ('error ')

Break

Concurrent effect (asynchronous) under a single thread ):

Import time

Def employee (name ):

Print ('% s preparations' % name)

While True:

Work = yield

Print ('% s employee has completed % s work assigned by the boss' % (name, work ))

Def boss (name ):

Wang = employee ('wang ')

Zhang = employee ('zhang zhang ')

Wang. _ next _ () # It is equivalent to stopping the function into a generator in yield (run here)

Zhang. _ next __()

Print ('% s is about to receive the ticket' % name)

For I in range (10 ):

Print ('% d list received' % I)

Wang. send (I) # pass the value to yield

Zhang. send (I)

Time. sleep (1) # every second

Boss ('eldest brother ') # Call the ticket, and the boss receives the ticket, and the employee completes the work at the same time! (Unfamiliar breakpoint debugging process)

Iterator: an object that can be called by _ next _ and continuously returns the next value until an exception is thrown. Generator (generator), range (), f (file handle), [I * 5 for I in range (10)], for loop (underlying)

Iterable: objects that can be directly used in the for loop. list tuple, dict, set, and str (can be converted into iterator by Iter (), functions with yield, generator

From collections inport Iterable (Iterator) first introduces

Isinstance (object, Iterable (Iterator) to determine whether it is an iteratable object (Iterator)

 

Some built-in functions:

Abs () returns the absolute value.

Bool () Determines true or false

All () checks whether the element values in the iteratable objects are false or false.

Any () checks whether the values of elements in an Iterated object are true or not.

Bin () to binary-"B '.....'

Bytearray ('value', encoding = 'code'), binary variable array, and bytes variable

Callable () callable, function, class --- "()

The chr returns the character corresponding to the ASCII value. The ord character is the opposite.

 

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.