Nine, the first knowledge of the module
Python's strength is that he has a very rich and powerful standard library and third-party library, almost any function you want to implement has the corresponding Python library support, later in the course will be in depth to explain the various libraries commonly used, now, we first to symbolically learn 2 simple.
Standard library (libraries that can be imported without installation, use Python for some of the most common features getpass, etc.)
Third-party libraries (libraries that must be downloaded to install)
Sys
Import SYS Print (Sys.path) # output [' c:\\users\\administrator\\pycharmprojects\\week1\\day1 ', ' c:\\users\\administrator\\pycharmprojects\\ Week1 ', ' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35\\python35.zip ',......]
The imported library cannot have the same name as the existing local library.
SYS module must exist in these paths, if you go to these paths found not found, it will be reported that the SYS module does not exist, the existence of the found, these environment variables are placed in Python internal files or modules of the place.
Third-party libraries are generally placed inside the lib\\site-packages.
Import#print (sys.path) #打印环境变量 print# output $ Python test.py helo world['test.py'helo' ' World '] # gets the arguments passed when executing the script.
The above output is actually a list.
Os
Import os Os.system ("df-h"# call system command
Import= Os.system ("dir"# executes the command, does not save the result print ("--", Cmd_res)
Import= Os.popen ("dir"). Read ()# No read indicates only one object address Print("--", Cmd_res)
#这个操作读出来以后和上面的不同, not garbled.
Import Osos.mkdir ("new_dir") # Create a directory
Completely combine:
Import Os,sys Os.system ("# ) give the user's input parameters as a command to Os.system to execute
Write a module yourself
Python tab Completion module
1#!/usr/bin/env python2#python startup file3ImportSYS4ImportReadLine5ImportRlcompleter6Importatexit7ImportOS8#Tab Completion9 Readline.parse_and_bind ('Tab:complete')10#History FileHistfile = Os.path.join (os.environ['HOME'],'. Pythonhistory')12Try:13readline.read_history_file (histfile)14exceptIOError:15Pass16Atexit.register (Readline.write_history_file, Histfile)17delOS, Histfile, ReadLine, Rlcompleter
For Linux
After you've written it, you can use it.
1localhost:~jieli$ python2Python 2.7.10 (default, Oct 23 2015, 18:05:06)3[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on Darwin4Type" Help","Copyright","credits" or "License" forMore information.5>>>Importtab
You will find that the above-written tab.py module can only be imported in the current directory, if you want to use the system where and how to do it? At this point you have to put this tab.py in the Python Global environment variable directory, basically put in a directory called Python/2.7/site-packages, this directory in different OS placed in the same location, with print (Sys.path) You can view a list of Python environment variables.
10. What's a pyc?
1. Is python an interpreted language?
The first thing I heard about Python when I was a beginner python was that Python was an explanatory language, and I kept believing until I found the *.pyc file. If it is an interpreted language, what is the generated *.pyc file? c should be the abbreviation of compiled!
In order to prevent other people who learn python from being misunderstood by this remark, we will clarify this issue in the text and make some basic concepts clear.
2. Explanatory and compiled languages
Computers are not able to recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in the process of translating high-level languages into machine languages 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 process of compiling, but rather, when the program is running, it interprets the program line by row, then runs directly, and the most typical example is Ruby.
Through the above example, we can summarize the advantages and disadvantages of the explanatory language and the compiled language, because the compiler language before the program has already made a "translation" of the program, so at run time there is less "translation" process, so the efficiency is higher. But we also can't generalize, some interpretive languages can also be optimized by the interpreter to optimize the whole program when translating the program, thus more efficiently than the compiled language.
In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide the language into two types-----explanatory and compiled.
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 we say that Java is a language that is compiled and interpreted first.
3. What exactly is Python?
In fact, Python, like java/c#, is also a virtual machine-based language, let's start with a simple look at the Python program's running process.
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.
Students familiar with Java can consider how we execute a Java program on the command line:
Javac Hello.java
Java Hello
Just when we were using an IDE like Eclipse, we fused these two parts into a piece. In fact, Python is also the same, when we execute Python hello.py, he also executes such a process, so we should describe the Python,python is a first compiled after the interpretation of the language.
4. Brief description of Python's running process
Before we say this question, let's start with two concepts, pycodeobject and PYC files.
The PYC we see on the hard drive naturally doesn't have to say much, and pycodeobject is actually the result of a Python compiler actually compiling it. Let's just get to the bottom of it and keep looking down.
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 Road, Day1-python Foundation 1