Yesterday's content review
Objects that can be iterated:
An internally contained __iter__ method is an iterative object.
An iterative object cannot be evaluated because the __next__ method is not included internally.
you can iterate over objects---> iterators.
obj.__iter__ ()
iter (obj)
iterators:
An object that contains the __iter__,__next__ method internally.
Advantages of iterators:
1, save memory.
2, inertia mechanism.
3, unidirectional, irreversible.
How to judge an iterator that iterates over an object.
1,__iter__ in dir (obj)
2,?
Generator:
An iterator that writes its own Python code is the generator.
The generator itself is an iterator.
def func ():
print (1111)
count = Yield 222
print (count)
Yield 333
g_obj = func ()
g_obj.send (666)
g_obj.__next__ ()
--------------->>>>>>>>>>>>> This section key generator expression list deduction <<<<<< <<<<<<<<<<<-------------------
Build a list: [' Python1 period ', ' python2 period ', ' Python3 period ', ' python4 period ', ' python6 period ', ' python7 period ', ' python8 period ', ' python9 period ', ' python10 period ']
L1 = []
For I in Range (1,14):
L1.append (' python%d period '% i)
Print (L1)
List Deduction formula:
# l3 = [' python%s period '% i for I in range (1, 14)]
# Print (L3)
[Variable (processed variable) for variable in iterable] loop mode
Advantages
# 1, Save the code, one line is done.
# 2, looking up on the tall.
Disadvantages
#, bad troubleshooting.
#整体:
# any list object constructed with a list deduction can be constructed in other ways.
A very complex list, the list derivation is not built,
# The list derivation is more magical.
# Build a list:
# [' Technician No. No. 0 ', ' Technician 1th ' ... ' Technician No. 15th '
# print ([' technician%s '% i for I in range (16)])
# [' Nurse No. 1th ', ' Nurse number 3rd ', ' Nurse 5th ', ..., ' Nurse 13th ']
# print ([' Nurse%s '% i for I in range (1, 14, 2)])
Classification
# [variable (processed variable) for variable in iterable] loop mode
# [variable (processed variable) for variable in iterable if condition] Filter mode
# 10 of the squares of all numbers within
# print ([I*i for I in range (11)])
# 30 of all the squares that can be divisible by 3
# print ([i**2 for I in range] if I% 3 = = 0])
# 100 of all the odd numbers within.
# print ([i-I in range (1,101,2)])
# print ([i-I in range (101) if I% 2 = = 1])
Builder expression
1. The [] Conversion of the list parsing [] to () is the generator expression
2. List parsing and builder expressions are a convenient way of programming, except that generator expressions are more memory-efficient
G_obj = (' python%s period '% i for I in range (1,14)) # Loop mode
G_obj1 = (' python%s period '% i for I in range (1,14) if I% 2 = = 0) # Filter mode
Print (G_obj)
Print (g_obj.__next__ ())
Print (g_obj.__next__ ())
Print (g_obj.__next__ ())
Print (g_obj.__next__ ())
For I in G_obj1:
Print (i)
# generator: Two different ways:
# generator function. Yield
# Builder Expression
# Why there are generators:
# The generator itself is built with Python code, which satisfies the individual needs and needs of your future work.
#Take the characters in the list names with two "E"#names = [[' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe '],#[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' Jennifer ', ' Sherry ', ' Eva ' ]List-derived#L2 = [name for i in names for name in I if Name.count (' e ') = = 2]Common methods:#L1 = []#For i in Names:#For name in I:#if Name.count (' e ') = = 2:#l1.append (name)#print (L1)#print (L2)swap the K,v key values in the Mcase position#mcase = {' A ': ten, ' B ':#print ({Mcase[i]: I for I in Mcase})
------>>>>>>> Built-in functions:
This thing slowly learn, you can hold down CTRL to see the source code, you can help a bit, it will be used
1.1 Scope-related
Locals: The function returns all local variables of the current position as a dictionary type.
Globals: The function returns all global variables in the dictionary type.
A = 1b= 2Print(Locals ())Print(Globals ())#These two are the same, because they are executed globally. ##########################deffunc (argv): C= 2Print(Locals ())Print(Globals ()) func (3)#These two are different, locals () {' argv ': 3, ' C ': 2}#Globals () {' __doc__ ': None, ' __builtins__ ': <module ' builtins ' (built-in);, ' __cached__ ': None, ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x0000024409148978>, ' __spec__ ': None, ' __file__ ': ' d:/lnh.python/.../built-in function. py ', ' Func ': & Lt;function func at 0x0000024408cf90d0>, ' __name__ ': ' __main__ ', ' __package__ ': None}
1.2.1 Execution of String type code Eval,exec,complie
Eval (' 2 + 2 ') # 4
N=81eval ("n + 4") # 85
Eval (' Print (666) ') # 666
EXEC: Executes the string type code.
s = "'
For i in [+/-):
Print (i)
‘‘‘
EXEC (s)
Complie useless, can not remember
Other functions in this I will not list, if there is a need to find the mother
Python basic 14-pass-through expression built-in function