Data type classification
Distinguished by the number of stored-value
Single value : number, string
A plurality of values (containers) : a list of tuples, dictionaries, set
Distinguished by the variable immutable
Variable : lists, dictionaries, collections
Unchangeable : numeric, string, a tuple
Order-disorder distinguish
Ordered (index value may be) : strings, lists, tuples
Disorder : dictionaries, collections
Python copy depth
copy:
When the copy of the object is lt2 lt variably changes within lt type, lt2 change; a change in the type immutable lt, lt2 change. (Simple assignment)
Shallow copy:
When shallow copy of the object lt2 lt variably changes within lt type, lt2 change; a change in the type immutable lt, lt2 is not changed. Built-method type copy.copy () variable data in .copy ()]
Deep copy:
When the deep copy of the object lt2 lt variably changes within lt type, lt2 not change; a change in the type immutable lt, lt2 unchanged. [Copy.deepcopy ()]
keep in mind:
Copy / Copy shallow / deep copy only for variable data type
Exception Handling
What anomaly?
An exception is the error signal generation program is running (when the program error occurs, an exception, if the program does not handle it, it will throw the exception, run the program also will be terminated)
General exceptions have syntax errors and logic errors .
Exception Handling
① If an error condition is predictable, we can deal with if: prevention before the error occurred
② If the condition is unpredictable errors occur, you need to use try ... except: processing after an error
#基本语法为
try:
被检测的代码块
except 异常类型:
try中一旦检测到异常,就执行这个位置的逻辑
In the try ... except if you want the effect, no matter what type of exception occurs, we unified discarded, or use the same code logic to deal with them, then you can use universal abnormal Exception.
In the try ... except if you want to effect that, for different exception we need to customize different processing logic, it would need to use a multi-branch.
Note :
- Syntax errors can not be captured with a try ... except
- Logical errors can be captured with a try ... except
For additional exception handling :
Thrown raise (basically useless)
Assert assert (basically useless)
Basic file operations
What is a file?
File is a unit of virtual hard disk read and write the operating system for the user or application provides.
Open the process file
- Locate the file path
path = r'D:\上海Python11期视频\python11期视频\day 09\test.py' # 鼠标右键点击文件,copy path
- Double-click to open
f = open(path, 'w') # r-->read 只读 ; w-->只写,清空当前文件后写入
print(f) # 文件数据类型
- See the file
data = f.read()
print(data)
- Write files.
f.write('nick handsome')
- Close the file
del f # 只删除了文件的引用以及文件在python内存中的占用,但是没有删除对操作系统的占用
f.close() # 关闭操作系统对文件的占用
Absolute and relative paths
Absolute path : From the letter (C:, D :) began to write a full path.
Relative path : relative to the currently executing file folder where to start looking.