Python ~~ Introduction, python Introduction

Source: Internet
Author: User

Python ~~ Introduction, python Introduction

Python (English pronunciation:/pa θ n/American pronunciation:/pa θ 116n/) is an object-oriented interpreted computer programming language, invented by Dutch Guido van rosum in 1989, the first public release was released in 1991.

Python is purely free software. The source code and interpreter CPython follow the GPL (GNU General Public License) protocol.

The Python syntax is concise and clear. One of its characteristics is to force the white space to be used as the statement indent.

Python has rich and powerful libraries. It is often called the glue language, and can easily connect various modules (especially C/C ++) made in other languages. A common application scenario is to use Python to quickly generate a prototype of a program (sometimes even the final interface of the Program) and then have a special requirement on it, rewrite with a more appropriate language. For example, if the graphic rendering module in 3D games has high performance requirements, you can rewrite it with C/C ++ and encapsulate it as an extension class library that can be called by Python. Note that you may need to consider Platform issues when using the extended class library, and some may not provide cross-platform implementation.

Style

Python adheres to a clear and uniform style in design, which makes Python a language that is easy to read, easy to maintain, and popular and widely used by a large number of users.

The general guiding ideology of designers during development is that there is only one best solution to a specific problem. This is expressed in The Python motto (called The Zen of Python) written by Tim Peters: There shocould be one -- and preferably only one -- obvious way to do it. this is exactly the opposite of the central idea of TMTOWTDI (There's More Than One Way To Do It) in Perl (another functional high-level Dynamic Language.

The author of Python intentionally designs highly restrictive syntax, so that bad programming habits (for example, the next line of the if statement is not indented to the right) cannot be compiled. One of the most important items is the indentation rule of Python.

The difference between a language and most other languages (such as C) is the boundary of a module, it is entirely determined by the position of the first character of each line in this line (while the C language uses a pair of curly braces {} to clearly define the boundary of the module, is irrelevant to the character position ). This has caused controversy. Since the birth of a language like C, the Syntactic meaning of the language is separated from the character arrangement, and it has been regarded as a progress of a programming language. However, it is undeniable that Python makes the program clearer and more beautiful by forcing programmers to indent (including if, for, and function definition.

Design Positioning

The design philosophy of Python is "elegant", "clear", and "simple ". Therefore, the idea of "there are always multiple ways to do the same thing" in Perl is often unbearable for Python developers. The philosophy of Python developers is "one method, preferably one method, to do one thing ". When designing the Python language, Python developers usually refuse to use fancy syntaxes if there are multiple choices, and select clear or unambiguous syntaxes. Due to this difference in design concepts, Python source code is generally considered to be more readable than Perl and can support large-scale software development. These rules are called the Python motto. Run import this in the Python interpreter to obtain the complete list.

Python developers try to avoid immature or unimportant optimizations. Some patches that accelerate the running of non-important parts are usually not merged into Python. So many people think Python is slow. However, according to the law, most programs do not require high speed. In some situations where the running speed is very high, Python designers tend to use JIT technology, or use C/C ++ to rewrite this part of the program. The available JIT technology is PyPy.

Python is a fully object-oriented language. Functions, modules, numbers, and strings are all objects. It fully supports inheritance, overload, derivation, and multi-inheritance, which is beneficial to enhance the reusability of source code. Python supports reload operators and dynamic types. Compared with the traditional functional programming language like Lisp, Python only provides limited support for functional design. There are two Standard libraries (functools, itertools) that provide long-tested functional programming tools in Haskell and Standard ML.

Although Python may be roughly classified as a "script language", Google also widely uses some large-scale software development plans, such as Zope, Mnet, and BitTorrent. Python fans prefer to call it a high-level dynamic programming language, because "scripting language" refers to a language that is used only for simple programming tasks, for example, shellscript, VBScript, and other programming languages that can only process simple tasks cannot be compared with Python.

Python itself is designed to be scalable. Not all features and functions are integrated into the language core. Python provides a variety of APIs and tools, so that programmers can easily use C, C ++, and Cython to write expansion modules. The Python compiler itself can also be integrated into other programs that require scripting languages. Therefore, many people also use Python as a "glue language. Use Python to integrate and encapsulate programs written in other languages. In many projects within Google, for example, Google Engine uses C ++ to write components with high performance requirements, and then calls the corresponding modules using Python or Java/Go. Alex Martelli, author of The Python Technical Manual, said: "This is hard to tell. However, in 2004, Python was used inside Google and Google recruited many Python experts, but before that, Python has been decided. Their purpose is to use Python where we can, C ++ where we must, and C ++ in hardware control, use Python during rapid development."

Basic syntax

One of Python's design goals is to make the code highly readable. It tries its best to use punctuation marks and English words frequently used in other languages to make the Code look neat and beautiful. Unlike other static languages such as C and Pascal, the statement must be repeatedly written, and there are not many special cases and accidents like their syntax.

 

Indent

Python developers intentionally prevent programs that violate indentation rules from being compiled to force programmers to develop good programming habits. In addition, the Python language uses indentation to indicate the start and exit of the statement block (Off-side rules), rather than using curly braces or certain keywords. Increasing indentation indicates the beginning of the statement block, while decreasing indentation indicates the exit of the statement block. Indentation is part of the syntax. For example, if statement:

1234 if age<21:    print("You cannot buy wine. ")    print("But you can buy chewing gum. ")print("This sentence is outside the if statement block. ")

Note: The above example is python 3.0 code.

According to PEP, four spaces must be used to represent each level of indentation (it is unclear how the four spaces are defined. In actual writing, the number of spaces can be customized, but the number of spaces must be equal for each indentation ). Although tabs and other spaces can be compiled, they do not conform to the encoding rules. Supporting Tab characters and other spaces is only compatible with old Python programs and some problematic editing programs.

 

Control statement

If statement. When the condition is true, the statement block is run. It is often used with else and elif (equivalent to else if.

The for statement traverses the list, String, Dictionary, set, and other iterators, and processes each element in the iterator in sequence.

When the while statement is true, the statement block is run cyclically.

Try statement. Works with mongot and finally to handle exceptions that occur during the program running.

Class statement. Defines the type.

Def statement. Defines functions and types.

Pass statement. This action is empty and does not run any operation.

Assert statement. Used to test whether the running conditions are met during program debugging.

With statement. The syntax defined after Python2.6 runs statement blocks in a scenario. For example, encrypt the block before running the statement and decrypt it after it exits.

Yield statement. Used in the iterator function to return an element. Since Python 2.5. This statement becomes an operator.

Raise statement. Make a mistake.

Import Statement. Import a module or package.

From import Statement. Import an object from a package or from a module.

Import as statement. Assign the imported object to a variable.

In statement. Determines whether an object is in a string/LIST/tuples.

 

Expression

The expression in Python is similar to that in C/C ++. However, there are differences in some writing methods.

The main arithmetic operators are similar to C/C ++. + ,-,*,/,//,**,~, % Indicates addition or addition, subtraction, or addition, multiplication, division, division, multiplication, population, and remainder. >>, <Indicates shift right and shift left. &, |, ^ Indicates the AND, OR, XOR operations of binary.>, <, = ,! =, <=, >= Is used to compare the values of two expressions, indicating greater than, less than, equal to, not equal to, less than or equal to, and greater than or equal. In these operators ,~, |, ^, &, <,> Must be an integer.

Python uses and, or, not to represent logical operations.

Is, is not is used to compare whether two variables are the same object. In, not in is used to determine whether an object belongs to another object.

Python supports "list comprehension", for example, calculating the sum of squares of 0-9:

>>> Sum (x * x for x in range (10 ))

285

Python uses lambda to represent anonymous functions. The anonymous function body can only be an expression. For example:

>>> Add = lambda x, y: x + y

>>> Add (3, 2)

5

Python uses y if cond else x to represent conditional expressions. If cond is true, the expression value is y; otherwise, the expression value is x. Is equivalent to cond in C ++ and Java? Y: x.

Python lists and groups (tuple. List is written as [1, 2, 3], while tuple is written as (1, 2, 3 ). You can change the elements in the list but not tuple. In some cases, the brackets of the tuple can be omitted. Tuple has special processing for the value assignment statement. Therefore, you can assign values to multiple variables at the same time, for example:

>>> X, y = # assign values to x and y at the same time. The final result is: x = 1, y = 2.

In particular, you can exchange the values of two variables in the following form:

>>> X, y = y, x # final result: y = 1, x = 2

Python uses '(single quotes) and "(double quotes) to represent strings. Unlike Perl, Unix Shell, Ruby, Groovy, and other languages, the two symbols work the same. Generally, if a string contains double quotation marks, the string is represented by single quotation marks. Otherwise, double quotation marks are used. If none of them appear, you can choose according to your personal preferences. The \ (backslash) in the string is interpreted as a special character. For example, \ n indicates a line break. Add r before the expression to indicate that Python does not interpret the \ in the string \. This method is usually used to write regular expressions or Windows File paths.

Python supports list slices to obtain a complete list. The following types are supported: str, bytes, list, And tuple. Its syntax is... [left: right] or... [left: right: stride]. Assuming that the value of the nums variable is [1, 3, 5, 7, 8, 13, 20], the following statements are true:

Nums [2: 5] = [5, 7, 8] Cut from an element whose subscript is 2 to an element whose subscript is 5, but does not contain an element whose subscript is 5.

Nums [1:] = [3, 5, 7, 8, 13, 20] Cut to the last element.

Nums [:-3] = [1, 3, 5, 7] Cut from the first element to the last 3rd elements.

Nums [:] = [1, 3, 5, 7, 8, 13, 20] returns all elements. Changing the new list does not affect nums.

Nums [] = [3, 7] Cut from an element whose subscript is 1 to an element whose subscript is 5 but does not contain an element whose subscript is 5, and the step size is 2.

For more information, see mingli.com.

A friend needs to add a ball :,
More Python technical knowledge is provided. Thank you!

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.