Python Learning op-expression precedence

Source: Internet
Author: User
Tags bitwise

In Python, a statement that has variables, values, and operators involved is called an expression.

Like what:

#字符串表达式 " Hello " #运算表达式 2+3#赋值表达式test  "hello"#变量表达式test

Operator Precedence

operator Description
Lambda Lambda expression
Or Boolean "or"
and Boolean "and"
Not X Boolean "Non"
In,not in Member Test
Is,is not Identity testing
<,<=,>,>=,!=,== Comparison
| Bitwise OR
^ Bitwise XOR OR
& Bitwise-AND
<<,>> Shift
+,- Addition and subtraction
*,/,% Multiplication, division and redundancy
+x,-x PLUS sign
~x Rollover by bit
** Index
X.attribute Property Reference
X[index] Subscript
X[index:index] Addressing segment
F (Arguments ...) Function call
(Experession,...) Binding or tuple display
[Expression,...] List display
{Key:datum,...} Dictionary display
' Expression,... ' String conversions

About shift Operations

Shift right: Shift right one representation divided by 2

8>>228>>318>>4  0

Here, the shift number is the number of the >> right, and the number shifted is the number of >> left.

Shift left: Shift left one means multiply by 2

3<<4

Here, the shift number is the number of the << right, and the number shifted is the number of << left.

Summary: The shift number is always on the right side of the shift symbol (<< or >>), and the shifted number is always on the left side of the shift symbol.

Priority level

Operator precedence is from low to high in the table above.

Other priority levels are as follows:

#以下优先级排名从高到低, in the same operation, the first execution of the high priority is low, and so on. #Top1: Function call, address, subscript #top2: Power Operation * *Prioritynumber=2*2**3print Prioritynumber #输出结果: -#Top3: Rollover Operation ~#Top4: Sign Print1+2*-3#输出结果:-5#Top5:*、/、%Print2+1*2/5#输出结果:2#Top6: +,-Print3<<2+1#输出结果: -#Top7:<<, >>#Top8: Bitwise &, ^, |#Top9: comparison operator priority=2*3+2<=2+1*7Print Priority #输出结果: True#topTen: Logical Not and Or#top One: lambda expression

General operation, left to right, assignment operation from right to left .

1+3>>> a4

Parentheses Precedence Action

>>> B = (1+2) *3>>> b9

Python Learning op-expression precedence

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.