Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Operators

Python Operators


What is an operator?

This chapter mainly describes Python's operators. Take a simple example: ** 4 + 5 = 9 . In the example,4 and 5 are calledoperands**, and + is called the operator.

The Python language supports the following types of operators:

Next let's learn Python's operators one by one.


Python arithmetic operators

Assume the following variables: ** a=10, b=20**:

operator Description Example
+ Add - adds two objects together a + b outputs 30
- Subtract - yields a negative number or subtracts one number from another a - b outputs -10
* Multiply - multiplies two numbers or returns a string repeated several times a * b outputs 200
/ Division - x divided by y b / a outputs 2
% Modulo - returns the remainder of a division b % a outputs 0
** Exponent - returns x raised to the power of y a**b is 10 to the 20th power, outputting 100000000000000000000
// Floor division - returns the integer part of the quotient (rounding down)
>>> 9//2

4 >>> -9//2 -5 |

The following example demonstrates all of Python's arithmetic operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

21

b

=

10

c

=

0

c

=

a

b

print

"

1 - the value of c is:

"

,

c

c

=

a

b

print

"

2 - the value of c is:

"

,

c

c

=

a

b

print

"

3 - the value of c is:

"

,

c

c

=

a

/

b

print

"

4 - the value of c is:

"

,

c

c

=

a

%

b

print

"

5 - the value of c is:

"

,

c

Modify variables a, b, and c

a

=

2

b

=

3

c

=

a

**

b

print

"

6 - the value of c is:

"

,

c

a

=

10

b

=

5

c

=

a

//

b

print

"

7 - the value of c is:

"

,

c

Run Example »

The example above produces the following output:


1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2

Note:In Python 2.x, integer divided by integer yields only an integer. To get a decimal part, simply change one of the numbers to a floating point number.


>>> 1/2 0 >>> 1.0/2 0.5 >>> 1/float(2) 0.5


Python Comparison Operators

Assume variable a is 10 and variable b is 20 below:

operator Description Example
== Equal - checks whether objects are equal (a == b) returns False.
!= Not equal - checks whether two objects are different (a != b) returns True.
<> Not equal - compares whether two objects are unequal. Deprecated in Python 3. (a <<> b) returns True. This operator is similar to !=.
> Greater than - returns whether x is greater than y (a > b) returns False.
< Less than - returns whether x is less than y. All comparison operators return 1 for true and 0 for false. These are equivalent to the special variables True and False, respectively. (a < b) returns True.
>= Greater than or equal - returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal - returns whether x is less than or equal to y. (a <= b) returns True.

The following example demonstrates all of Python's comparison operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

21

b

=

10

c

=

0

if

a

==

b

:

print

"

1 - a equals b

"

else

:

print

"

1 - a does not equal b

"

if

a

!=

b

:

print

"

2 - a does not equal b

"

else

:

print

"

2 - a equals b

"

if

a

<>

b

:

print

"

3 - a is not equal to b

"

else

:

print

"

3 - a is equal to b

"

if

a

<

b

:

print

"

4 - a is less than b

"

else

:

print

"

4 - a is greater than or equal to b

"

if

a

b

:

print

"

5 - a is greater than b

"

else

:

print

"

5 - a is less than or equal to b

"

Modify the values of variables a and b

a

=

5

b

=

20

if

a

<=

b

:

print

"

6 - a is less than or equal to b

"

else

:

print

"

6 - a is greater than b

"

if

b

=

a

:

print

"

7 - b is greater than or equal to a

"

else

:

print

"

7 - b is less than a

"

The example above produces the following output:


1 - a 不等于 b
2 - a 不等于 b
3 - a 不等于 b
4 - a 大于等于 b
5 - a 大于 b
6 - a 小于等于 b
7 - b 大于等于 a

Python assignment operators

Assume variable a is 10 and variable b is 20 below:

operator Description Example
= Simple assignment operator c = a + b assigns the result of a + b to c
+= Addition assignment operator c += a is equivalent to c = c + a
-= Subtraction assignment operator c -= a is equivalent to c = c - a
*= Multiplication assignment operator c *= a is equivalent to c = c * a
/= Division assignment operator c /= a is equivalent to c = c / a
%= Modulo assignment operator c %= a is equivalent to c = c % a
**= Exponent assignment operator c **= a is equivalent to c = c ** a
//= Floor division assignment operator c //= a is equivalent to c = c // a

