Here is some basic knowledge of Python, the main content is the use of higher-order functions. Perhaps my pericardium has a crust, and the things that can break into the shell are extremely limited. That's why I can't passionately devoted people.
The higher order function one, the map (), the Reduce (), and the filter () function in Python use
The map () function receives two parameters, one is the function, and the other is iterable,map the incoming function to each element of the sequence and returns the result as a new iterator.
def f (x): return x * xprint# [1, 4, 9, +, +]print(list (map (Lambda # [1, 4, 9, approx. , +--]
Reduce functions a function in a sequence [X1, x2, x3, ...] , the function must receive two parameters, and reduce accumulates the result and the next element of the sequence.
from Import Reduce def Add (x, y): return x + yprint# print (Reduce (lambda #
The filter () function is used for filtering sequences.
def is_odd (n): return n% 2 = = 0print# [2, 4, 6, 8]print(list (filter (lambda< /c9># [2, 4, 6, 8]
The sorted () function is used for sorting.
def ownsort (n): return = [ -3, 9,-7, ten]print# [-7, -3, 9, ten]print# [-3 ,-7, 9,print# [9, 7,-3]print# [10,-3,-7, 9 ]
Second, the scope of the Python variable understanding
defscope_test ():defdo_local (): Spam="local spam" defdo_nonlocal (): nonlocal spam spam="nonlocal spam" defDo_global ():Globalspam spam="Global spam"spam="test spam"do_local ()Print("After local assignment:", spam) do_nonlocal ()Print("After nonlocal assignment:", spam) Do_global ()Print("After global assignment:", spam) scope_test ()Print("In global scope:", spam)#After local assignment:test spam#After nonlocal assignment:nonlocal spam#After global assignment:nonlocal spam#In global scope:global spam
Note how the local assignment (which are default) didn ' t change scope_test ' s binding of spam. The nonlocal assignment changed Scope_test ' s binding of Spam, and the global assignment changed the module-level binding.
Third, a case of the Python in the association process
ImportAsyncioImportRandomImportThreadingasyncdefHello (index):Print('Hello world! index=%s, thread=%s'%(Index, Threading.currentthread ())) await Asyncio.sleep (Random.randint (1, 5)) Print('Hello again! index=%s, thread=%s'%(Index, Threading.currentthread ())) Loop=Asyncio.get_event_loop () Tasks= [Hello (1), Hello (2)]loop.run_until_complete (asyncio.wait (Tasks)) Loop.close ()
The results of the operation are as follows:
Hello world! index=237900) >HelloWorld! index=137900) >Hello again! index=137900) >Hello again! index=237900) >
Iv. base64 encoding and decoding in Python
Import Base64 # base64 encoded m = Base64.b64encode (b'my name is HUHX. ' )print# B ' bxkgbmftzsbpcybodwh4lg== '# # base64 decoding bytes = Base64.b64decode (m) Print # B ' My name is Huhx. '
Simple use of the codecs module
Print('China'. Encode ('Utf-8'))#b ' \xe4\xb8\xad\xe5\x9b\xbd 'Print('China'. Encode ('GBK'))#b ' \xd6\xd0\xb9\xfa 'ImportCodecsPrint(Codecs.encode ('China','Utf-8'))#b ' \xe4\xb8\xad\xe5\x9b\xbd 'Print(Codecs.encode ('China','GBK'))#b ' \xd6\xd0\xb9\xfa '
The documentation for the Encode method of the Str object is as follows:
defEncode (self, encoding='Utf-8', errors='Strict'):#real signature unknown; restored from __doc__ """S.encode (encoding= ' utf-8 ', errors= ' strict '), bytes encode S using the codec registered for encoding. D Efault encoding is ' utf-8 '. Errors May is given to set a different error handling scheme. Default is ' strict ' meaning that encoding errors raise a unicodeencodeerror. Other possible values is ' ignore ', ' replace ' and ' xmlcharrefreplace ' as well as ' all other ' name registered with code Cs.register_error that can handle unicodeencodeerrors. """ returnB""
Friendship Link
Python Basics---->python Use (v)