The module is essentially a. py file, which you can see under the Lib folder in the installation directory
Modules are divided into three parts: built-in modules (in the interpreter), third-party modules (under Lib folder), custom modules (defined by themselves)
1.time Module
Import time# Returns the timestamp of the current time print (Time.time ()) #1498027773.1063557# takes a timestamp as a parameter, returns a structured time tuple, and the parameter defaults to the current time print (Time.localtime ( 1912412470)) #time. Struct_time (tm_year=2030, tm_mon=8, Tm_mday=8, tm_hour=17, tm_min=41, tm_sec=10, tm_wday=3, Tm_yday =220, tm_isdst=0) #以时间戳为参数, returns the structured GMT, the default parameter is the current time print (Time.gmtime ()) #time. Struct_time (tm_year=2017, tm_mon= 6, tm_mday=21, tm_hour=6, tm_min=49, tm_sec=2, tm_wday=2, tm_yday=172, tm_isdst=0) #结构化时间转化成字符串时间print (Time.strftime (' %y:%m:%d-%x ', Time.localtime ())) #2017:06:21-14:48:50# string time into formatted time print (Time.strptime (' 1993-10-15 ', '%y-%m- %d ') #time. Struct_time (tm_year=1993, tm_mon=10, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday= 288, Tm_isdst=-1) #结构化时间转换成时间戳print (Time.mktime (Time.localtime ())) #1498027859.0print (Time.asctime ()) #Wed June 14:53:11 2017print (Time.ctime ()) #Wed June 21 14:53:11 2017# thread delays the specified time, the parameter is the specified number of seconds Time.sleep (5)
Conversion of several time forms:
2.random Module
Floating point 0.40165771797504246print (Random.randint (1,3)) between import Randomprint (Random.random ()) #随机返回一个 (0,1) # Randomly returns a specified range [1,3] int type 3print (Random.randrange (1,3)) #随机返回一个指定范围 [1,3] int type 1print (Random.choice ([ 12,21,23,34, ' A ', ' B ']) #随机返回一个指定列表中的元素 12print (Random.sample ([12,21,23,34, ' A ', ' B '],3)) #随机返回指定个数的指定列表中的元素 [' A ', +, ' B ']print (Random.uniform (1,3)) #随机返回一个 (1,3) floating-point number 2.2162483038520144l=[1,2,3,4,5,6] Random.shuffle (L) #把指定列表的顺序打乱print (L) #[1, 3, 6, 2, 4, 5]
Practice
#练习, randomly generate a 5-bit verification code, including numbers, uppercase and lowercase letters import randomres= ' for I in Range (5): num=random.randint (0,9) ALPA=CHR ( Random.randint (65,90)) alpa=chr (Random.randint (97,122)) s=random.choice ([str (num), Alpa,alpa]) Res=res+sprint (RES)
3.os Module
1 Import OS 2 3 #获取当前工作目录 4 print (OS.GETCWD ()) #C: \untitled\0621day12 5 6 #改变当前工作目录, equivalent to CD 7 Os.chdir (File_path) in the shell 8 9 #生成多层递归目录10 os.makedirs (' dirname1/dirname2 ') #递归删除空目录13 os.removedirs (path) #生成单级目录16 os.mkdir (' dirname ') #删除单级空目录, the directory is not empty can not delete the Os.rmdir (path) #列出指定目录下的所有文件和子目录, including hidden files, printed in a list print (Os.listdir (R ' C:\untitled\ 0612Python Fifth day ')) #[' 0612 jobs. Py ', ' a.txt ', ' Job title ', ' character encoding. Py ', ' file operation. py ']23 #删除一个文件25 os.remove () #重命名文件/catalogue Os.ren Ame (' oldname ', ' newname ') #获取文件/directory information Os.stat (path) print (Os.stat (R ' C:\untitled\0612Python fifth Day ')) #os. Stat_ Result (st_mode=16895, st_ino=2814749767244350, st_dev=3504670893, St_nlink=1, St_uid=0, St_gid=0, st_size=4096, St_ atime=1497356489, st_mtime=1497356489, st_ctime=1497227714) #返回文件目录, is actually the first element of Os.path.split (path) 35 Os.path.dirname (Path) #返回文件的文件名, is actually the second element of Os.path.split (path), Os.path.basename (path) #如果path以/or \ End, will return a null value 39 #如果文件存在, returns True if the False41 os.path.exists (path) #如果path是绝对路径 is returned, and the TRue44 os.path.isabs (path) #将多个路径组合后返回, parameters before the first absolute path will be ignored Os.path.join (path,paths) 48 49 # Returns the last access time of the file or directory that the path points to (path) Os.path.getatimme #返回path所指向的文件或目录的最后修改时间53 os.path.getmtime (path) 54 55 # Returns the size of path, os.path.getsize (path)
4.hashlib
The Hashlib module provides a common digest algorithm, such as MD5,SHA1, in Python3, where the hash directly replaces the MD5 and SHA
Abstract the algorithm is a function, the arbitrary length of the data into a fixed length of the data string.
1. Abstract the algorithm is irreversible and cannot be reversed by the data string to get the meta-data
2. Two data, even if only 1bit difference, get the summary is not any relationship
3. Regardless of the size of the metadata, the resulting digest is fixed-length
4. When the metadata is large, you need to read one line at a time, and then one line of validation, and the last. Hexdigest () can get a summary of all previous results
MD5 is very fast and very common, and the resulting result is a fixed 128bit usually represented by a 32-bit 16 binary string.
Import hashlibmd5=hashlib.md5 () md5.update (' Aasg '. Encode (' Utf-8 ')) #434c591c9c5a04e44ec8dbafe81058e7md5. Update (' ADASDG '. Encode (' Utf-8 ')) #d20a55b2194493fdeb66e2cd358dab15md5. Update (' AASGADASDG '. Encode (' UTF8 ')) # D20a55b2194493fdeb66e2cd358dab15print (Md5.hexdigest ())
Not to be continued ....