Python Brief introduction

Source: Internet
Author: User
Tags arithmetic operators readable

Python (United Kingdom pronunciation:/paθn/American pronunciation:/paθɑn/) is an object-oriented, interpreted computer programming language, invented by Dutch Guido van Rossum in 1989, and the first public offering was released in 1991.

Python is purely free software, and the source code and interpreter CPython follow the GPL (GNU general public License) agreement.

Python syntax is simple and clear, and one of the features is to force whitespace (white space) to be indented as a statement.

Python has a rich and powerful library. It is often nicknamed the glue language and is able to easily connect a variety of modules made in other languages, especially in C + +. A common application scenario is to use Python to quickly build a prototype of a program (sometimes even the final interface of the program) and then rewrite it in a more appropriate language, such as a graphics rendering module in a 3D game, with a particularly high performance requirement, and can be rewritten in C + + with a specially requested part. It is then encapsulated as an extension class library that Python can call. It is important to note that you may need to consider platform issues when you use the Extended class library, and some may not provide cross-platform implementations.

Style

Python is designed with a clear, consistent style, which makes Python an easy-to-read, easy-to-maintain, widely-used language that is popular with many users.

The overall guiding ideology of designer development is that, for a particular problem, there is only one best way to solve it. This is stated in the Python motto (known as the Zen of Python) written by Tim Peters: there should be one--and preferably only one--obvious the-do it. This is exactly the opposite of the central idea of the Perl language (another feature-like advanced Dynamic language), TMTOWTDI (there's more Than one-way-to-do It).

Python authors intentionally design restrictive grammars that make bad programming habits (such as the next line of an if statement not indented to the right) fail to compile. One of the most important is the Python indentation rules.

The difference between one and most other languages, such as C, is that the bounds of a module are determined entirely by the position of the first character of each line in that line (and the C language is explicitly defined by a pair of curly braces {} to define the bounds of the module, which has no relation to the position of the character). This has been controversial. Since the birth of a language such as C, the grammatical meaning of the language is separated from the arrangement of the characters, which was once thought to be the progress of a programming language. It is undeniable, however, that Python does make the program clearer and more beautiful by forcing programmers to indent, including if,for and function definitions, where all the modules need to be used.

Design positioning

Python's design philosophy is "elegant", "clear", "simple". Therefore, the idea that "there are always multiple ways to do the same thing" in the Perl language is often unbearable for Python developers. The Python developer's philosophy is "in one way, it's best to have only one way to do something." When designing the Python language, Python developers tend to reject the fancy syntax when faced with a variety of options, and choose a syntax that is unambiguous and has little or no ambiguity. Because of this conceptual difference, Python source code is often thought to be more readable than Perl and can support large-scale software development. These guidelines are called Python maxims. Run import this in the Python interpreter to get a complete list.

Python developers try to avoid immature or unimportant optimizations. Patches that speed up operations against non-essential parts are usually not incorporated into Python. So many people think that Python is slow. However, according to the 28 law, most programs do not require high speed. In some cases where the speed requirements are high, Python designers tend to use JIT technology, or rewrite this part of the program in C + + language. The available JIT technology is pypy.

Python is a fully object-oriented language. Functions, modules, numbers, and strings are objects. and fully support inheritance, overloading, derivation, multi-inheritance, beneficial to enhance the reusability of the source code. Python supports overloaded operators and dynamic types. Python has limited support for functional design compared to Lisp, a traditional functional programming language. There are two standard libraries (Functools, itertools) that provide proven functional programming tools in Haskell and standards ml.

Although Python may be roughly categorized as a "scripting language" (script language), some large-scale software development programs such as Zope, Mnet, and Bittorrent,google are also widely used. Python supporters prefer to call it a high-level dynamic programming language because the "scripting language" refers to a language that is designed only for simple programming tasks, such as Shellscript, VBScript, and so on, which can only handle simple tasks, and cannot be compared to Python.

Python itself is designed to be extensible. Not all features and functionality are integrated into the language core. Python provides a rich range of APIs and tools so programmers can easily write extensions using C, C + +, Cython. The Python compiler itself can also be integrated into other programs that require scripting languages. As a result, many people also use Python as a "glue language" (glue language). Use Python to integrate and encapsulate programs written in other languages. Many projects within Google, such as Google Engine, use C + + to write high-performance parts, and then call the appropriate modules in Python or java/go. Alex Martelli, author of the Python Technical handbook, said: "It's hard to say, but in 2004, Python was used inside Google, and Google recruited many Python gurus, but before that, Martelli had decided to use Python. Their purpose is python where we can, C + + where we must, use C + + in the case of hardware manipulation, using Python in rapid development. ”

Basic syntax

One of the design goals of Python is to make the code highly readable. It is designed to use punctuation and English words often used in other languages to make the code look neat and tidy. Unlike other static languages such as C and Pascal, it does not have to be repeated to write declarative statements, nor is it often special cases and surprises like their syntax.

Indent in

Python developers intentionally make it impossible for a program that violates indentation rules to compile, forcing programmers to develop good programming habits. And the Python language uses indentation to represent the start and exit (Off-side rules) of a statement block, rather than using curly braces or some kind of keyword. Increasing the indentation represents the beginning of the statement block, and decreasing the indentation indicates the exit of the statement block. Indentation becomes part of the syntax

According to THE PEP rules, you must use 4 empty glyd to represent each level of indentation (unclear about the 4-space rule, you can customize the number of spaces in the actual writing, but to satisfy the equal number of spaces per level of indentation). Using the tab character and the other number of spaces can be compiled through, but not in accordance with the encoding specification. Support for tab characters and other numbers of spaces is just for compatibility with very old Python programs and some problematic editing programs.

Control statements

If statement, run the statement block when the condition is set. Often used in conjunction with else, elif (equivalent to else if).

For statement, which iterates through the iterators, such as lists, strings, dictionaries, and collections, and sequentially processes each element in the iterator.

While statement that loops through the statement block when the condition is true.

Try statement. Use with except,finally to handle exceptions that occur during program operation.

Class statement. Used to define the type.

def statement. The method used to define functions and types.

Pass statement. Indicates that this line is empty and does not run any operations.

Assert statement. The test run condition is satisfied when used in the program debugging phase.

With statement. Python2.6 The syntax that is defined later, running a block of statements in a scene. For example, the statement block is encrypted before it is run and then decrypted after the statement block runs out.

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

The Raise statement. Make a mistake.

Import statement. Import a module or package.

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

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

In statement. Determines whether an object is in a string/list/tuple.

An expression

Python's expressions are written in a similar style to C + +. Only in some of the wording of the differences.

The main arithmetic operators are similar to C + +. +,-,,/,//,, ~,% for addition or to take positive, subtraction or take negative, multiplication, division, divide, the powers, take the complement, take the remainder. >> << represents right and left shifts. &, |, ^ denotes binary and, OR, XOR operations. <, = =,! =, <=, >= the values used to compare two expressions, respectively, are greater than, less than, equal to, not equal to, less than equals, greater than or equal to. Inside these operators, ~, |, ^, &, <<, >> must be applied to integers.

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

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

Python supports list comprehension, such as calculating the sum of squares of 0-9:

SUM (x * x for x in range (10))

Python uses lambda to represent anonymous functions. An anonymous function body can only be an expression. Like what:

Add=lambda x, y:x + y
Add (3,2)

Python uses y if cond else x to represent the conditional expression. This means that when cond is true, the value of the expression is Y, otherwise the value of the expression is x. Equivalent to cond?y:x in C + + and Java.

There are two types of Python (list) and tuple (tuple). The list is written in [All-in-one], and the tuple's notation is (a). You can change the elements in the list without changing the tuple. In some cases, the parentheses of a tuple can be omitted. Tuple has special handling for assignment statements. Therefore, you can assign values to multiple variables at the same time, such as:

X, y=1,2# also assigns X, Y, and the final result: X=1, y=2

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

X, Y=y, x #最终结果: Y=1, x=2

Python uses ' (single quotation marks) and "(double quotation marks) to represent strings. Unlike languages such as Perl, the Unix shell language, or Ruby, groovy, the two symbols work the same way. In general, if double quotation marks appear in a string, a single quotation mark is used to denote the string, whereas a double quotation mark is used. If none of them appear, choose according to your preferences. The (backslash) that appears in the string is interpreted as a special character, such as \ n for newline characters. The expression pre-plus r indicates that Python does not interpret \ As it appears in the string. This notation is commonly used to write regular expressions or Windows file paths.

Python supports list slices, which can be part of a complete list. The types of support cutting operations are str, bytes, list, tuple and so on. 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], then the following statements are true:

Nums[2:5] = = [5, 7, 8] The element from subscript 2 is cut to the element labeled 5, but does not contain an element labeled 5.

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

NUMS[:-3] = = [1, 3, 5, 7] from the beginning of the element has been cut to the 3rd element of the bottom.

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

Nums[1:5:2] = = [3, 7] from the subscript 1 of the element is cut to the element subscript 5 but does not contain the element labeled 5, and the step is 2.

Python Brief introduction

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.