2.3.2 Operator Precedence

Source: Internet
Author: User
Tags bitwise
1, what is the priority of Python 2, Python priority use combat
The role of priority
The execution of a program or operator in Python is sequential, such as A and B, and if a can take precedence over B, then the priority of a is higher than B, and B has a lower priority than a. Where A and B can be operators or programs. That is, there are two types of precedence in Python, one is the priority between programs, and one is the precedence between operators.
#-*-coding:utf-8-*-
__author__ =' Allen '

Priority Use Combat

Priority list 1--function call, addressing, subscript

Priority List 2--Power operation * *
A=4*2**3
PrintA

Priority list 3--Flip Operation ~ #其实翻转和 +,-peer, but flip must be written in the data before, but to flip the operation
Print~18**2 #等于-325, power operation First ~ (18**2)

Priority List No. 4--plus sign
Print2+4*-2
Print4*-2**2 #幂运算优先 > Minus > Multiplication

priority list of the first rank5name--' * ','/','%'(the remainder of the business),'//'(Take the quotient of the whole number)
Print2+4*2/4

Priority List 6--'+''-'
Print3<<2+1

Priority List 7--' << '' >> '
Print18<<1 #18 *2**n,n=1

priority list of the first rank8name--Bitwise AND' & ' >per-Bitwise XOR' ^ ' >by bit or' | '
Example: 3|9^7&18
Print(3|9^7&18) #11 (3| ( 9^ (7&AMP;18)) priority Operation 7&18=2 > 9^2=11 > 3|11=11
Print(((3|9) ^7) &18) #0 3|9=11 > 11^7=12 > 12&18=0

priority list of the first rank9name--comparison Operators' <= ', ' < ', ' > ', ' >= '
A=2*3+5<=5+1*2
PrintA

Priority list 10--Logical' not '' and '' or '

There should also be an assignment identifier (= %= /= //= -= += *= **=);
Flag Identifier(is isn't);
member Identifiers(in);
Detailed view of thePythonBasic Operators Documenthttp://www.yiibai.com/python/python_basic_operators.html


Priority list 11--lambda expressions
Examples of lambda expressions:
#!/usr/bin/envpython
li=[{"Age": 20,"Name":"Def"},{"Age": 25,"Name":"ABC"},{"Age": 10,"Name":"Ghi"}]
Li=sorted (li,key=Lambdax:x["Age"])
Print(LI)

If you don't use a lambda expression and write a regular function, you need to do this:
#!/usr/bin/envpython
defComp (x):
returnx["Age"]
li=[{"Age": 20,"Name":"Def"},{"Age": 25,"Name":"ABC"},{"Age": 10,"Name":"Ghi"}]
Li=sorted (Li,key=comp)
Print(LI)

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.