Python's Notes (i)

Source: Internet
Author: User
Tags python script

Basic features of Python
    • A programming language with dynamic explanatory type

    • Canonical code: Python uses forced indentation to make the code extremely readable.

    • Advanced language Features: encapsulated memory management, etc.

    • Portability: If a program avoids using system-dependent features, it can run on any platform without modification

    • Explanatory: Run the program directly from the source code, no longer need to worry about how to compile the program, making the program easier to port.

    • Object-oriented: support for process-oriented programming also supports object-oriented programming.

    • Extensibility: Require confidential or efficient code that can be written in C or C + + and then used in Python programs.

    • Embeddable: Python can be embedded in a C + + program to provide scripting capabilities to your program users.

    • Rich library: includes regular expressions, document generation, unit tests, threads, databases, Web browsers, CGI, FTP e-mail, XML, XML-RPC, HTML, WAV files, cryptography, GUI (graphical user interface), TK, and other system-related operations. In addition to the standard library, there are many other high-quality libraries, such as Wxpython, twisted, and Python image libraries, and so on.

    • Summary: Python is really a very exciting and powerful language. It combines the high performance with the simple and interesting features that make the program easy to write.

    • Python is developed using the C language, but Python no longer has complex data types such as pointers in the C language.

    • Python has a strong object-oriented nature and simplifies object-oriented implementations. It eliminates object-oriented elements such as protection types, abstract classes, interfaces, and so on.

    • Python has only 31 reserved words, and there are no semicolons, curly braces, begin, end, and so on.

Python Installation and operation
    • Python

    • Windows, after running the installation file, you also need to configure environment variables, after the "Path" of the environment variable with the English semicolon and Python installation directory such as: "; C:\Python27 "Without configuring environment variables, you cannot use Python directly on the command line.

    • There are two ways to run your program using Python

      @echo off Python.exe test.py Pause

      and the "test.py" content is the program that needs to be executed

      • 1. Use the interactive prompt interpreter directly double-click to run "Python.exe", enter content inside, such as: print ' Hello word '

      • 2. Use the source file in the Python installation directory, build a batch (Test.bat), write:

Python command-line options
Options        functions-c cmd    execute Python code directly on the command line. such as python -c print  "Hello world". -d        generates debug information from the interpreter after the script is compiled. With Pythondebug=1. -e        ignores environment variables. -h        Displays the Python command-line options help information. The-i        script executes immediately after it enters the interactive command line mode. With Pythoninspect=1. -o        optimizes the bytecode generated by the interpreter prior to execution. With  pythonoptimize=1. -oo       optimizes the bytecode generated by the interpreter prior to execution and removes the embedded document string from the optimized code. -q arg    In addition to the Regulation option,-qold (default),-qwarn,-qwarnall,-qnew. The-s        interpreter does not automatically import the site.py module. -t        generates a warning when the tab indentation format of the script is inconsistent. The-u        does not buffer stdin, stdout, and stderr, which are buffered by default. With Pythonunbuffered=1. -v        generates information for each module. If the two-v option, more detailed information is generated. With Pythonverbose=x. -v        Display PythThe version information for on. -w arg    error Message control. (Arg is action:message:category:module:lineno)-x        ignores the first line of the source file. Useful to execute scripts on multiple platforms. file      executes the code in file. -         reads the execution code from the stdin.
Common skills
Python-m Simplehttpserver [Port] # current directory opens a small file server, default port 8000# In addition, Python 3, python-m http.serverpython-m this # Python's zenpython-m calendar # show a calendar python-m antigravity # This is interesting python-m smtpd-n-C Debuggi Ngserver localhost:20025 # can write a small script as "mail server" interacts with MUA software python-c "Import Django" # This sentence is used to detect whether it can run successfully
Environment variable Configuration

*1. Python after installation, the Python command cannot be used directly under default Windows. You need to configure environment variables. Assuming that the installation path for Python is c:\python27, modify the path in the system variable, high-level environment variable---, properties---To run the Python command in command-line mode, You need to attach the directory where the Python.exe resides to the PATH environment variable. )

Path=path;c:\python27

Once the above environment variable is set up successfully, you can use the Python command directly at the command line. or execute "python *.py" to run the Python script.

Import of the third party libraries
 How do I enable the Python interpreter to import directly from a third-party module other than the default installation path? In order to be able to  import  a third-party module other than the default installation path (such as a module,  or a third-party library version conflict), the system environment is a list that can add the libraries you need, such as MySQL libraries, hive libraries, and so on. There are three ways to add, all validated by: 1. Add in code: 
