Python first day

Source: Internet
Author: User

First,python introduction

About Python

The founder of Python is Guido van Rossum (Guido van Rossum). During the Christmas of 1989, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.

The latest Tiobe leaderboard, Python overtook Php occupy five, Python advocating beautiful, clear, simple, is a good and widely used language.

Python is a high-level scripting language that combines explanatory, compiled, interactive, and object-oriented.

Python's design is highly readable, with English keywords often used in other languages, some punctuation in other languages, and it has a more distinctive grammatical structure than other languages.

    • Python is an interpreted language: This means that there is no compilation in the development process. Similar to the PHP and Perl languages.

    • Python is an interactive language: This means that you can execute your program in a Python prompt directly interacting with the execution.

    • Python is an object-oriented language: This means that Python supports object-oriented styles or code encapsulation in object programming techniques.

    • Python is the language of beginners: Python is a great language for novice programmers, and it supports a wide range of application development, from simple word processing to WWW browser to gaming.

Python type

    • Cpython
      The official version of Python, implemented using the C language, is the most widely used, and the CPython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine.
    • Jyhton
      Python Java implementation, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.
    • IronPython
      In Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)
    • PyPy(Special)
      Python implements Python, which compiles Python bytecode bytecode into machine code.
    • Rubypython,brython ...

Python development

Python was designed by Guido van Rossum in the late 80 and early 90 at the Dutch National Institute of Mathematics and Computer science.

Python itself is developed in many other languages, including ABC, Modula-3, C, C + +, Algol-68, SmallTalk, Unix Shell, and other scripting languages, among others.

Like the Perl language, the Python source code also follows the GPL (GNU general public License) protocol.

Now that Python is being maintained by a core development team, Guido van Rossum still occupies a vital role in guiding its progress.

Python features

    • 1. Easy to learn: Python has a relatively small number of keywords, simple structure, and a well-defined syntax that is easier to learn.

    • 2. Easy to read: The Python code is defined more clearly.

    • 3. Easy maintenance: The success of Python is that its source code is fairly easy to maintain.

    • 4. A broad library of standards: One of the biggest advantages of Python is the rich library, cross-platform, compatible with Unix,windows and Macintosh very well.

    • 5. Interactive mode: interactive mode of support, you can enter the execution code from the terminal and obtain the results of the language, interactive testing and debugging code snippets.

    • 6. Portable: based on its open source features, Python has been ported (that is, making it work) to many platforms.

    • 7. Extensible: If you need a critical code that runs fast, or if you want to write some algorithms that you don't like to open, you can use C or C + + to complete that part of the program and then call it from your Python program.

    • 8. Database: Python provides the interface for all major business databases.

    • 9.GUI Programming: Python support GUI can be created and ported to many system calls.

    • 10. Embeddable: You can embed python in a C + + program to give your program's users the ability to "script".

Second,python file Operation

File handle =open (' file ', ' open mode ')

Open file, after the operation remember to close the file

File handle. Close ()

Close () method

The close () method of the file object flushes any information that has not yet been written in the buffer and closes the file, which can no longer be written.

When a reference to a file object is re-assigned to another file, Python closes the previous file. Closing a file with the close () method is a good habit.

To prevent file open forgetting to close, you can use the with Operation file

With open (' A.txt ', ' R ') as F:    Pass

You can also manipulate multiple

With open (' A.txt ', ' R ') as F1,with open (' B.txt ', ' R ') as F2:    Pass

Different mode punch-in Files

Mode Description
R Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
Rb Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ Open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
Wb Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
Ab Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.

Three, the first Knowledge of the module

The power of Python is that he has a very rich and powerful library of standards and third-party libraries, and almost any feature you want to implement has the appropriate Python library support. A brief introduction to the 2 modules, Os,sys.

SYS module

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on Darwintype "Hel P "," copyright "," credits "or" license "for more information.>>> import sys>>> print (Sys.path) [', '/li Brary/frameworks/python.framework/versions/3.5/lib/python35.zip ', '/library/frameworks/python.framework/ versions/3.5/lib/python3.5 ', '/library/frameworks/python.framework/versions/3.5/lib/python3.5/plat-darwin ', '/ Library/frameworks/python.framework/versions/3.5/lib/python3.5/lib-dynload ', '/library/frameworks/ Python.framework/versions/3.5/lib/python3.5/site-packages ']

OS Module

Os.system

Run system commands on only one sub-terminal and cannot get return information after command execution

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on Darwintype "Hel P "," copyright "," credits "or" license "for more information.>>> import os>>> print (Os.system (' Date ')) May 14, 2016 Saturday 09:08 09 sec CST0

Os.popen

This method not only executes the command but also returns the executed information object.

You can also assign the returned result to a variable to facilitate the processing of the program

>>> a = Os.popen (' date '). Read () >>> print (a) May 14, 2016 Saturday 09:12 17 sec CST

Reference Blog: http://www.cnblogs.com/alex3714/articles/5465198.html

Python first day

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.