IX iterator class generator, modules and packages

First, a list of derivation of formula

  Even-numbered less than 10

li = [0,2,4,6,8,10] 

li = list (range (0,11,2))

  And repeated with

li=[]
for i in range(0,11,2):
    li.append(i)
   print(li)

  List can also be used to derive the formula

 
Original formula: 
Li = [] 
for I in Range (21 is): 
   IF I% 2 == 0: 
         li.append (I) 
is equivalent to: 
LI2 = [I for I in Range (21 is)% 2 == 0 I IF ] i # a first element that corresponds to the the append 

LI3 = [ 'a' for i in Range (21 is) IF i% 2 == 0] # if you are an even number, in a list of Riga 'a' 

LI4 = [i if i% 2 == 0 else 'a' for i in range (21)] # satisfied if the even add a i, otherwise add an 'a'. to add more conditions must be put back for
 

Second, the iterative and generator

  1, iterators

    Iterator object requires support for the iterator protocol objects in Python, support the iterator protocol is to achieve the object __iter __ () and __next __ () method.
    Wherein the __iter __ () method returns an iterator object itself; __ __ Next () returns the next container element method, initiated at the end of the StopIteration exception.

 
li = [1,2,3] # only iter, there is no next, it is not an iterator 

li [1] # Returns the index of the element 1, 2 
 
li .__ ITER __ () # li returns the iterator itself (the change li into an iterator), there is a trim function iter acting as 

g = li .__ iter __ () # assignment 

built-in functions: Next () 
Next (G) # returns the index 0, to achieve the role of the iterator, with g .__ nest__ () acts like 
next (g) # returns the index value of 0 
next (g) # returns the index value of 0 
next (g) # error StopIteration 

NOTE: You must assign a value () after the iter to a variable, otherwise it will redefine 

cycle: 
G = ITER (Li) 
for I in G: 
    Print (I, End = '') 
output: 1 2 3
 

 

  2, generator generator

 
Fun DEF (): 
    I =. 1 
    the while I <. 5: 
        Print (I) 
        the yield 'STOP' # plus side put fun () into an iterator, the call next () is to play the role of blocker (pause), plus (similar to the return on the role, will output 'stop') 'stop'. Total character on it, is to achieve a builder functions: pause, return value 
        I + =. 1 
        Print ( 'AAA', I) 

Next () # output. 1 
Next () # output AAA 2 
                    2 
Next () # Output: aaa 3 
                    3 

generator is essentially an iterator
 
 
Feibolaqi number 
1,1,2,3,5,8,13,21,34 ,. . . 
A number generator to write Feibolaqi 
DEF FIB (NUM): 
    n-# = 0 indicates generations 
    A, B = 0,1 
    the while n-<NUM: 
        Print (B) represents the amount that #b to generation 
        if n% 10 0 ==: 
            the yield # add a generator can see a 
        a, B = B, a + B 
        n-+. 1 =
 

Third, the package module, and

  Module: py file

  Package: Many py files in a folder

  Methods: Functions

  Properties: variable name

  1, built-in module

  2, third-party modules

  3, the import module

import copy # import entire module 

from copy import deepcopy # turned large modules inside the small module, saving memory, designated introduced

    May also be introduced to cmd.exe pip

  4, a custom module

    The same path: direct import file name

    Different paths: With one other module sys sys.path will find from your python installation path

import sys # function call sys 
sys.path # search for your python folder all py file path 
sys.path.append (r 'absolute path') # absolute path to your import 
import absolute path where the module can then # the calling module    

    __main__ meaning:
    When the file is run directly is, __name__ document is __main__, when the file is imported, __ name__ is the file name

  

Guess you like

Origin www.cnblogs.com/sysun110/p/11267588.html