Module
Python has a very rich library of standard and third-party libraries. It is imported with the import command before it can be called in subsequent code.
SYS module
Import Sysprint (sys.path) # Print environment variable
The result output is a list of directories, which are the environment variables that Python runs. Python will look in these directories in turn when importing.
But there is no sys.py this file. Search, sys module does not have a. py file, it is built into the interpreter, and is written in C language.
An important usage of the SYS module
Import Sysprint (SYS.ARGV)
It needs to be executed in the system, with the parameter after the file name.
$ python test.py 123 ABC
The directory in which the execution file will be output, followed by all parameters (strings) at execution time, as follows
[' test.py ', ' 123 ', ' ABC ']
OS Module
Import osa = Os.system ("dir") # Call system command Print ("-->\n", a)
Here is an example of the dir command in a Windows system. The execution results are output after execution, but the content in variable A is only 0, indicating that the command executed successfully. If the command executes an error, the result will be 1.
If you need to save the results of the command execution to a variable, you need this:
Import osa = Os.popen ("dir"). Read () print ("-->\n", a)
Os.system just executes the command, saves the execution results, only the output feedback on the screen, no records, so we can not root in the feedback information to do any processing.
Os.popen is the feedback to be executed is saved, but not directly output, the output should be memory address. It also uses the Read method to take out the contents.
Os.mkdir ("directory Name") can also create a directory
Finally, the code you write is saved as a py file, and you can import it elsewhere. Be careful to place the environment variable in the directory, or modify the environment variable (this is not speaking). Can be placed in the same directory as the execution file, and the directory where the files are executed is also in the environment variable.
Data type
Numbers and strings are not too much of a problem. The plural a+bj in a number. Python uses j, and I used to learn it in school. Well, about the plural basically forget the light, seemingly also think of what needs to be applied to the scene.
The square root of negative numbers also forgets what is used. and the Math module does not support complex numbers. The Cmath module is required to support the calculation of complex numbers. Do not try it specifically.
Data operations
It's just a little too simple.
Member operations
In and not in. is in the specified sequence.
A = [1,2,3,4]print (1 in a) print (5 in a) print (1 not in a) print (5 not in a)
Identity operations
Is and is not. Whether the object is the same. Generally used to determine the data type.
A = [1,2,3,4]b = "Hello" print (Type (a)) print (type (a) is list) print (type (a) was str) print (type (b)) print (type (b) is not list) Print (type (b) is not str)
Bit arithmetic
&,|,^,~,<<,>>. These. Mainly binary computing. You should use it when you count the IP address.
A = 3 # 0b11print (~a&0xff) # limit inversion within 255 (0xff)
0000 0000 0000 0011 negation is [1]111 1111 1111 1100 The first is the sign bit, so the direct negation is negative. Here &0xff the high minus sign (the first 1) and the other 1 to 0, the result is 0000 0000 1111 1100 or 252. You should be able to use it when calculating the mask.
Ternary operations
First, fill in the shorthand for if. If the condition is determined, then only one sentence can be abbreviated in a line.
If True:print ("Hello")
Assign a value or an operation with a ternary operation
A = 1 if True Else 2b = 3 if False else 4print (A, B)
Print ("a") if True else print ("B") print (1) if False else print (2)
Try it. Using logical operations and and or can also be implemented, but not recommended. In this case, the empty string is judged to be false instead of the empty string we want to get.
A = "If True else" You can't See me a "b =" "and True or" You can't see me B "c = True and" "or" you can't see Me C "Print (" a ", a) print (" B ", b) print (" C ", c)
So it makes sense to use the officially provided ternary operation. Another try, and you can keep nesting.
Print (' a ') if 1 else print (' B ') if 1 else print (' C ') print (' a ') if 0 else print (' B ') if 1 else print (' C ') print (' a ') if 0 el Se print (' b ') if 0 else print (' C ') print (' a ') if 0 else print (' B ') if 0 else print (' C ') if 0 else print (' d ')
Binary data bytes
The Pathon3 separates the string from the binary type. You can't mix now, only type conversions.
When the text is transmitted over the network, you can pass the string directly, and now you must first turn the string into binary before passing it.
Conversion of data types using encode and decode.
msg = "749b= Knight Reinforcements" a = Msg.encode ("Utf-8") # default Utf-8, so can be omitted, but the recommendation is to add print (a) code = B ' \xe6\x9d\xa5\xe7\x8e\xa9\xe9\xad\ X94\xe7\x8e\x8b\xe7\x9a\x84\xe5\x92\x9a\xef\xbc\x81 ' B = Code.decode ("Utf-8") print (b)
Homework
Finish the work of this module
Writing the Login interface
Multilevel Menu
Python Automation Development Learning 2