1. Collection Operations
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
s = set ([3,5,9,10]) #创建一个数值集合
t = Set ("Hello") #创建一个唯一字符的集合
A = T | The set of S # T and S
b = Intersection of T & S # T and S
c = t–s # differential Set (item in T, but not in s)
D = t ^ S # symmetric difference set (items in t or S, but not both)
Basic operation:
T.add (' x ') # Add an item
S.update ([10,37,42]) # Add multiple items in S
Use Remove () to delete an item:
T.remove (' H ')
Len (s)
The length of the set
X in S
Test if X is a member of S
X not in S
Test if X is not a member of S
S.issubset (t)
S <= t
Test if every element in S is in t
S.issuperset (t)
S >= t
Tests if every element in T is in S
S.union (t)
s | T
Returns a new set containing each element in S and T
S.intersection (t)
S & T
Returns a new set containing the common elements in S and T
S.difference (t)
S-t
Returns a new set containing elements in s but not in t
S.symmetric_difference (t)
s ^ t
Returns a new set containing elements that are not duplicates in S and T
S.copy ()
Returns a shallow copy of the set "s"
2.
file Operations
Procedure for file operation
- Open the file, get the file handle and assign a value to a variable
- Manipulating a file with a handle
- Close File
The mode of opening the file is:
- R, read-only mode (default).
- W, write-only mode. "unreadable; not exist; create; delete content;"
- A, append mode. "Readable; not exist" create; "only append content;"
"+" means you can read and write a file at the same time
- r+, can read and write files. "readable; writable; can be added"
- w+, write and read
- A +, with a
"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading
"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)
File modification
f = open ("Yesterday2.txt", "R", encoding= "Utf-8")
f_new = open ("Yesterday2_bak.txt", "W", encoding= "Utf-8")
For line in F:
If "Dick and Harry" in line:
line = Line.replace ("Dick and Harry", "Zhangsanlisi")
F_new.write (line)
F.close ()
F_new.close ()
File read/write
f = open ("Yesterday.txt", ' r+ ', encoding= "Utf-8") #文件句柄 Encoding=utf-8
#a = Append append cannot be read, append does not overwrite W overlay
data = F.read ()
Print (data)
#f. Write ("I love Beijing Tian ' an door \ n")
#f. Write ("Taiyanghong")
#print (f)
#low的循环
‘‘‘
For Index,line in Enumerate (F.readlines ()):
If index = = 9:
Print ("--------fengexian------")
Continue
Print (Line.strip ())
‘‘‘
#hight
For line in F:
Print (line)
Print (F.tell ()) #打印光标位置
F.seek (#光标重新定位)
3. Character encoding and transcoding
Detailed article: http://www.cnblogs.com/luotianshuai/articles/5735051.html
4. Function parameters and Local variables
#列表 Dictionary collection classes can be changed in local variables
Not #字符串 integer.
School = "Old boy"
def chang_name (name):
Global School
School = "Qinghua"
Print ("Before Chang", name)
Name = "Alex li" #这个函数就是这个变量的作用域
Print ("After Chang", name)
Name = "Alex"
Chang_name (name)
Print (name)
Print ("School:", school)
---------------------------
Import time
Def logger_test ():
Time_format = "%y-%m-%d%x"
Time_current = Time.strftime (Time_format)
With open (' a.txt ', ' A + ') as F:
F.write ("%s End action \ n"%time_current)
def test1 ():
Print ("Test1 starting action")
Logger_test ()
Def test2 ():
Print ("Test2 starting action")
Logger_test ()
def test3 ():
Print ("Test3 starting action")
Logger_test ()
Test1 ()
Test2 ()
Test3 ()
def test4 (x, Y, z):
Print (x)
Print (y)
Print (z)
Test4 (y=2,x=1,z=6)
Test4 (4,5,9)
Test4 (4,z=3,y=8)
def test5 (x,y=2):
Print (x)
Print (y)
TEST5 (1,3)
#默认参数特点: When calling a function, the default function must not pass
#用途: 1, default installation value
# 2, default value
#接收多个参数组成元组
#*args: Accept n Positional parameters, convert Narimoto Group
def test6 (X,*args):
Print (x)
Print (args)
Test6 (1,2,3,45,66,8)
---------------------------
#吧N个关键字参数写成字典, receive Key:value
def test (**kargs):
Print (Kargs)
Print (kargs[' name '])
Print (kargs[' age ')
Test (name= "Alex", age= "8", sex= "F")
Test (**{' name ': ' Alex ', ' age ': ' 8 '})
def test2 (Name,**kwargs):
Print (name)
Print (Kwargs)
Test2 (' Alex ', age=18,sex= ' F ')
def test3 (Name,*args,**kwargs):
Print (name)
Print (args)
Print (Kwargs)
Test3 (' Alex ', 2,3,4,5,age= ', sex= ' M ')
#位置参数不能放在关键字参数后
5. Recursion
Inside a function, you can call other functions. If a function calls itself internally, the function is a recursive function.
Def calc (n):
Print (n)
if int (N/2) > 0:
Return calc (int (N/2))
Print ("---", N)
Calc (10)
Learning Python:day3