slices(intercept element, can operate list,tuple,string)
L = List (range) # [0,1,2,3,...,]l[0:10] = l[:10] # intercept the first 10 numbers [0,1,2,..., 9 ]# l[-10: # 10 numbers after intercept [90,91,92,...,]l[10:20] # intercept elements of 10-20 positions [ 10,11,12,13,...,]l[:10:2] # first 10 numbers, every two take one [0,2,4,6,8]L[::5] # all numbers, fetch 1 per 5 [0,5,10,...,] l[:] # Copy the original array
iterative for...in ...
D = {'a'b'c': 3} # By default, the Dict iteration is key. If you want to iterate over value, you can use for value in D.values (), if you want to iterate both key and value at the same time, you can use for K and V in D.items ()
for in Enumerate (["a""b""C" ]): Print(i, value) # Iteration list needs to use the Enumerate function # 0 A# 1 b# 2 C
List-generated
List (range (1,11))#generate [,..,][X*x forXinchRange (1,11)]#build [1*1,2*2,3*3,..., 10*10]#list Generation and if-judging[X*x forXinchRange (1,11)ifx%2 ==0]#build [2*2,4*4,6*6,8*8,10*10]#double-layer loop generation full array[X+y forXinch "ABC" forYinch "XYZ"]#generate [' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']
Generator: Genterator (side loop calculation, if a function contains the yield keyword, then the function is a generator)
- The list generation [] should be ()
for in range (1,10)# Call Method:# each time you call the next () method to get the element, you can only get one for theNext () method print(n) is constantly called inside the For loop
def fib (max): = 0, 0, 1 while n < Max: yield b = b, A + b = n + 1
return'done'
iterators
- An object that can act directly on a for loop iterable
from Import iterableisinstance ([], iterable) # True use Isinstance () to determine whether the Iterable object
- An object that can be called by next () and continually returns the next value Iterator
from Import for in range (Iterator) # use Isinstance () to determine if the object is Iterator, The iterator object is an inert evaluation and is evaluated only when needed
Python functions Advanced Features