Objective:
Have to spit groove, write a half-day thing, directly lost, but also to rewrite, Got.
Basic Review: 1, Set
The set has 2 important functions: the relationship test (set, difference, Intersection) and De-weight.
2. File encoding
2.7 On the default file encoding is ASCII code, because it does not support chinese, out of the GB2312, on 2.7 to support the Chinese must declare the file encoding in UTF-8 format, UTF-8 and GB2312 relationship?
UTF-8 is an extended collection of unicode, Unicode includes national codes, many Chinese start programs or in GBK format, GBK backwards compatible gb2312,windows default encoding is Gbk.
Why should Unicode be done? In order to save space, the English language is 2 bytes, originally I use ASCII code in English only 1 bytes, but now with you 2, so out of UTF-8, in English is 1 bytes, Chinese Unity 3 Bytes.
If 1 files are GBK encoded, The other is UTF-8, if it is to read this file, it is necessary to make a conversion, but they can not directly convert, this time involves the problem of transcoding. So GBK converted to UTF-8, The syntax is first decode into unicode, and then encode into utf-8, see:
In 3.0, the default encoding is unicode, in 2.7 to print the Chinese will have to declare the character encoding #-*-coding:utf-8-*-
In 3.0 can not write, the default file encoding is unicode, then the file encoding is unicode, because Unicode is also supported in chinese, 2 bytes stored, do not need to convert to utf-8, to become Utf-8 also have to encode, as Follows:
' I was '. Encode ("utf-8")
。 Of course you can also declare the character encoding #-*-coding:utf-8-*-, then the current file encoding is utf-8.
3. functions
The format is as Follows:
def func_name (): Pass
Positional parameters, such as Arg1 and arg2
def func_name (arg1,arg2): passfunc_name (5,3)
5 corresponds to Arg1 3, which corresponds to arg2.
Key parameter, You can specify the parameter name, for example:
def func_name (arg1,arg2,arg3): passfunc_name (1,2, arg3=5)
Note that key parameters cannot be written in front of the position parameter.
Multiple parameters, The *args is used, for example:
def func_name (arg1,arg2,*args): passfunc_name (4,5,6,7 ,8) Then print out effects 4,5, (6,7,8)
Write the non-fixed parameters of the back to the Yuan Zu
**kwargs, print Out is a dictionary, for example
def func_name (arg1,arg2,arg3,*args,**kwargs): passfunc_name (3,4, a, 666,name=,Xiedi) printed out results 3,4, 77,(666) {'name':'xiedi'}
4. Local Variables and global variables
In general, local variables take effect only within the function and do not work outside of the Function.
When it comes to a scope problem, it only works in the function, the function is finished, the variable is gone, the scope only allows to change things in the Function.
Python route four