Module:
A module is a file that contains the Python definition and declaration, and the filename is the suffix of the module name plus the. Py.
To be blunt, it is a Python file that defines classes and methods, implements functions that can be called by other Python files
So how do you use a module?
Of course, using the Import module name is the way to load a module.
If you want to use a class in a module, it is:
Module name Little Class
So what if the method in the module doesn't know what to do with it?
Two methods:
1.help (module name. Class)
2.Ctrl + Left button click Class
After pulling a bunch of very useful information, let's talk about what the usual modules are all about.
Time Module
You can see from the name that this module is related to time, yes, all the classes and methods in this module are time-relevant
code example:
1 #edited by: Shanlong2 ImportTime#importing the time module3 Print(Time.time ())#prints the timestamp from 1970-01-01 00:00:00 to this moment4 Print(Time.strftime ("%y-%m-%d"))#format the display format of time strings5 Print(Time.localtime ())#Prints the date time of the current time zone, returned in tuples6 Print(Time.localtime (). Tm_hour)#A field that prints hours in the current time zone, with many similar properties: Tm_year,tm_mon,tm_mday, etc.7 Print(Time.gmtime ())#Prints the datetime of the UTG time zone, returned in tuples with the same properties as in LocalTime ()8 Print(Time.localtime (1393204575))#Prints the date time of the current time zone corresponding to the timestamp, returned in tuples9 Print(Time.asctime ())#print a well-defined date format: Wed Apr 19:04:50Ten Print(Time.ctime (1393204575))#print a timestamp with a defined date format: Mon Feb 09:16:15 OneTime.sleep (1)#keep the current process in hibernation for 1 secondsTime Module
Random module
Random module, here is a method that contains a series of random number generation
code example
1 #edited by: Shanlong2 ImportRandom#importing the Random module3 Print(Random.random ())#get a random decimal number greater than 0, less than 14 Print(Random.choice ([1,2,3,4,5]))#randomly extracting a value from the list5 Print(Random.randint (1,100))#randomly extracts an integer value from 1-1006 Print(Random.randrange (1,100))#randomly extracts an integer value from 1-997 Print(Random.sample ([1,2,3,4,5,6,7],2))#randomly extract two values from a list8 Print(Random.uniform (1,5))#randomly extracts a number greater than or equal to 1 and less than or equal to 5 with decimals9L = [1,2,3,4,5,6,7]TenRandom.shuffle (L)#intended list Order One Print(l)
Random Module
OS Module
As the name implies, this module is the operating system interaction module, which contains a lot of methods
1 OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work2Os.chdir ("dirname") Change the current script working directory, equivalent to the shell CD3Os.curdir returns the current directory: ('.')4Os.pardir Gets the parent directory string name for the current directory: ('..')5Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory6Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on7Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir dirname8Os.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir dirname9Os.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints as a listTen os.remove () delete a file OneOs.rename ("oldname","newname") Rename File/Catalogue AOs.stat ('Path/filename') Get File/directory information -OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/" -OS.LINESEP output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n" the os.pathsep output is used to split the file path of the string win down for;, Linux for: -The Os.name output string indicates the current usage platform. Win->'NT'; Linux->'POSIX' -Os.system ("Bash Command") to run the shell command to display directly - Os.environ getting system environment variables + Os.path.abspath (path) returns the absolute path normalized by path - os.path.split (path) splits path into directory and file name two tuples returned + os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path) A os.path.basename (path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) at os.path.exists (path) returns true if path exists, false if path does not exist - os.path.isabs (path) returns True if path is an absolute path - os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false - os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false - Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored - os.path.getatime (path) returns the last access time of the file or directory to which path is pointing in os.path.getmtime (path) returns the last modified time of the file or directory to which path is pointing -Os.path.getsize (path) returns the size of pathOS module
I stole a lazy, borrowed the Egon summary code (actually I know he also stole from others).
SYS module
1 #edited by: Shanlong2 ImportSys#Import Module SYS3sys.argv#understood from the name, in fact, like args, here is the command line to execute code, followed by parameters, receive a list4Sys.version#get the version of the Python interpreter5Sys.path#Gets the search path (list) of the module, which is searched sequentially6Sys.platform#gets the current Python running platform (operating system)7Sys.exit ()#Exit Program
Hashlib Module
hash algorithm module, which computes characters into a fixed-length value and is irreversible
code example:
1 # edited by: Shanlong 2 Import hashlib# importing Hashlib module 3 a = HASHLIB.MD5 ()# let a get hash.md5 algorithm 4 a.update ("aaaa". Encode ("UTF8")) # convert "AAAA" to MD5 form 5 Print (A.hexdigest ())
Python Concepts-common modules What the hell are you?