1. Collection
The set mainly carries out the heavy and relational operations on the elements inside, and the permutation of elements is unordered.
A. Operation of the collection
############ #去重
Set1 = {1,1,2,2,3,4,4,5}
Print (Set1) #输出set1 = {1,2,3,4,5}
############# #增加值
Set1 = {1,1,2,2,3,4,4,5}
Set1.add (6)
Print (Set1) #输出set1 = {1,2,3,4,5,6}
Set1.update (' abc ')
Print (Set1) #输出set1 = {1,2,3,4,5,6, ' a ', ' B ', ' C '}
############## #删除
Set1.pop () #随机删除
Set1.remove (' A ') # is deleted according to the element, if no this element will error
Set1.clear () #清空集合
Del Set1 #删除集合
################ #查询
Query with for loop because the elements of the collection are unordered
B. Relational operations for sets
Set1 = {1,2,3,4,5} Set2 = {1,2,3,7,8}
Print (Set1 & Set2) #交集. Output {1, 2, 3}
Print (Set1 | set2) #并集. Output {1, 2, 3, 4, 5, 7, 8}
Print (Set1-set2) #差集. Output {4, 5}
Print (SET2-SET1) #差集. Output {7, 8}
Print (set1 ^ set2) #反交集. Output {4, 5, 7, 8}
Print (Set1 < Set2) #子集. Output false
Print (Set2 > Set1) #超集. Output false
2. Depth copy
##### #浅copy The first layer is independent, starting with the second layer, sharing a memory address
S1 = [1, 2, 3,[11,22]]
S2 = s1.copy ()
S1[-1].append (666)
Print (s1, S2) #s1和s2一样的值
Print (ID (s1), ID (S2)) #s1和s2的内存id不同
Print (ID (s1[-1]), ID (s2[-1])) #内层列表的id值是一样的
Deep copy no matter how many layers, are independent of each other
Import Copy
S1 = [1, 2, 3,[11,22]]
S2 = copy.deepcopy (S1)
S1.append (4)
Print (s1, S2) #只有s1增加了4, S2 does not increase by 4, indicating that the first layer is not the same memory address
S1[-1].append (#在内层的列表增加值33)
Print (s1, S2) #只有s1内层列表增加了33, S2 does not increase by 33, indicating that the inner layer is not the same memory address
3. File operation
Execution flow of file operations: Open file, generate file handle (F1)---manipulate file handle (open ())---Close file handle (F1.close ())
####### #文件的读
R mode:
F1 = open (' 123.txt ', encoding= ' UTF8 ', mode= ' R ') #123. txt to present the current directory, or it will error
Print (F1.read ()) #打印读取的内容
F1.close () #关闭文件句柄
F1.read (n) #按照字符读取, n represents n characters
F1.readline () #按行读取
F1.readlines () #返回列表 that contains all the rows
RB mode (read non-literal Class):
F1 = open (' 1.jpg ', mode= ' RB ') #1. jpg to present the current directory, or it will be an error
Print (F1.read ())
F1.close ()
F1.read (n) #按照字节读取, n represents n bytes
For loop mode: One cycle consumes a piece of memory and is released after the loop.
r+ mode and R+b mode are similar to the above modes.
############# #文件的写
F1 = open (' 456.txt ', encoding= ' utf-8 ', mode= ' W ') #没有文件, create a new file to write to, have the original file, empty the content, and then write the new content
Print (F1.write (' 456 '))
F1.close ()
WB mode (write to non-literal class)
F2 = open (' 2.jpg ', mode= ' WB ')
F2.write (F1.read ()) #f1. Read () reads a picture content
F2.close ()
w+ mode and W+b mode are similar to the above modes.
############## #文件的追加
F1 = open (' 456.txt ', encoding= ' utf-8 ', mode= ' a ') #没有文件, new file write content, original file, add on base of source file
Print (F1.write (' 789 '))
F1.close ()
AB Mode A + mode a+b mode is similar to the above mode
######## #文件的其它常用操作
F1.seek () #调整光标的位置
F1.tell () # Tell the cursor where
With open (' 123.txt ', encoding= ' utf-8 ') as F1 #不用主动关闭文件句柄
4. Primary knowledge function
Function: function-oriented to solve the problem of multiple readability of repeated code
Definition of the function:
def keyword function name ():
function body
Function execution: function name ()
Return of the function: terminates the function or returns a value to the performer of the function.
Parameters of the function:
######## #实参角度
A. Position parameters correspond to order one by one
Def func1 (A, B, c):
Print (A, B, c)
Func1 (1, 2, 3) #a =1 b=2 c=3
B. keyword parameter one by one correspondence
Def func1 (A, B, c):
Print (A, B, c)
Func1 (C=1,b=2,a=3)
C. Mixed parameters (including positional and keyword parameters), the keyword parameter must be behind the position parameter
Def func1 (A, B, c):
Print (A, B, c)
Func1 (1,2,c=3)
########### #形参角度
A. Position parameters correspond to order one by one
Def func1 (A, B, c):
Print (A, B, c)
Func1 (1, 2, 3) #a =1 b=2 c=3
B. Default parameter, the default parameter is after the position parameter.
Def func1 (A, B, c=3):
Print (A, B, c)
Func1 (ON)
############ #动态参数 args: All positional parameters are placed in a tuple. Kwargs: All the keyword parameters, put in a dictionary
def func1 (*args, Kwargs): #函数的定义的时候represents aggregation
Print (args)
Print (Kwargs)
Func1 ([1, 2, 3],{' name ': ' CC ', ' Age ': '} ') #函数的执行的时候representative
Order of formal parameters: Positional parameter--args--default parameter--**kwargs
Python's third day study summary