1. Built-in functions
# ### built-in functions # ABS absolute value function RES = ABS (-10 ) Print (RES) # Round rounded (n.5 n is an even number rounding n.5 n is odd, into a!) Does not even enter into odd RES = round (13.56 ) res = round(4.5) res = round(5.5) res = round(4.53) res = round(4.9) print(res) # SUM is calculated and a sequence obtained TUP = (1,2,3,43,5,6,6 ) res = sum(tup) print(res) # Max obtaining a maximum sequence inside TUP = (1,2,3,43,5,6,6 ) res = max(tup) print(res) # Max higher order function using DEF FUNC (n-): # Print (n-) # is a parameter tuple ( "Lin Minghui ', 58) return n-[. 1] # 33 is 58 99 -8 LST = [( " Chang far " , 33), ( " Lin Minghui " , 58), ( " Lide Liang " , 99), ( " Selena " , -8 )] res = max(lst,key=func) print(res) # Min obtain a minimum sequence inside TUP = (1,2,3,43,5,6,6 ) res = min(tup) print(res) # Min higher order function using DIC = { " Renpeng Wei " : 60, " were Yong-Ling " : 59, " Huang Lexi " : 90, " Selena " : -7 } DEF FUNC (n-): # Print (n-) # Parameter a dictionary key return DIC [n-] # returns the key values, sorted by value, find the minimum value corresponding key RES = min (DIC, key = FUNC) Print (RES) # POW calculating a power value x RES = POW (2,3 ) "" " value before and two operands in the third number of modulo " "" RES = POW (2,3,3 ) # POW = RES (2,3,4) Print (RES) # Range produce a specified range data iterable for I in Range (. 3 ): Print (I) for i in range(1,5): print(i) for i in range(1,10,3): print(i) # Bin 10 the binary data into binary RES = bin (255 ) Print (RES) # OCT binary data is converted into 10 octets RES = OCT (255 ) Print (RES) # Hex to decimal data into hexadecimal RES = hex (255 ) Print (RES) # CHR convert the ASCII code characters RES = CHR (97 ) Print (RES) # the ord converted to ASCII coded characters RES = the ord ( " A " ) Print (RES) # Eval string as python code execution strvar = " Print ( 'I'm Marshal pot') " Print (strvar) eval(strvar) # Eval has its limitations, can not create variables # strvar = "A = 5" # eval (strvar) # Exec string as python code execution (more powerful) used with caution, there are security risks strvar = " A = ' brother Man Zhenshuai! ' " Exec (strvar) Print (A) """ # Review name = Global () print (say) dic [ "wangwen"] = "first man of the universe." print (wangwen) """ strvar = """ for i in range(5): print(i) """ exec(strvar) # Repr not escape character output string strvar = " D: \ NABC " RES = repr (strvar) Print (RES) # The INPUT accepts input string # name = the INPUT ( "first Mori, you mother name?") # Print (name) # Hash hash value is generated . "" " (1) can be encrypted password (2) may be a hash value verification document "" " # Same string, no matter how many times the hash are the same hash value strvar1 = " abc " strvar2 = " abc " res1 = hash (strvar1) res2 = hash(strvar2) print(res1,res2) with open("ceshi1.txt",mode="r+",encoding="utf-8") as fp1 , open("ceshi2.txt",mode="r+",encoding="utf-8") as fp2: res = fp1.read() res2 = hash(res) res = fp2.read() RES3 = the hash (RES) IF RES2 == RES3: Print ( " the same for both file contents " ) the else : Print ( " two files are not the same " )
2. pickle serialization module
# ### pickle serialization module . "" " Serialization: the data can not be stored directly to storage file becomes available, the procedure is serialized Deserialize: the stored data out, return to the original data type, this process is deserialization php: (understand) serialize serialization unserialize deserialization pickle can serialize all data types "" " Import pickle # file which can only store a string or binary byte stream, the other not ." "" lst = [1,2,34,5] with open("ceshi.txt",mode="w",encoding="utf-8") as fp: fp.write(lst) """ # (1)容器类型数据可以序列化 lst = [1,2,34,5] #dumps 把任意对象序列化成一个bytes res = pickle.dumps(lst) print(res) #loads 把任意bytes反序列化成原来数据 lst = pickle.loads(res) print(lst,type(lst)) # (2)函数可以序列化 def func(): print("我是函数func") #dumps 把任意对象序列化成一个bytes res = pickle.dumps(func) print(res) #loads 把任意bytes反序列化成原来数据 func = pickle.loads(res) print(func,type(func)) func() # (3)迭代器可以序列化 it = iter(range(5)) # 序列化迭代器 res = pickle.dumps(it) # 反序列化恢复原来的数据类型 it2 = pickle.loads(res) print(it2) from collections import Iterator,Iterable res = isinstance(it2,Iterator) print(res) # 获取迭代器中的数据 for i in range(3): res = next(it2) print(res) for i in it2: print(i) # 方法一 #dump 把对象序列化后写入到file-like Object(即文件对象) lst = [1,2,3] with open("ceshi.txt",mode="wb") as fp: pickle.dump(lst,fp) #load 把file-like Object(即文件对象)中的内容拿出来,反序列化成原来数据 with open("ceshi.txt",mode="rb") as fp: res = pickle.load(fp) print(res,type(res)) # 方法二 # 用dumps 和 loads 对数据进行存储 lst = [1,2,3] # 写入 with open("ceshi4.txt",mode="wb") as fp: res = pickle.dumps(lst) fp.write(res) # 读取 with open("ceshi4.txt",mode="rb") as fp: res = fp.read() lst = pickle.loads(res) print(lst)
3. math 数学模块
day14