Python Learning (4)

Source: Internet
Author: User
ArticleDirectory
    • 1. operators:
    • 2. Operators and usage:
    • 3. Operator priority:
    • 4. Change the computing sequence:
    • 5. combination rules:
    • 6. expression:
Iv. Operators and expressions

Most statements (logical rows) contain expressions. A simple expression example is 2 + 3. An expression can be divided into operators and operands.

The function of operators is to complete something, which is represented by symbols such as + or other specific keywords. Operators need data to perform operations. Such data is called operands. In this example, 2 and 3 are operands.

1. operators:

The interpreter can be used interactively to calculate the expression given in the example. For example, to test expression 2 + 3, use an interactive Python interpreter with a prompt:

>>> 2 + 3
5
>>> 3*5
15
>>>

2. Operators and usage:

Most operators are similar to C/C ++. Here we propose different operators:

** Power Returns the Power Y of X. 3 ** 4 get 81 (3*3*3*3)
// Integer Division Returns the integer part of the operator. 4 // 3.0 get 1.0
Not Boolean "Non" If X is true, false is returned. Returns true if X is false. X = true; not y returns false.
And Boolean "and" If X is false, X and Y returns false. Otherwise, it returns the calculated value of Y. X = false; y = true; X and Y, returns false because X is false. Python does not calculate y here, because it knows that the value of this expression must be false (because X is false ). This phenomenon is called short circuit computation.
Or Boolean "or" If X is true, it returns true; otherwise, it returns the calculated value of Y. X = true; y = false; X or Y returns true. Short circuit calculation is also applicable here.
3. Operator priority:
Operator Description
Lambda Lambda expressions
Or Boolean "or"
And Boolean "and"
Not x Boolean "Non"
In, not in Member Testing
Is, is not Identity Test
<, <=,>, >= ,! =, = Comparison
| By bit or
^ Bitwise OR
& Bitwise AND
<,> Shift
+ ,- Addition and subtraction
*,/, % Multiplication, division, and remainder
+ X,-x Plus or minus sign
~ X Flip by bit
** Index
X. Attribute Attribute reference
X [Index] Subscript
X [index: Index] Addressing segment
F (arguments ...) Function call
(Experession ,...) Bind or display tuples
[Expression,...] List display
{Key: datum ,...} Dictionary display
'Expression ,...' String Conversion
4. Change the computing sequence:

The operator priority table determines which operator is calculated before another operator. However, parentheses can be used to change the order of calculation. For example, to allow addition to be calculated before multiplication in an expression, you must write it as (2 + 3) * 4.

5. combination rules:

Operators are usually combined from left to right, that is, operators with the same priority are calculated from left to right. For example,2 + 3 + 4Calculated(2 + 3) + 4. Some operators such as the value assignment operator are combined from the right to the left, that isA = B = CProcessedA = (B = C).

6. expression:

For example:

# Filename: expression. py length = 5 breadth = 2 area = length * breadthprint ('area is ', Area) print ('Perimeter is', 2 * (Length + breadth ))

Output:

C: \ Users \ Administrator> Python D: \ Python \ expression. py

Area is 10

Perimeter is 14

AboveProgramThe area and perimeter of the rectangle are obtained.

We noticed that although we are not'Area is'And VariablesAreaSpecify a space between them. Python automatically places a space there, so that we can get a clear and beautiful output, the program also becomes easier to read (because we do not need to worry about the space issue between outputs ).

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.