import syssys.pathsys.path.append (Path) #sys. Path.insert (1,  './libs ')  #  This will give the library a higher priority. 
This notation only allows the code to run in memory, without affecting other programs. 2.  use PTH files to permanently add      use PTH files, create  .pth  files in  site-packages  files, write the path of the module in , one line at a path, the following is an example, the PTH file can also be annotated with:         # .pth file for  the  my project (this line is a comment)         E:\DjangoWord         E:\DjangoWord\mysite         e:\djangoword\mysite\polls     This is a good way to do this, but there are administrative issues that cannot be shared in different versions of Python and affect all running Python programs. 3. Use  PYTHONPATH  environment Variables      use  PYTHONPATH  environment variables to enter the relevant path in this environment variable. The different paths are separated by commas (in English!). Separately, if the  PYTHONPATH  variable does not already exist, you can create it!      paths are automatically added to  sys.path  and can be shared in different versions of Python, which should be a convenient method      linux  Set Method:         command line   export pythonpath=/usr/lib /python27/site-packages/   This dialogue takes effect         vi ~/.bash_profile         Add  export PYTHONPATH=/usr/lib/python27/site-packages/        at the end   Re-login can be effective,  only for login user         vi /etc/profile          add at the end  export PYTHONPATH=/usr/lib/python27/site-packages/          re-login is effective,  for all users     windows  Setup method:          command line   SET  "pythonpath=c:\python27\lib\ Site-packages "   This session takes effect         " environment variable ", create a new variable named" PYTHONPATH ", Point to the directory you specified to take effect for your entire computer.
File types in Python
The 1.Python file type is divided into 3 types, namely source code, byte code, and optimized code. These can be run directly and do not need to be compiled or connected.  2. The source code with ". Py" is the extension, the Python is responsible for interpretation; 3. The source file is compiled with a file that has the extension ". PYc", which is a compiled byte file. This file cannot be modified with a text editor.  The PYc file is not related to the peace platform and can be run on most operating systems.  The following statement can be used to generate the PYc file: Import py_compile py_compile.compile (' hello.py ') 4. The optimized source file is suffixed with ". Pyo", which optimizes the code. It cannot be modified directly with a text editor, as the following command can be used to generate a PYO file: Python-o-M Py_complie hello.py
 byte compiled  .pyc  file      input A module is relatively time consuming, so Python does some tricks to make the input module faster.      one way is to create   byte-compiled files with  .pyc  as extensions. In addition, the files compiled by these bytes are platform-independent.     ,.pyc  files are very useful when you enter this module next time from another program-it will be much faster because the processing required for some of the input modules is complete.      Note: Although the  python  is open source, but the server can only keep  .pyc  files, and do not retain the source code, so normal operation. This ensures that the source code is not seen.     PYC content, is related to Python version, the different version of the compiled PYc file is different, 2.5 compiled PYC files, 2.4 version of the  python is unable to execute.      According to the Python source code provided in the opcode, can be based on the PYc file to decompile  py file source code, 2.6 after the source does not provide anti-compilation, but can also try to do. 
Generate  py  files  pyc  files: 1. Run directly from the command, you can see that the following command does not use the  compile ()   function,  This is because  py_  compile () is called in the  main ()   function of the compile  module  .    python -m py_ compile test.py    python -o -m py_compile test.py     -O  Optimized bytecode     -m  indicates that the following module is run as a script     -OO  Delete the document string while representing the optimization      if you want to see  compile (),  compile_dir (),  compile_path ()   The specific purpose of each parameter,     can be viewed using  print py_compile.compile () .__doc__ , or directly open  py_ compile.py,compileall.py  file to see. 2. Compile the. py file by writing the Python code, and then run the Python script to enter the. py file you want to compile during the run.     import py_compile    file=raw_input ("Please enter  filename:  ")     py_compile.compile (file) 3. If you are in a Linux environment, you can compile the. py by writing a bash script.     #! /bin/sh     (echo  ' Import compileall '; echo  ' Compileall.compile_dir ("./") ')  | python      Run with the Bash command after completing the above code. 4. Disable  python  automatically generate  .pyc  files at run time   python2.6  add a feature, as long as the environment variables   pythondontwritebytecode  is set to  x, the  .pyc  file is no longer generated.


This article is from the "Ops Boy" blog, please make sure to keep this source http://kaliroot.blog.51cto.com/8763915/1876053

Python's Notes (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.