First knowledge of Python

Source: Internet
Author: User

First, application and characteristics

Python is the interpretation of execution, which belongs to the scripting language. Has a better cross-platform performance. Although the implementation speed is somewhat different than the C, but not the pursuit of speed of business and procedures, has been fully satisfied.

Current Python main application areas:

1. Cloud computing: The hottest language in cloud computing, typical applications OpenStack

2, Web development: A number of excellent web frameworks, many large sites are Python development, Youtube, Dropbox, watercress ... , a typical web framework has Django

3, scientific Computing, artificial intelligence: Typical library numpy, SciPy, matplotlib, Enthought Librarys,pandas

4. System operation and maintenance: necessary language for operation and maintenance personnel

5, Finance: Quantitative transactions, financial analysis, in the field of financial engineering, Python is not only used, and use the most, but also the importance of increasing year by year. Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific calculation and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting

6, graphic gui:pyqt, wxpython,tkinter

Second, the Python interpreter

When we write the Python code, we get a text file that contains the Python code for the .py extension. To run the code, you need the Python interpreter to execute the .py file.

Since the entire Python language is open source from spec to interpreter, it is theoretically possible for anyone to write a Python interpreter to execute Python code (which is difficult, of course) as long as the level is high enough. In fact, there are a number of Python interpreters.

CPython

When we downloaded and installed Python 2.7 from the official Python website, we immediately got an official version of the interpreter: CPython. This interpreter was developed in C language, so called CPython. Running at the command line python is to start the CPython interpreter.

CPython is the most widely used Python interpreter. All the code for the tutorial is also executed under CPython.

IPython

Ipython is an interactive interpreter based on CPython, meaning that Ipython is only enhanced interactively, but the functionality and CPython of executing Python code are exactly the same. Like many domestic browsers although the appearance of different, but the kernel is actually called ie.

CPython >>> is used as a prompt, while Ipython is used In [ 序号 ]: as a prompt.

PyPy

PyPy is another Python interpreter whose goal is to perform speed. PyPy uses JIT technology to dynamically compile Python code (note that it is not interpreted), so it can significantly improve the execution speed of Python code.

Most python code can run under PyPy, but PyPy and CPython are somewhat different, which results in the same Python code being executed under both interpreters. If your code is to be executed under PyPy, you need to understand the differences between PyPy and CPython.

Jython

Jython is a Python interpreter running on the Java platform that compiles python code directly to Java bytecode execution.

IronPython

IronPython is similar to Jython, except that IronPython is a Python interpreter running on the Microsoft. NET platform that compiles python code directly into. NET bytecode.

Three, python2.x and 3.x

In Summary:python 2.x are legacy, Python 3.x is the present and future of the language

Python 3.0 is released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

Extended support for this end-of-life release. The 2.x branch would see no new major releases after that. 3.x is

Under active development and have already seen over five years of stable releases, including version 3.3 in 2012,

3.4 In, and 3.5 in 2015. This means the recent standard library improvements, for example, is only

Available by default in Python 3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard F Or backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement are the better Unicode support (with all text strings being Unicode by default) as well as San ER bytes/unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) HAV E been adjusted to being easier for newcomers to learn and to is more consistent with the rest of the language, and old cruft have been removed (for example, all classes is now New-style, "range ()" Returns a memory efficient iterable, not a list a s in 2.x).

Since 2008 Python founder decided to upgrade Python to 3.x version, and no longer compatible with the version of 3.0, this is to remove some of the shortcomings of Python and add it a bit, is a bold decision, but there are still a large number of companies in the business is written in python2.x, the code can not be in the short Time migrated to 3.x and expensive, so in 2008 years after the 3.x and 2.x version of the update, but python2.x last stop update service time has been set for 2020 years, so python3.x is the development direction of Python.

Old:Print "The answer is", 2*2 New:Print("The answer is", 2*2) Old:PrintX#Trailing comma suppresses newline new:print (x, end= "") # Appends a space instead of a newlineOld:Print #Prints a newlineNew:Print()#You must call the function!Old:Print>>sys.stderr,"Fatal error"New:Print("Fatal error", file=Sys.stderr) Old:Print(x, Y)#Prints Repr ((x, y))New:Print((x, y))#Not the same as print (x, y)!
new and old code

And then the names of some of the libraries changed.

LD Name

New Name

_winreg

WinReg

Configparser

Configparser

Copy_reg

Copyreg

Queue

Queue

Socketserver

Socketserver

Markupbase

_markupbase

Repr

Reprlib

Test.test_support

Test.support

Iv. installation of Python

Install directly after downloading on official website, finally configure environment variables add Python's path to $path and execute the python command on the command line.

The same is true for Linux, but some dependencies may be required, or the/usr/local/nginx/bin/nginx.* can be made into a soft link to/etc/bin

Five, beginner python

The first representative code is typed

Print ("Hello world! ")

Vi.. 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.

Vii. Types of data

1. Digital

int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807long (Long integer)
Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.
Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.
Float (float) floats are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol.
Complex (plural)
The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers. Note: Small number pools exist in Python:-5 ~ 257 2, Boolean true or False 1 or 03, string
"Hello World"
string concatenation of all evils: the string in Python is embodied in the C language as a character array, each time you create a string, you need to open a contiguous space in memory, and once you need to modify the string, you need to open up again, the evil + sign every occurrence will re-open a space within. String formatted output
" Alex " Print "  " % name  # output: I am Alex

PS: string is%s; integer%d; floating-point number%f

String Common functions:
    • Remove whitespace
    • Segmentation
    • Length
    • Index
    • Slice
4. List Creation list:
Name_list = ['Alex'seven'Eric'  ] or name_list = List (['Alex'seven'  'Eric'])

Basic operation:

    • Index
    • Slice
    • Additional
    • Delete
    • Length
    • Slice
    • Cycle
    • Contains
5, tuples (immutable list) Create tuples:
Ages = (one,22, 33, 44, 55), and 11,
6. Dictionaries (unordered) Create dictionaries:
person = {"name""mr.wu"'age': 18  = dict ({"name""mr.wu"'  Age ': 18})

First knowledge of Python

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.