[Python] vocabulary and grammar

Source: Internet
Author: User
Tags class definition

1. Row structure and indentation

Each statement in the program ends with a newline character. You can use the continuation character "\" to break a long statement into several lines, for example:

a = math.cos(3 * (x - n)) +             math.sin(3 * y - n)

A string, list, tuple, or dictionary that is defined with three quotation marks does not need to use a continuation character when it is spread over multiple lines. Indents are used to represent different blocks of code, such as function bodies, conditional statements, loops, and classes. The indentation of the first statement in a code block can be arbitrary, but the indentation in the entire code block must be consistent.
To place multiple statements on a single line, you can use the semicolon ";" Separated. Unless you are running in interactive mode, the interpreter ignores all blank lines.

2. Identifiers and reserved words

Identifiers are names that recognize variables, functions, classes, modules, and other objects. Identifiers can contain letters, numbers, and underscores, but must start with non-numeric characters and are case-sensitive. In addition, words such as if, else, and for are reserved words and cannot be used as identifier names.
Identifiers that begin or end with an underscore usually have a special meaning. For example, an identifier that starts with an underscore cannot be imported using the From module import * statement, and the identifiers with double underscores are reserved for special methods, and the identifiers preceded by double underscores are used to implement private class members.

3. Digital literals

Built-in digital literals are divided into 4 types: boolean, Integer, floating-point, and complex. Identifiers true and False are interpreted as Boolean values whose integer values are 1 and 0, respectively. A number like 123 is interpreted as a decimal number. To specify integers using octal, hexadecimal, or binary, you can add 0, 0x, or 0b to the front of the value.
In Python, the number of digits in an integer is arbitrary, so if you want to specify a very large integer, simply write out all of the digits. Old code may see a number followed by L or L, and it is not necessary to choose a machine integer or a long integer type with arbitrary precision that is represented internally as a fixed precision in an integer.
Figures such as 123.4 and 1.23e+02 are interpreted as floating-point numbers. An integer or floating-point number followed by J or J constitutes an imaginary number, such as 12.34J, a real number plus an imaginary number that forms the plural.

4. String literals

String literals are used to specify a sequence of characters, which is defined by placing the text in single quotation marks "'", double quotation marks "", or "" "or" "". There is no semantic difference between the three forms of quotation marks. A string of three quotes can be spread over multiple lines and include all formatting symbols (line breaks, tabs, spaces, and so on).
The backslash "\" character is used to escape special characters such as line breaks, backslash itself, quotation marks, and nonprinting characters. Alternatively, you can add R or R, such as R ' \d ', before the string literal. These strings are called raw strings, because all of the escaped characters are preserved intact. The original string cannot end with a single backslash, such as r "\".

5. Containers

Place some values in square brackets "[...]", parentheses "(...)" and braces "{...}" , you can represent a collection of objects contained in a list, tuple, and dictionary, respectively.

6. Operators, separators and special symbols

The operators that Python can recognize are: + 、-、 *, *,/,//,%, <<, >>, &, |, ^, ~, <, >, <=, >=, = =,! =, <>, + =,-=, =,/=,//=,%=, *=, &=, |=, ^=, >>=, <<=. The following tags can be used as delimiters for expressions, lists, dictionaries, and different parts of statements: (,), [,], {,} 、,、: 、.、 ', =,;.
$ and? There is no meaning in Python and cannot appear in the program, but can appear in the string literal in quotation marks.

7. Document String

If the first statement defined by a module, class, or function is a string, the string becomes the document string for the related object, for example:

def fact(n):    "This function computes a factorial"    if (n <= 1): return 1    else: return n * fact(n-1)

The Code-browsing tool and the document generation tool sometimes use document strings. The document string can be accessed through the object's __doc__ property.

8. Decorative Device

A special symbol, called an adorner, can be used before a function, method, or class definition to modify the behavior behind the definition. Adorners are represented by the "@" symbol, which must be placed on a separate line and precede the corresponding function, method, or class, for example:

@staticmethoddef bar():    pass

[Python] vocabulary and grammar

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.