I. Initial knowledge of the module (i.)
Modules, also called libraries. The library has a third-party library of standard libraries.
Note: The file name cannot be the same as the imported module name
1. SYS module
Import Sysprint (Sys.path) #打印环境变量print (SYS.ARGV) #打印相对路径, the pycharm output is an absolute path because print (Pycharm]) is called in Sys.argv[2 # You can take a value from the data list, [2] to refer to the third digit.
Standard Stock Placement: C:\Python35\Lib
Third-party inventory placement C:\Python35\Lib\site-packages
2. OS Module
Example one:
Import Osos.system ("dir") #运行结果: the dir command in the cmd command line
Example two:
Import oscmd_res = Os.system ("dir") #执行命令, do not save the result print ("--->", cmd_res)
The output is 0 because the output from the OS module is called only to the screen. 0 is the status of the success or failure of the command execution: return 0 for success, not 0 to indicate execution error
Example three:
Import OS cmd_res = Os.popen ("dir"). Read () print ("--->", cmd_res)
Note Add the ". Read ()" and the Non-additive difference: The output is not the memory address, plus the reference to read ()
Example four: Creating a directory under the current directory
Import Osos.mkdir ("New_dir") print ("--->", cmd_res)
Ii.. What is PYC?
1. Is python an interpreted language?
The first thing you hear about Python when you learn Python is that Python is an interpreted language until the existence of the *.pyc file is discovered. If it's an interpreted language, what happens when the *.pyc file is generated?
c should be an abbreviation for compiled.
To prevent this from being misunderstood when learning python, the problem needs to be clarified and some basic concepts should be cleared up.
2. Explanatory and compiled languages
Computers do not recognize high-level languages, so when we run a high-level language program, we need a "translator" for the process of translating high-level languages into machine language that computers can read. This process is divided into two categories, the first of which is compilation, and the second is interpretation.
A compiled language before a program executes, the program executes a compilation process through the compiler, transforming the program into machine language. The runtime does not need to be translated and executes directly. The most typical example is the C language.
The explanatory language does not have this compile process, but the program runs, through the interpreter to the program to explain the line, and then run directly, the most typical example is Ruby.
Through the above examples, summarize the advantages and disadvantages of the explanatory language and the compiled language. Because the compiled language has already made a "translation" of the program before the program runs, it burns out the "translation" process at run time, so it is more efficient. But we also can't generalize, some explanatory language can also be optimized by the interpreter to optimize the whole program when translating the program, so as to get close to the compiled language in an efficient mode.
In addition, with the emergence of virtual machine-based language, such as Java, we can not divide the language into two purely interpreted and compiled type.
In Java, for example, Java is first compiled into a bytecode file by a compiler and then interpreted as a machine file by the interpreter at run time. So Java is a language that is encoded and then interpreted.
3. What exactly is Python?
In fact, Python, like java/c#, is also a virtual machine-based language.
When we enter Python hello.py on the command line, we actually activate the Python interpreter and tell the interpreter: You're going to start working.
But before the "explain", the first thing that actually executes is the same as Java, which is compiled.
Python is a language that is compiled and interpreted first.
4. Brief description of Python's running process
Before saying this question, first say two concepts, pycodeobject and PYC files.
In fact, Pycodeobject is actually compiled by the Python compiler.
When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.
When the Python program runs for the second time, the program will first look for the PYc file on the hard disk, and if it is found, load it directly or repeat the process.
So we should be able to locate Pycodeobject and pyc files, we say that PYc file is actually a kind of persistent saving way of pycodeobject.
Python Learning notes (what are the modules, PYC, and Pycodeobject)