Set (unordered, non-repeating sequence)
Create two ways
Example one: S1 = {£ º}
Example two: S2 = set (), s3 = Set ([1,2,3,4,5])
Function
S2.add (123) #添加s2集合中123元素
S2.clear () #清除内容
S2.copy () #浅拷贝
S1.difference.s2 #获取s1中存在 not present in the S2
S1.DIFFERENCE_UPDATE.S2 # Remove the same element from the current collection as in B
S1.discard (' AAA ') #移除指定元素, there is no guarantee of error
S1.intersection (S2) #找到s1与s2的交集
S1.intersection_update (S2) #取交集并更新到当前集合中 "S1"
S1.isdisjoint (S2) #如果没有交集, returns True, otherwise false
S1.issubset (S2) #判断是否是子子集合, S1 completely covers S2 then S2 is a subset of S1
S1.issuperset (S2) #判断是否是父集合, S2 completely forgive S1 then S2 is the parent collection of S1
S1.pop () #随机删除元素
S1.remove (1) #删除指定元素, the element does not exist will be an error
S1.union (S2) #求s1与s2的并集
function a basic mechanism
1. def keyword, create function
2. Name of function
3, () fixed collocation
4. Function body
5. Return value
Note:
Once the function is return, the function execution process terminates immediately.
If there is no return in the function, the default equals none
When a function passes a parameter, it passes a reference to the variable
Two-parameter concept
1, def sendmail (xx) #xx form parameters
2, ret = SendMail (' abc ') #abc actual parameters
3, def sendmail (xx,ss = ' BB '): #默认参数, the default SS=BB call function can not pass the actual parameters 默认参数必须在参数的最后
4, ret = SendMail (ss=1,xx=2) #指定参数
5. Dynamic Parameters
def f1 (*args): #此方法定义形参, args will inevitably become a tuple, and all parameters received will be added to this tuple example 1:ret = def (' abc ', ' AAA ') # will add both ABC and AAA to the formal parameter, becoming a tuple 2:ret = def ([1,2,3,4,]) #会讲列表当成一个元素传到形参中, inclusions in tuples such as: ([1,2,3,4]) Example 3:ret = Def (*[1,2,3,4,]) #会将列表迭代循环, respectively written to tuple XX: (1,2,3,4)
def f1 (**kwargs): Example 1:ret = def (' A ' = 1, ' b ' = 2) # will add both ABC and AAA to the formal parameter, become a tuple example 2:ret = def (**{' A ': 1, ' B ': 2}) #会将字典循环迭代, assign to a dictionary such as: K Wargs ={' A ': 1, ' B ': 2} example 3:ret = def (key = {' A ': 1, ' B ': 2}) #会将字典当作value match key into a health value pair assignment to Kwargs
6. Universal Parameters
Def f1 (args,*Kwargs) # is a parameter that can receive a single element added to a tuple, or it can receive a specified argument added to the dictionary
Note: The default actual parameter assignment order is the same as the formal parameter
Note:
Once the function is return, the function execution process terminates immediately.
If there is no return in the function, the default equals none
Three global variables (all scopes are readable, defining global variables must use uppercase)
Global variables are defined outside the function
Global variables can be modified in the function
For special variables: list, dictionary, can be modified not re-assigned value
Built-in functions
n = ABS ( -1) #获取绝对值
n = All ([1,2,3,4]) #判断所有为真才为真
n = All ([1,0,[],{}]) #判断只要有真就为真
Bin (5) #十进制转换二进制
Oct (5) #十进制转换八进制
Hex (5) #十进制转换十六进制
s = ' abc '
Bytes (s, encoding= "Utf-8") #字符串转换成字节类型, define the encoding type in the parameter
t = bytes (s, encoding= "Utf-8")
STR (t, encoding= "Utf-8") #字节类型转换成字符串
Decorative Device
Three mesh operation
If1== 1:
Name ="Zhangshaoxiong"
Else:
Name ="Zshaox"
Name ="Zhangshaoxiong"if1== 1Elsename = =' Zshaox '
(A1): A1 + f1 () F2 = a1:a1 + F2 ()
File operations
withOpen(' Test.txt ',' r+ ') asF,Open(' Test2.txt ',' r+ ') asF2:
times = 0
for line in f:
times += 1
f2.write(line)
R Read File contents
W Write file contents, first case original file
X when there is an error in the file, there is no content created and written
A Append file contents
RB open read file in binary form
WB writes files in binary open, combined with bytes ("str", encoding= "Utf-8")
Write to the end of the r+ file
F.write () Write file
F.read () reads the file by default reads all files, if the parameter reads 1 pointers (character, byte) position according to open mode (b)
F.close () Close Open file
F.flush () force writes the buffer file to disk
F.readline () Read only the first row
F.truncate () Truncate the file, emptying all data after the pointer position
For loop File Object
For line in F:
Print (line)
F.seek (1) The pointer is adjusted to the first position by the byte count
Print (F.tell ()) calculates the position of the current pointer by byte
This article from "Good memory than bad writing" blog, please be sure to keep this source http://zhangshaoxiong.blog.51cto.com/4408282/1783584
Python Learning Day3