December 30, 2018
A collection is a collection of elements that are not repeated in order. Basic functions include relationship testing and de duplication.
Create collection: braces or set() Function can be used to create a collection. Note: to create an empty set, you must use set() instead of {}, which is used to create an empty dictionary. Braces also do not create collections of elements that contain dictionaries and lists.
1 #Operations on a collection 2 set1 = {1,2,3,5,6} 3 set1.add(8) #Add an element to the collection 4 set1.clear() #Empty collection 5 set1.copy() #Shallow copy 6 set1.discard() #No error is reported when deleting an element that does not exist 7 set1.pop() #Randomly delete an element and return 8 set1.remove(5) #Delete an element without error 9 set1.update([1,5,8]) #Add multiple elements 10 #If there are two elements 11 set2 = {12,4,7,8} 12 set1.difference(set2) or set1 - set2#The difference set of set1 and set2 13 set1.intersection(set2) or set1 & set2 #Intersection of set1 and set2 14 set1.isdisjoint(set2) #Judge whether set1 and set2 intersect. If not, return True. If yes, return False 15 set1.issubset(set2) #Determine whether set1 is a subset of set2 16 set1.issuperset(set2) #Determine whether set1 is the parent set of set2 17 set1.symmetric_difference(set2) or set1 ^ set2 #The symmetric difference set of set1 and set2 18 set1.union(set2) or set1 | set2 #Union of set1 and set2
II. Document operation
1. Open the file to get the file handle
2. Operate the file through the file handle
3. Close file
1 f = open("1.txt","r",encoding='utf-8') #File handle opens a file in read-only mode 2 f = open("1.txt","w",encoding='utf-8') #Open the file in write only mode, and a new file will be created 3 f = open("1.txt","a",encoding='utf-8') #Open in append mode, write only, not read 4 5 f = open("1.txt","rb") #Open in binary mode 6 f = open("1.txt","r+",encoding='utf-8')#Open write in read-write mode can only append write 7 f= open("1.txt","w+",encoding='utf-8') #Open in read-write mode, write before read 8 f = open('1.txt',"a+",encoding='utf-8') #Append read / write mode on 9 f.close() #Close file 10 f.encoding #View the encoding of the file 11 f.encoding #View the encoding of the file 12 f.flush() #Refresh cache 13 f.mode #File open mode 14 f.name #file name 15 f.readline() #Read line by line 16 f.read() #Read all the contents of the file read f.read(n) read n characters of the file 17 f.readlines() #Read everything and return a list 18 f.readable() #Determine whether the file is readable 19 f.seek(10) #Move the cursor to the tenth position 20 f.tell() #Get the current cursor position 21 f.truncate(10) #Ten characters from start 22 f.write() #Write contents 23 f.writable() #Is the file writable 24 for line in f: 25 print(line) #Traverse file and read 26 27 with open("1.txt","r",encoding='utf-8') as f1,\ 28 open("2.txt","w",encoding='utf-8') as f2:
Document code
1. The default file code in Python 3 is utf-8, so you can write Chinese directly
2. The declared variable is unicode by default, so to convert it to GBK, encode('gbk ') directly
3. When encoding, str type data will also become bytes type data
4. How to transfer from UTF-8 to gbk? First, decode to unicode, then encode to gbk