One, character encoding and transcoding
First, Python2.
- The default encoding in PY2 is ASCII
- The code declaration at the beginning of the file is to tell the program that explains it to read the code into memory, because in memory, this code is actually stored in the bytes binary format, but even the 2 binary stream can be converted to 2 streams in different encoding formats, you know?
- If the file header is declared #_*_coding:utf-8_*_, you can write Chinese, do not declare, Python in the processing of this code by ASCII, obviously error, add this declaration, the inside of the code is all Utf-8 format
- In the case of #_*_coding:utf-8*_, you declare the variable if it is written name=u "Big Health", that character is Unicode format, do not add this u, then you declare the string is utf-8 format
- Utf-8 to GBK How to turn, UTF8 first decode into Unicode, then encode into GBK
Besides, Python3.
- Py3 in the default file encoding is Utf-8, so you can directly write Chinese, do not need to file header declaration code, dry Beautiful
- The variable you declare is Unicode encoding, not utf-8, because the default is Unicode (unlike in py2, you want to declare it directly to Unicode and add a U before the variable), and if you want to turn into GBK, direct your_str.encode ("GBK" ) that can
- But Py3, you in Your_str.encode ("GBK"), feel as if also added an action, that is, encode data into bytes, I rub, this is how a situation, because in Py3, str and bytes made a clear distinction, You can understand that as bytes is the 2 stream, you will say, I see not 010101 such a 2, that is because Python to allow you to manipulate the data and at the memory level to help you to do a layer of encapsulation, or let you directly see a bunch of 2 binary, can you see which character corresponds to which paragraph 2 binary? What the? Your own conversion, come on, you even more than 2 digits of the number plus and minus operations are laborious, or save worry bar.
- Then you said, there seems to be a bytes in the Py2, yes, but py2 bytes only to str made an individual name (Python2 str is bytes, py3 str is Unicode), there is no like py3 to show you a more layer of encapsulation, But in fact, its interior is encapsulated. So let's say, whether it's 2 or three, from hard disk to memory, the data format is 101,012 to-->b ' \xe4\xbd\xa0\xe5\xa5\xbd ' bytes type--follow the specified code to the text you can read
In the Py3 encode, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string
The Transcoding method is as follows:
1 __author__='NL'2 3 ImportSYS4 5 Print(Sys.getfilesystemencoding ())6 Print(Sys.getdefaultencoding ())7 8str ="Coding Test" #Python3, the default encoding format is Unicode9 Print(str)Ten OneSTR_GBK = Str.encode ("GBK")#while encoding, convert the string to bytes AStr_decode = Str_gbk.decode ("GBK")#while decoding, turn the bytes into a string - Print(STR_GBK)#output Result: B ' \xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4 ' - Print(Str_decode)View Code
Second, function
1. Basic definition of function
__author__='NL' defFunc1 ():#function without parameters, no return value,---"Procedure Print("func1") defFunc2 ():#functions without parameters, with return value,---"Function Print("func1") return0func1 ()#function CallFunc2 ()Print(Func1 ())#The return value is NonePrint(Func2 ())#The return value is 0
2. Functions with Parameters
1 __author__='NL'2 3 deffunc1 (x, y):4z = x +y5 returnZ6 7 defFUNC2 (*args):8 returnargs9 Ten defFUNC3 (* *Kwargs): One returnKwargs A - defFunc4 (x,y=3,*args,**Kwargs): - returnX,y,args,kwargs the - Print(Func1 (3,4))#result is 7 - Print(Func2 (1,"haha", [1, 5,"Kkkte"]))#The result is a tuple: (1, ' haha ', [1, 5, ' Kkkte ') - Print(Func3 (name="NL", age=22,sex="M"))#The result is dict:{' name ': ' nl ', ' age ': $, ' sex ': ' M '} + Print(Func4 (4,9,"Nihao", [1,2,3],name="WQX"))#The result is: (4, 9, (' Nihao ', [1, 2, 3]), {' name ': ' WQX '})Key parameters
Under normal circumstances, to pass parameters to the function in order, do not want to order the key parameters can be used, just specify the parameter name, But remember a requirement is that the key parameters must be placed after the position parameter.
Recursive functions
Code demo, two-point lookup:
Recursive properties:
1. There must be a clear end condition
2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.
Python Basics (DAY3)