input - out --- correction (O correct)
*** a first module review
byte Type: data is stored to the hard disk, the hard disk can store binary.
gbk / utf-8 --- >> decode ( decoding) --- >> Unicode
the Unicode --- >> encode (encoding) --- >> gbk / utf-8 (2 hex)
word - "utf -8 / gbk - >> 2 binary
images - "jpg / png - >> 2-band
music -" mp3 / wav - >> 2-band
video - "mp4 / mov - >> binary
bytes type, expressed in hexadecimal form, two hexadecimal numbers (e.g. 0x e 5) constituting a byte (byte),
to 'b' to identify
1byte = 8bit
default file is encoded python3 utf- 8 (pycharm files are loaded by default with utf-8 encoded)
eg: f = open ( 'bytes.txt', 'W', encoding = 'GBK') # own designated coding
eg: f = open ( 'bytes.txt ',' wb '
Summary: 1, to become a character hard disk memory bytes
2, network transmission type character bytes to become
** depth Copy
dict, List, SET, (not the same point, and variable assignment)
S = { 'name': 'Alex', } .....
s2 = s, s, and s2 are shared at this time (refer to the same memory address (cup)), of the same data
s, s2 change dict, list, any one of a set of elements which will also change.
Depth copy (via the copy module)
s.copy, shallow copy (copy only the first layer) copy of the data,
only change (independent of the first layer) of the first layer, the data inside the nested disconnection changed will At the same time become
s.deepcopy, deep copy, all the layers are completely independent.
** transcoding
>>> S
'Hello future'
>>> s.encode ( 'utf-8') # encoded to utf-8 encoded into binary
b '\ xe4 (2-digit hexadecimal) \ xbd \ xa0 \ xe5 \ xa5 \ xbd \ xe6 \ x9c \ xaa \ xe6 \ x9d \ xa5 '# 12 bytes
>>> s_utf8 = s.encode (' UTF-. 8 ')
>>> s_utf8
B'
>>> s_utf8.decode ( 'utf-8' ) # to decode utf-8 format, may not be written, it will default to python3 utf-8
'Hello future'
** encoding and decoding (in utf-8 must be encoded format decoding utf-8, or will be error)
GBK /. 8 --- >> UTF-decode (decoding) --- >> Unicode
the Unicode --- >> encode (encoding) --- >> GBK / utf-8 (binary)
s.encode ( 'utf-8') - >> utf-8 format to encoded into binary
s.dncode ( 'utf-8') - >> 2 from str-ary decoded into Unicode
** encoding conversion: from the text-to-one encoding into another, such as from 8-gbk converted to UTF
Unicode: Unicode, the mapping between the relationship with all the coding
>>> S
'Hello future'
>>> s.encode ( 'UTF-. 8')
B '\ XE4 \ XBD \ XA0 \ xe5 \ xa5 \ XBD \ XE6 \ x9c \ Xaa \ XE6 \ x9d \ xa5'
>>> S
'Hello future'
>>> S.encode('gbk')
b'\xc4\xe3\xba\xc3\xce\xb4\xc0\xb4'
S_gbk = s.encode >>> ( 'GBK')
>>> s_gbk.decode ( 'GBK')
'Hello future'
>>> s_gbk.decode ( 'GBK'). Encode ( 'UTF-. 8') # gbk. 8-converted to UTF
B '\ XE4 \ XBD \ XA0 \ xe5 \ xa5 \ XBD \ XE6 \ x9c \ Xaa \ XE6 \ x9d \ xa5'
binary mode: RB WB ab &
R & lt text mode, the decoded binary to STR, the Unicode
** function
characteristics: 1, 2 to reduce duplication, the program is easy to make the extension 3, so that the program is easy to maintain
the return value (return), the suspension means function
def register (name, age, major , country = 'CN' , * args, ** kwargs): # this country is the default parameter
info = "" "
-------- your registration information ---------
name:% S
Age:% S
Major :% S
Country:% S
"" "% (name,age,major, country)
print(info)
Register ( 'Alex', 22 is, 'CS')
Register ( 'Al', 22 is, 'Math')
Register ( 'AX', 22 is, 'US', 'US')
function of the parameters: shape parameter and argument
function priority parameters: location parameter> default parameters / key parameters of
the external function code in order to obtain the execution result of the function, you can use return in a function
to return the results statement.
stu_register DEF (name, Age, Course, = 'PY', Country = 'CN'):
Print ( '----- registered student information -----')
Print ( 'the Name:', name)
Print ( ' Age: ', Age)
Print (' Country: ', Country)
Print (' Course,: ', Course)
IF Age> 22 is:
registriation_status = stu_register ( 'Alex', 22 is, Course = 'PY', 'the CN')
IF registriation_status:
Print ( 'registration success')
the else:
Print ( '. TOO Old to BE A Student')
* Note: 1. Function in the implementation process, as long as the return statement is encountered, it will stop execution and return the result, it
can be understood as the end of the return statement on behalf of the function.
2. If the function return is not specified, then the function returns None
function returns multiple values
DEF Test ():
return 1,2,3,4,5 # tuple will return multiple values wrap
print ( Test ())
** global variables and local variables (about locals () method, print all local variables
Globals () method, printing all global variables) (from local variable "nested variable" global variable)
name = 'Alex' # global variable
DEF Change ():
name = 'Golden King angle' local variable #
global name # (create) function in a global variable declared inside
Print (name)
Print (about locals ())
Print (Globals ())
Change ()
Print (name)
** function transfer lists, dictionaries, a phenomenon that occurs when a set of
d = { 'name': 'Alex', 'Age': 28, 'Hobby': 'dabaojian'}
L = [ 'rebeeca', 'Katrina', 'Rachel']
DEF change_data (info, Girls):
info [ ' Hobby '] =' Study '
girls.append (' Xiaoyun ')
change_data (d, L)
Print (d, L) ## DIC, Li this memory address has not changed, just inside the element becomes the
result is {d' name ':' alex ',' age ': 22,' hobbie ':' study '}
L is [ 'Rebeeca', 'Katrina', 'Rachel', 'Xiaoyun']
** nested function, anonymous functions and higher order functions
Nested function name = 'circle brassicae'
DEF Change ():
# name = 'circle brassicae, self-programming'
DEF change2 ():
#global name declared if the phrase, the following name is changed outermost global variables
# name = 'ape small circle, self-programming, not money'
print ( 'layer 3 print', name)
change2 () # call the inner function
print ( 'layer 2 print', name)
Change ()
Print ( 'outermost print', name)
anonymous function
C = the lambda X, Y: X ** Y
Print (C (2,8))
with the following code equivalent:
DEF Calc (X, Y):
return * X Y *
Print (Calc (2,. 8))
Examples: Calc DEF (X):
return X ** 2
#res = Map (Calc, [1,2,3,4,5,6])
RES = Map (the lambda X: X ** 2, [1,2 , 3,4,5,6]) # lambda added up to a triple function calculation
Print (RES)
for I in RES:
Print (I)
order function: 1, as a function of receiving one or more inputs 2, return another function returns
DEF get_abs (n-):
IF n-<0:
n-= int (STR (n-) .strip ( '-'))
return n-
DEF the Add (X, Y, F):
return F (X) + F (Y)
RES = the Add (. 3, -6, get_abs)
Print (RES)
bytes type
Guess you like
Origin www.cnblogs.com/ylkx/p/11222278.html
Recommended
Ranking