Ask Python's basic article "one" to know Python

Source: Internet
Author: User
Tags integer division

asked python "One" of the basic article

know python

1.python the introduction

1.1. What is python?

Python is an object-oriented, interpreted computer programming language, invented by Guido van Rossum in 1989, and the first public release was released in 1991.

Python syntax is simple and clear, one of the features is to force the use of whitespace as a statement indentation.

Python has a rich and powerful library. It is often nicknamed the glue language, and the various modules (especially C + +) that can be made in other languages are easily joined together.

1.2.python the characteristics

Simple, easy to learn, free, open source, high-level language, portability, interpretive, object-oriented, extensible, embeddable, Rich library.

Python's philosophy of Design: simple, elegant, clear

Two main advantages of Python: (1) Glue language (2) maintainability

1.3.python applications

1.3.1. System Programming

Python's built-in interface to operating system services makes it an ideal tool for writing portable management tools and parts (sometimes called shell tools) that maintain the operating system. Python programs can search for files and directory trees, run other programs, process or thread in parallel, and so on.

Python's standard library binds POSIX and other general operating system (OS) Tools: Environment variables, files, sockets, pipelines, processes, multithreading, regular expression pattern matching, command-line arguments, standard stream interfaces, shell command initiators, file name extensions, and so on. In addition, many Python system tools are designed with portability in mind. For example, a script that replicates a directory tree can run on almost any Python platform without any modification.

1.3.2. User Graphics interface

Python's simplicity and fast development cycles are ideal for developing GUI programs. Python's built-in Tkinter's standard object-oriented interface tk GUI API enables Python programs to generate portable, native-looking GUI. The Python/tkinter GUI can be run on platforms such as Microsoft Windows, X Windows (Unix and Linux), Mac OS (Classic and OS x support), without any changes. A free expansion pack, PMW, adds some advanced components to the Tkinter toolkit. In addition, the C + + platform-based toolkit Wxpython GUI APS can use Python to build a portable GUI.

1.3.3.Internet Script

Python provides standard Internet modules that enable Python to function extensively across a wide range of network tasks, both on the server and on the client side. Scripts can communicate through sockets, parse information from a form that is sent to a server-side CGI script, fetch a Web page from a URL, parse HTML and XML files from a retrieved Web page, and communicate through XML-RPC, soap, and Telnet. The Python library makes it all quite simple.

Furthermore, many third-party tools for Internet programming using Python are available on the web. For example, Htmlgen can generate HTML files from the description of the Python class, Mod_python packages can make Python programs running on Apaache services more efficient and support server templates like Python server page. It also supports server-side applets that are run by the client. In addition, Python has sprung up with many web development kits, such as Django

, TurboGears, flask and so on, so that PYTHN can quickly build a well-functioning and high-quality website.

1.3.4. Component Integration

Python can be extended through a C + + system and can nest the features of A/C + + system so that it can be scripted to handle the behavior of other systems and components as a flexible glue language.

1.3.5. Database Programming

For traditional database requirements, Python provides interfaces to all major relational database systems, such as Sybase, Oracle, Informix, ODBC, MYSQL, PostgreSQL, SQLite.

The Python standard pick module provides a simple object persistence system: It allows the program to easily save and restore entire Python objects to objects in files and file classes. On the network, you can also find a third-party system called ZODB, which provides a complete object-oriented database system for Python scripts, and the system sqlobject can map relational databases to Python's class modules. And, starting with the python2.5 version, SQLite has become a part of Python's own standard library.

1.3.6. Rapid Prototyping

1.3.7. Numerical Computation and scientific computational programming.

1.3.8. Games, images, artificial intelligence, XML, robotics, etc.

1.3.9. Cloud computing Platform (operation and maintenance system)

OpenStack is an IAAS (infrastructure as a service) component that allows anyone to build and deliver cloud computing services, all in Python.

1.4.python2. X and 3. The difference between x

1.4.1 . __FUTURE__ Module

python3.x introduces some keywords and features that are incompatible with python2. In Python2, these new content can be imported through the built-in __future__ module. If the code you want to write in the Python2 environment can also be run in python3.x, then we recommend using the __FUTURE__ module. For example, if you want to have python3.x integer division behavior in Python2, you can import the corresponding module by using the following statement

The following table lists the other features that can be imported in __future__:

For example:

1.4.2.print function

Although the print syntax is a small change in python3 and should be well known, it is worth mentioning that the print statement in Python2 is replaced by the print () function in the Python3. This means that in Python3, parentheses are required to enclose the correlation that requires output.

