table of Contents
2019/08/06 Learning finishing
Advanced Python
Data type classification
A, distinguished by the number of stored value
The number of storage | type of data |
---|---|
A single value | Numbers, strings |
Multiple values | Lists, tuples, dictionaries, collections |
Second, according to the variable to distinguish immutable
Variable or immutable | type of data |
---|---|
Immutable | Number, a string, a tuple |
variable | Lists, dictionaries, collections |
Third, the order-disorder distinguished by
Ordered or unordered | type of data |
---|---|
Ordered | Strings, lists, tuples |
Disorderly | Dictionaries, collections |
Fourth, according to distinguish the type of access
Access Type | type of data |
---|---|
direct interview | digital |
Sequential access (sequence type) | Strings, lists, tuples |
Access key value (mapping type) | dictionary |
Python copy depth
A copy (direct assignment)
In fact, object reference (alias).
a = [1,2,3]
b = a
print('b=',b)
a.append(4)
print('a=',a)
print('b=',b)
print(id(a))
print(id(b))
b= [1, 2, 3]
a= [1, 2, 3, 4]
b= [1, 2, 3, 4]
1364297998920
1364297998920
Second, shallow copy
Copy of the child object inside the parent object, the object will not be copied.
import copy
a = [1, 2, 3, [4, 5]]
b = copy.copy(a)
print('b=', b)
a.append('hey') # 对被拷贝对象父对象进行添加
a[3].append(6) # 对被拷贝对象子对象a[3]进行添加
print('a=', a)
print('b=', b)
print(id(a))
print(id(b))
b= [1, 2, 3, [4, 5]]
a= [1, 2, 3, [4, 5, 6], 'hey']
b= [1, 2, 3, [4, 5, 6]] # 随子对象改变而改变,父对象不受影响
1974080170568
1974080170824
Third, the deep copy
A complete copy of the parent object and its children.
import copy
a = [1, 2, 3, [4, 5]]
b = copy.deepcopy(a)
print('b=', b)
a.append('hey')
a[3].append(6)
print('a=', a)
print('b=', b)
print(id(a))
print(id(b))
b= [1, 2, 3, [4, 5]]
a= [1, 2, 3, [4, 5, 6], 'hey']
b= [1, 2, 3, [4, 5]]
1916977867208
1916977867464
深拷贝对象不受原对象改变的影响
Exception Handling
First, what is abnormal?
That is an abnormal event, which occurs during program execution, affecting the normal program execution.
Under normal circumstances, when the Python program can not be processed normally occurs an exception.
Python is an exception object representing a mistake.
When an exception occurs we need to capture Python script to handle it, otherwise the program will be terminated.
Second, exception handling
python provides two very important functions to handle exceptions and errors occurring in the python program running. You can use this feature to debug python programs.
- Exception Handling
- Assert (Assertions)
There are various types of abnormalities only to remember everything Exception
s1 = 'hello'
try: # 语法 try: except:
int(s1)
except Exception as e:
print(e)
The basic file operations
First, what is the file?
File is a unit of virtual hard disk read and write the operating system for the user or application provides. File operation is based on operating core file, that file is: read and write.
Second, the operation of the document
read:
# read模式打开文件
f = open(r'/Users/mac/desktop/jupyter/pythonCourseware/32.txt', mode='r')
# 读取文件内容,向操作系统发起读请求,会被操作系统转成具体的硬盘操作,将内容由硬盘读入内存
data = f.read()
print(data)
# 由于Python的垃圾回收机制只回收引用计数为0的变量,但是打开文件还占用操作系统的资源,所以我们需要回收操作系统的资源资源
# del f 只是回收变量f
f.close()
name = '胡歌'
pwd = '123'
write:
# write模式打开文件
f = open(r'/Users/mac/desktop/jupyter/pythonCourseware/32.txt', mode='w')
f.write("""name = '胡歌'
pwd = '123'""")
f.close()
f = open(r'/Users/mac/desktop/jupyter/pythonCourseware/32.txt', mode='r')
data = f.read()
print(data)
name = '胡歌'
pwd = '123'
Absolute and relative paths
First, the absolute path
As the name expressed, it is absolute.
Is the start position until the file from the root directory
C:/user/hp/test.txt
Second, the relative path
Relative to the current directory to the program file location
./test.txt