day09 depth copy, exception handling, basic file processing, the absolute and relative paths

Summary data type classification
1) according to the number of stored value
stored values: integer / floating point / string
stored plurality of values: List / dictionary / tuple / set

2) in an ordered or random
order: string / lists / tuples
disorder: Dictionary / set

3) at a variable or non-variable
variable: list / dictionary / set
immutable: integer / floating point / string / tuple

Python shades copy
1) copy (assigned)
= lt [. 1, 2,. 3]
LT2 = lt
lt.append (. 4)

Print (lt) # [. 1, 2,. 3,. 4]
Print (LT2) # [. 1, 2,. 3,. 4]
because the list is variable type, so lt value change, the value will change along lt2 2) shallow copy
Copy Import 
LT2 case where there is no change
lt = [. 1, 2,. 3]
LT2 = copy.copy (lt)
lt.append (. 4)
Print (lt) # [. 1, 2,. 3,. 4]
Print (LT2) # [ 1, 2, 3]


where lt2 change
lt = [1, 2, 3, [. 4,. 5,. 6]]
lt2 = copy.copy (lt)
lt.append (. 4)
Print (lt) # [1, 2 ,. 3, [. 4,. 5,. 6],. 4]
Print (LT2) # [. 1, 2,. 3, [. 4,. 5,. 6]]

lt [-1] .append (. 7)
Print (lt) # [. 1 , 2,. 3, [. 4,. 5,. 6,. 7]]
Print (LT2) # [. 1, 2,. 3, [. 4,. 5,. 6,. 7]]


. 3) deep copy
Import copy
lt = [. 1, 2, . 3, [. 4,. 5,. 6]]
LT2 = copy.deepcopy (lt)

lt.append (. 4)
Print (lt) # [. 1, 2,. 3, [. 4,. 5,. 6],. 4]
Print (LT2) # [1, 2, 3, [4, 5, 6]]
lt[-1].append(7)
print(lt) # [1, 2, 3, [4, 5, 6, 7]]
print(lt2) # [1, 2, 3, [4, 5, 6]]
Bearing in mind: Copy / Copy shallow / deep copy only for variable data type 

copy: when the copy target lt lt2 is, changes in the type lt variable, lt2 change; a change in the type immutable lt, lt2 change
simple assignment

shallow copy: when the copy of the object as a pale lt2 lt variably changes within lt type, lt2 change; a change in the type immutable lt, lt2 is not changed
copy.copy (built-in method in the variable data type). copy ()

deep copy: when the depth of the copy target lt2 lt variably changes within lt type, lt2 not change; a change in the type immutable lt, lt2 constant
copy.deepcopy ()

no applications, often interview Q.




exception handling
syntax anomalies (errors are SyntaxError)
IF # SyntaxError
0 = 1 # SyntaxError
Exception Logic (error type is often not the same) 
1/0
DIC = {}
Print (DIC [ 'skldfj']) # a KeyError

the except:
the try: # attempt
num = input ( 'Enter a value') # 123 124
DIC = { '0': 'A'}
Print (DIC [NUM]) # DIC [ '123 124']

Print (. 3) # Code from upper and lower, upper runs
1 / int (num) # error code does not affect the other, it is given immediately try to terminate inside the indented code
Print (. 4)

the except the ZeroDivisionError AS E: # # try inside the code except the what went wrong, what mistakes you have to capture the # as the error is assigned to E
print ( 'e:', e)
except KeyError AS E: # can write multiple capture multiple exceptions except
print ( 'e:', e )


Exception:
most important, after trying to capture abnormal use this, just remember that this is enough to use
Print (1)
the try: # try
num = input ( 'enter a value') # 123 124
dic = { '0': ' A '}
Print (DIC [NUM]) # DIC [' 123 124 ']

Print (. 3) # codes from top to bottom,The above will run
1 / int (num) # error does not affect other code error immediately terminate try to indent the code inside
Print (4)

the except Exception AS E: # Exception can capture any exceptions, syntax errors can not capture
print ( 'e:', e )


the next to speak is also understood that part of
a finally:
Print (1)
the try: try #
1/1
the except AS E Exception:
Print ( 'E:', E)
a finally: # ultimate meaning, whether it will print an error report is not talking about tomorrow when processing files with you using
Print (3)

the assert: assertion, the first time no pycharm, that this for debugging, lifetime irrelevant

a = 1
a = 1 +
the Assert a == # 3 conditions are met skips, conditional error will be reported AssertionError error
Print (3)


the raise: take the initiative to throw wrong, to no avail created. framework / language to create C / C ++ Useful

Print (1)
The raise ZeroDivisionError ( 'throwing error actively doing')
Print (2)


The basic document processing
What is a file: the operating system available to users of a unit of virtual 

file what's the use: storing data

open the process file
1) Locate the file path
path = r'D: \ Shanghai Python11 of video \ python11 of video \ day 09 \ test. py '# right click the file, Copy path

2) Double-click to open
f = open (path,' w ') # r -> read read only; w - write> write-only, clear the current file, the file is automatically created
print (f) # file data type

3) See file
data reached, f.read = ()
Print (data)

. 4) to write the file.
f.write ( 'Nick Handsome')

. 5) Close the file
del f # just deleted file references and file in python memory footprint, but did not remove the occupancy of the operating system
f.close () # command will occupy the external memory operating system files


absolute and relative paths
Absolute Path: This type is called an absolute path from the root directory (C / D / E disk) to start 

path = r'D: \ Shanghai Python11 of video \ python11 of video \ day 09 \ test.py '# Right click file, copy path

relative path :( the current folder, you can write directly to the file name)
path = r'test.py '

Why can not they read and write to the file?
Assuming that the file contents 1
1. file can be read and written

2. After an additional 2, file content becomes 12 (time required 10s)

3. 10s reads the paper in which (5S), the content is read out 1

4. append 2 1 behind, file content becomes 12 (time required 10s), and the like 10s had finished file content is actually 12, will be given

 
 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11311337.html