The following example demonstrates all of Python's assignment operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

21

b

=

10

c

=

0

c

=

a

b

print

"

1 - the value of c is:

"

,

c

c

+=

a

print

"

2 - the value of c is:

"

,

c

c

*=

a

print

"

3 - the value of c is:

"

,

c

c

/=

a

print

"

4 - the value of c is:

"

,

c

c

=

2

c

%=

a

print

"

5 - the value of c is:

"

,

c

c

**=

a

print

"

6 - the value of c is:

"

,

c

c

//=

a

print

"

7 - the value of c is:

"

,

c

The example above produces the following output:


1 - c 的值为: 31
2 - c 的值为: 52
3 - c 的值为: 1092
4 - c 的值为: 52
5 - c 的值为: 2
6 - c 的值为: 2097152
7 - c 的值为: 99864

Python bitwise operators

Bitwise operators treat numbers as binary. Python's bitwise operator rules are as follows:

In the following table, variable a is 60 and b is 13, in binary format as follows:


a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011
operator Description Example
& Bitwise AND: for two values, if both corresponding bits are 1 the resulting bit is 1, otherwise 0 (a & b) outputs 12, binary: 0000 1100
| Bitwise OR: if either of the two corresponding bits is 1, the resulting bit is 1. (a | b) outputs 61, binary: 0011 1101
^ Bitwise XOR: when the two corresponding bits differ, the result is 1 (a ^ b) outputs 49, binary: 0011 0001
~ Bitwise NOT operator: inverts every bit of the binary representation, changing 0 to 1 and 1 to 0. ~x is similar to -x-1. (~a) outputs -61. Binary explanation: 1100 0011 (in two's complement form), the two's complement form of a signed binary number.
<< Left shift operator: shifts all the binary digits of the operand to the left by a number of places, determined by << the number on the right, which specifies the number of places to shift. High bits are discarded and low bits are filled with 0. a << 2 outputs 240, binary: 1111 0000
>> Right shift operator: shifts all the binary digits of the operand to the left of '>>' to the right by a number of places. The number to the right of >> specifies the number of places to shift. a >> 2 outputs 15, binary: 0000 1111

The following example demonstrates all of Python's bitwise operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

60

60 = 0011 1100

b

=

13

13 = 0000 1101

c

=

0

c

=

a

&

b

;

12 = 0000 1100

print

"

1 - the value of c is:

"

,

c

c

=

a

|

b

;

61 = 0011 1101

print

"

2 - the value of c is:

"

,

c

c

=

a

^

b

;

49 = 0011 0001

print

"

3 - the value of c is:

"

,

c

c

= ~

a

;

-61 = 1100 0011

print

"

4 - the value of c is:

"

,

c

c

=

a

<<

2

;

240 = 1111 0000

print

"

5 - the value of c is:

"

,

c

c

=

a

2

;

15 = 0000 1111

print

"

6 - the value of c is:

"

,

c

The example above produces the following output:


1 - c 的值为: 12
2 - c 的值为: 61
3 - c 的值为: 49
4 - c 的值为: -61
5 - c 的值为: 240
6 - c 的值为: 15

Python logical operators

Python supports logical operators; assume variable a is 10 and b is 20 below:

operator Logical expression Description Example
and x and y Boolean 'AND' - if x is False, x and y returns False; otherwise, it returns the computed value of y. (a and b) returns 20.
or x or y Boolean 'OR' - if x is non-zero, it returns the computed value of x; otherwise, it returns the computed value of y. (a or b) returns 10.
not not x Boolean "not" - if x is True, returns False; if x is False, returns True. not(a and b) returns False

The example above produces the following output:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

10

b

=

20

if

a

and

b

:

print

"

1 - both variables a and b are True

"

else

:

print

"

1 - one of the variables a and b is not True

"

if

a

or

b

:

print

"

2 - both variables a and b are True, or one of them is True

"

else

:

print

"

2 - neither variable a nor b is True

"

Modify the value of variable a

a

=

0

if

a

and

b

:

print

"

3 - both variables a and b are True

"

else

:

print

"

3 - one of the variables a and b is not True

"

if

a

or

b

:

print

"

4 - both variables a and b are True, or one of them is True

"

else

:

print

"

4 - neither variable a nor b is True

"

if

not

(

a

and

b

)

:

print

"

5 - both variables a and b are False, or one of them is False

"

else

:

print

"

5 - both variables a and b are True

"

The example above produces the following output:


1 - 变量 a 和 b 都为 True
2 - 变量 a 和 b 都为 True,或其中一个变量为 True
3 - 变量 a 和 b 有一个不为 True
4 - 变量 a 和 b 都为 True,或其中一个变量为 True
5 - 变量 a 和 b 都为 False,或其中一个变量为 False

Python membership operators

In addition to the operators above, Python also supports membership operators, which test whether a sequence contains members, including strings, lists, or tuples.

operator Description Example
in Returns True if the value is found in the specified sequence, otherwise False. x is in sequence y; returns True if x is in sequence y.
not in Returns True if the value is not found in the specified sequence, otherwise False. x is not in sequence y; returns True if x is not in sequence y.

The following example demonstrates all of Python's membership operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

10

b

=

20

list

=

[

1

,

2

,

3

,

4

,

5

]

;

if

(

a

in

list

)

:

print

"

1 - variable a is in the given list

"

else

:

print

"

1 - variable a is not in the given list

"

if

(

b

not

in

list

)

:

print

"

2 - variable b is not in the given list

"

else

:

print

"

2 - variable b is in the given list

"

Modify the value of variable a

a

=

2

if

(

a

in

list

)

:

print

"

3 - variable a is in the given list

"

else

:

print

"

3 - variable a is not in the given list

"

The example above produces the following output:


1 - 变量 a 不在给定的列表中 list 中
2 - 变量 b 不在给定的列表中 list 中
3 - 变量 a 在给定的列表中 list 中

Python identity operators

Identity operators compare the memory locations of two objects

operator Description Example
is is checks whether two identifiers refer to the same object x is y, similar to id(x) == id(y) , returns True if they reference the same object, otherwise False
is not is not checks whether two identifiers refer to different objects ** x is not y** , similar to id(a) != id(b). Returns True if they do not reference the same object, otherwise False.

Note: id() The function is used to get an object's memory address.

The following example demonstrates all of Python's identity operators:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

20

b

=

20

if

(

a

is

b

)

:

print

"

1 - a and b have the same identity

"

else

:

print

"

1 - a and b do not have the same identity

"

if

(

a

is

not

b

)

:

print

"

2 - a and b do not have the same identity

"

else

:

print

"

2 - a and b have the same identity

"

Modify the value of variable b

b

=

30

if

(

a

is

b

)

:

print

"

3 - a and b have the same identity

"

else

:

print

"

3 - a and b do not have the same identity

"

if

(

a

is

not

b

)

:

print

"

4 - a and b do not have the same identity

"

else

:

print

"

4 - a and b have the same identity

"

The example above produces the following output:


1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识

Difference between is and ==:

is is used to determine whether two variables refer to the same object (the same memory space), while == is used to determine whether the values of the referenced variables are equal.


>>> a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True


Python operator precedence

The following table lists all operators from highest to lowest precedence:

operator Description
** Exponentiation (highest precedence)
~ + - Bitwise inversion, unary plus and minus (the method names for the last two are +@ and -@)
* / % // Multiplication, division, modulo and floor division
+ - Addition and subtraction
>> << Right shift and left shift operators
& Bitwise 'AND'
^ | Bitwise operators
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in member operators
not and or Logical operators

The following example demonstrates operator precedence in Python:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

a

=

20

b

=

10

c

=

15

d

=

5

e

=

0

e

=

(

a

b

)

c

/

d

#( 30 * 15 ) / 5

print

"

(a + b) * c / d evaluates to:

"

,

e

e

=

(

(

a

b

)

c

)

/

d

(30 * 15 ) / 5

print

"

((a + b) * c) / d evaluates to:

"

,

e

e

=

(

a

b

)

(

c

/

d

)

;

(30) * (15/5)

print

"

(a + b) * (c / d) evaluates to:

"

,

e

e

=

a

(

b

c

)

/

d

;

20 + (150/5)

print

"

a + (b * c) / d evaluates to:

"

,

e

The example above produces the following output:


(a + b) * c / d 运算结果为: 90
((a + b) * c) / d 运算结果为: 90
(a + b) * (c / d) 运算结果为: 90
a + (b * c) / d 运算结果为: 50