It is also possible to use extra parentheses in Python2. But in turn, when you want to call the Print function in python2 form without parentheses in Python3, the syntaxerror is triggered

Python 2

Python3

1.4.3. Integer Division

Because people tend to ignore Python3 's changes in integer division (which are incorrectly written and do not trigger syntaxerror), you need to pay particular attention to this change when porting code or executing python3 code in Python2.

So I will try to replace the 3/2 with float (3)/2 or 3/2.0 in the Python3 script. This avoids the errors that the code may cause in the PYTHON2 environment (or, in contrast, the Python3 division using the From __FUTURE__ Import division in the Python2 script)

Python 2

Python 3

1.4.4.Unicode

The Python2 has an ASCII-based STR () type, which can be converted to a Unicode type by a separate Unicode () function, but without a byte type.

In Python 3, there is finally a Unicode (Utf-8) string, and two byte classes: bytes and bytearrays.

No longer have to worry about coding problems.

1.4.5.xrange

In python2.x, an iterative object is often created with xrange (), which usually appears in "or loop" or "List/collection/dictionary derivation"

This behavior is very similar to the generator (such as "lazy evaluation"), but here the xrange-iterable is endless, meaning that there may be infinite iterations on this xrange.

Because of Xrange's "lazy curiosity" feature, range () is usually faster than xrange () if you only need to iterate once (for example, in a For loop). However, it is not recommended to use range () in multiple iterations, because a range () will regenerate a list in memory each time.

In Python3, Range () is implemented in the same way as the xrange () function, so there is no dedicated xrange () (The use of xrange () in Python 3 triggers nameerror).

Python 2

Python 3

1.4.6.range the __contains__ method in an object

In Python3. X, Range has a new __contains__ method. The __contains__ method can effectively accelerate the python3. "Find" speed for integers and booleans in X

1.4.7. Triggering exceptions

Python 2 supports both the old and new exception-triggering syntax, while Python 3 accepts only the parenthesized syntax (which would otherwise trigger SyntaxError):

1.4.8. Exception Handling

Python's exception handling in 3 also changed a little. The "as" keyword must be used in Python 3

1.4.9.next () functions and the. Next () method

In Python 2.7.5, both the function form and the method form can be used, whereas in Python 3, only the next () function (attempting to invoke the. Next () method will trigger Attributeerror)

1.4.10.for loop variables and global namespace leaks

In Python 3.x, the variables in the for loop no longer leak into the global namespace!

This is described in the official website of Python3, 0:

"List deduction no longer supports [... for var in item1, Item2, ...] This syntax, using the [... for Var in (item1, item2, ...)] Replace. Also note that list derivation has different semantics: now the list derivation is closer to the syntax sugar such as the generator expression in the list () constructor, especially note that the loop control variable is no longer leaking into the space around the loop.

1.4.11. Parsing the user's input via input ()

In Python 3, the input () function always stores the user's inputs as a Str object. In Python 2, in order to avoid some risky behavior of reading non-string types, you have to use Raw_input () instead of input ()

Python 2

Python 3

1.4.11. Returns an iterative object instead of a list

As you can see in the Xrange section, some functions and methods return an iterative object in Python, rather than returning a list in pythn2.

Because these objects are usually traversed only once, this approach saves a lot of memory. However, if you iterate over these objects multiple times through the generator, the efficiency is not high.

At this point we do need the list object, which can be easily converted to a list by the list () function.

1.5.python the installation

installation Python

Windows :

1

2

3

4

5

6

7

1. Download the installation package

https://www.python.org/downloads/

2. Installation

Default installation path: C:\python27

3. Configure Environment variables

"Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the second content box to find the variable named path of a row, double-click"-"Python installation directory appended to the variable value, with;

such as: the original value; C:\python27, remember that there's a semicolon in front

Linux :

1

2

3

No installation required, original Python environment

PS: If you bring your own 2.6, please update to 2.7

Update python

Windows:

1

Unloading load can be

Linux:

Linux Yum relies on its own python, to prevent errors, the update here is actually to install a python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

View the default Python version

Python-v

1, install GCC, for compiling python source code

Yum Install GCC

2, download the source package, https://www.python.org/ftp/python/

3. Unzip and enter the source file

4. Compile and install

./configure

Make all

Make install

5. View version

/usr/local/bin/python2.7-v

6. Modify the default Python version

mv/usr/bin/python/usr/bin/python2.6

Ln-s/usr/local/bin/python2.7/usr/bin/python

7. To prevent Yum from performing exceptions, modify the Python version used by Yum

Vi/usr/bin/yum

Change the head #!/usr/bin/python to #!/usr/bin/python2.6

Ask Python's basic article "one" to know 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.