- list, tuple operations
- String manipulation
- Dictionary operations
- Collection operations
- File operations
- Character encoding and transcoding
list, tuple operations
Slice
>>> names = [the test","Tenglan","Eric","Rain","Tom","Amy"]>>> names[1:4] #取下标1至下标4之间的数字, including 1, not including 4['Tenglan','Eric','Rain']>>> names[1:-1]the value of #取下标1至-1, excluding -1['Tenglan','Eric','Rain','Tom']>>> names[0:3] ['Test','Tenglan','Eric']>>> names[:3] #如果是从头开始取, 0 can be ignored, the same as the effect of the sentence ['Test','Tenglan','Eric']>>> names[3:]#如果想取最后一个, must not write-1 , only to write like this ['Rain','Tom','Amy'] >>> names[3:-1]#这样- 1 would not have been included ['Rain','Tom']>>> names[0::2] #后面的2是代表, every other element, take a ['Test','Eric','Tom'] >>> names[::2] #和上句效果一样['Test','Eric','Tom']
Append Operation--append
Insert operation
Modify Operation
Delete operation
Extended operations
Copy operation
Statistical operations
Sorting and reversing operations
Get Index value action
Tuple operations
string Manipulation
The Format function and the Join function
Replace function
Dictionary Operations
Add action
Modify Operation
Delete operation
Find operations
Other operations
Cycle
Collection
A collection is an unordered, non-repeating combination of data, and its main functions are as follows:
Go to the weight, turn a list into a set, and then automatically go heavy.
Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
Example
L1 = [1,2,5,3,6,7,9]l2= [2,3,5,8,9]①, set a collection list_1=Set(L1) # # #设置一个集合print (Type (list_1)) # # #打印类型, prints the result for the collection : {1,2,3,5,6,7,9} <class 'Set'>②, intersection/set/subtraction/ symmetric differential set # # #交集 ( & )c=l1.intersection (L2) #print (C,type (c))# # #并集 ( | )D=l1.union (L2) print (D,type (d))# # #差集 ( - )e=L1.difference (L2) # # #l1里面有, but there is no print (e) F in L2=l2.difference (L1) # # #l2里面有, but the L1 does not have print (f)# # # #子集G=L1.issubset (L2) print (g) # # #返回false, where L1 is not a subset of L2 # # # #父集H=L1.issuperset (L2) Print (h) # # # #返回fasle, where L1 is not the parent set of L2 # # #对称差集 (Take out the two collections without each other) (^) I=l1.symmetric_difference (L2) print (i)③, add and Revise check : L1.add ('x') # # #添加一项 L1.update (1,2,3) # # #添加多项删除: Pop # # #删除一个元素并打印删除的返回值 Remove # # # #删除指定的元素
File operations
Procedure for file operation
1. Open the file, get the file handle and assign a value to a variable
2. Manipulating files with a handle
3. Close the file
f = open ("AAAA", encoding="Utf-8") # Open File first_line=F.readline () print ('First line :', First_line) # Read a line of print ('I'm the dividing line .'. Center ( -,'-')) #打印结果----------------------I'm the dividing line-----------------------Data=F.read () # Read All the rest of the content, do not print the file when the file is large (data) all_lines >=F.readlines () print ("All_line:", All_lines) # # #读取所有行f.close () # Close file #打印前5行 forIinchRange5): Print (F.readline ())
# # # #一行一行的读取 (recommended, more efficient)F= Open ("AAAA") # Open File count=0 forLineinchF:ifCount = =2: Print ("Llall") Count+=1 Continueprint (line) Count+=1 Shorthand:count=0 forLineinchF:print (line) Count+=1 ifCount = =2: Print ("Llall") Continue # # # #一次性读取所有文件内容Print the first 10 lines, if you print to the 6th line skip (read all to memory directly, encountered large files are not suitable): forIndex,valueinchEnumerate (F.readlines ()): # # # #enumerate枚举, subscript starting from 0, ifindex = =2: Print ("DAF" # # # #此处可不写, just jump out of the loop and start again Continue
encoding and decoding
1. In Python2 (first turn to Unicode, first decode into Unicode in encode into GBK or utf-8) The default encoding is ASCII, python3 default is Unicode bytes encoded by decode into Chinese string
2.unicode is divided into utf-32 (4 bytes), utf-16 (accounting for two bytes), Utf-8 (1-4 bytes), so utf-16 is now the most commonly used Unicode version, but in the file is still utf-8, because the UTF8 save space
3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string
Example:
Import Sysprint (sys.getdefaultencoding ()) MSG="I love Beijing Tian ' an gate"# #此时为unicodemsg_gb2312= Msg.encode (encoding="gb2312") # # #此时只能encodegb2312_to_gbk= Msg_gb2312.decode ("GBK"). Encode ("GBK") printing (msg) print (msg_gb2312) prints (GB2312_TO_GBK) results: UTF-8I love Beijing Tian ' an gate b'\xce\xd2\xb0\xae\xb1\xb1\xbe\xa9\xcc\xec\xb0\xb2\xc3\xc5'b'\xce\xd2\xb0\xae\xb1\xb1\xbe\xa9\xcc\xec\xb0\xb2\xc3\xc5'all transcoding must first be converted to Unicode in the form "UTF" from encode-8"or" gb-2312”
Python Foundation One