List of Python operators

Source: Internet
Author: User
Tags bitwise

X+y,x-y
Add, Subtract, "+" can be overloaded as a connector

X*y,x**y,x/y,x%y
Multiply, square, divide, balance, "*" can be overloaded to repeat, "%" number can be overloaded to format

<,<=,>,>=,==,<>,!=
Comparison operators

+=,-=,*=,/=,%=,**=,<<=,>>=,&=,^=,|=
Self-variable operators

X|y
Bitwise OR

X^y
Bitwise XOR OR

X&y
Bitwise-AND

~x
Bitwise REVERSE

X<<,x>>y
X move y-bit left or right

is, are not
Equivalent test

In, not in
Whether to test for members

Or,and,not
logical operators

X[i],x[i:j],x.y,x (...)
Index, Shard, qualified reference, function call

(...),[...],{...},‘...‘
Tuple, list, dictionary, convert to string

2.3.2. Operator precedence
Table 2.2. Operator precedence list (highest to lowest)
operator
Description

' expr '
string conversion

{key:expr,...}
Dictionary

[expr1,expr2 ...]
List

(EXPR1,EXPR2,...)
Tuple

function (expr,...)
Function call

X[index:index]
Slice

X[index]
Subscript index value

X.attribute
Property Reference

~x
Bitwise REVERSE

+x,-x
Positive, negative

X**y
Power

x*y,x/y,x%y
Multiply, divide, modulo

x+y,x-y
Add, subtract

X<<y,x >>y
Shift

X&y
Bitwise with

X^y
Bitwise XOR or

x|y
Bitwise OR

X<y,x<=y,x==y,x!=y,x >=y,x>y
Compare

X is Y,x was not y
equivalent test

X in y,x not in Y
member judgment

not x
Logical no

X and y
Logic with

X or y
logic or

Lambda arg,...: expr
Lambda anonymous function

2.3.3. Truth table
Table 2.3.
Object/constant
Value

""
False

"String"
Really

0
False

>=1
Really

<=-1
Really

() Empty tuple
False

[] Empty list
False

{} Empty Dictionary
False

None
False

2.3.4. Composite expressions
· For and, when a and B are computed, Python calculates a, and if a is false, take a value, and if A is true, Python calculates B and the entire expression
Will take the B-value. Such as:

>>> a,b=10,20

>>> A and B #a is true 20 ·

>>> a,b=0,5 #a is False

>>> A and B. 0 ·

For or, when a or B is computed, Python calculates a, and if A is true, the entire expression takes a value, and if A is false, the expression takes a B-value. Such as:

>>> a,b=10,20

>>> A or B. 10 ·

>>> a,b=0,5

>>> A or B. 5 ·

For Not,not, the "actual value" of the table expression is reversed, and if the expression is true, the not is a false return, if the expression is false, not is the return true. Such as:

· >>> not 2 · False

· >>> not 0 · True

· >>> not ' test ' · False

· >>> not "" • True2.4. Assigning a value to a variable
· Simple assignment, Variable (variable) =value (value).

· >>>a=1

· >>>b=2

· >>>print a,b 1 2 · Multi-variable assignment, variable1,variable2,... =value1,value2,...

· >>>a,b,c=1,2,3

· >>>print A. 1

· >>>print B. 2

· >>>print C. 3 multivariate assignments can also be used for variable exchange, as in the following example:

>>>a,b,c=c,b,a

>>>print A3>>>print B2

>>>print C1 Multi-objective assignment, a=b=variable

· >>> a=b=1

· >>> A. 1

· >>> B. 1

· >>> a=2

· >>> A. 2

· >>> B. 1 · Self-variable assignment, such as +=,-=,*=

List of Python operators

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.