Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python operator


May 10, 2021 Python2


Table of contents


Python operator


What is an operator?

This section focuses on Python's operators. T o give a simple example, 4 plus 5 plus 9 . In the example, 4 and 5 are called operans, and the "plus" number is the operator.

The Python language supports the following types of operators:

Let's learn Python's operators one by one.


Python arithmetic operator

The following assumes that the variable a is 10 and the variable b is 20:

Operator Describe Instance
+ Plus - Two objects add up The output result of a plus b is 30
- Subtract - Get a negative number or one number minus another number a - b Output -10
* Multiply - Multiply two numbers or return a string that is repeated several times a :b Output 200
/ Divide - x divided by y b / a Output 2
% Molding - Returns the remaining part of the divide b % a Output 0
** Power - Returns the y-power of x The a-b is 20th party of 10, and the output result is 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// Rounding and divide - returns the integer portion of the dealer 9//2 Output 4, 9.0//2.0 Output 4.0

The following example demonstrates the operation of all Python arithmetic operators:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

c = a + b
print "1 - c 的值为:", c

c = a - b
print "2 - c 的值为:", c 

c = a * b
print "3 - c 的值为:", c 

c = a / b
print "4 - c 的值为:", c 

c = a % b
print "5 - c 的值为:", c

# 修改变量 a 、b 、c
a = 2
b = 3
c = a**b 
print "6 - c 的值为:", c

a = 10
b = 5
c = a//b 
print "7 - c 的值为:", c
Try it out . . .


The above example output results:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2

Note: I n Python 2.x, an integer can only be derived by divided by an integer. I f you want to get a small part, change one of the numbers to floating points.

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

Python comparison operator

The following assumes that the variable a is 10 and the variable b is 20:

Operator Describe Instance
== Equal to - Compare whether the object is equal False is returned.
!= Not equal to - Compare whether two objects are not equal (a!-b) returns true.
<> Not equal to - Compare whether two objects are not equal (a. slt;b) returns true. This operator is similar to ! .
> Greater than - returns whether x is greater than y (a) Return false.
< Less than - returns whether x is less than y. A ll comparison operators return 1 for true and 0 for false. T his is equivalent to the special variables True and False, respectively. Note that the names of these variables are capitaled. (a- lt; b) returns true.
>= Greater than or equal to - returns whether x is greater than or equal to y. (a) return false.
<= Less than or equal to - returns whether x is less than or equal to y. (a . .

The following example demonstrates the operation of all Python comparison operators:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

if ( a == b ):
   print "1 - a 等于 b"
else:
   print "1 - a 不等于 b"

if ( a != b ):
   print "2 - a 不等于 b"
else:
   print "2 - a 等于 b"

if ( a <> b ):
   print "3 - a 不等于 b"
else:
   print "3 - a 等于 b"

if ( a < b ):
   print "4 - a 小于 b" 
else:
   print "4 - a 大于等于 b"

if ( a > b ):
   print "5 - a 大于 b"
else:
   print "5 - a 小于等于 b"

# 修改变量 a 和 b 的值
a = 5;
b = 20;
if ( a <= b ):
   print "6 - a 小于等于 b"
else:
   print "6 - a 大于  b"

if ( b >= a ):
   print "7 - b 大于等于 a"
else:
   print "7 - b 小于 a"

The above example output results:

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

Python assignment operator

The following assumes that the variable a is 10 and the variable b is 20:

Operator Describe Instance
= A simple assignment operator C s a s b assigns the result of the operation of a sb to c
+= The addition assignment operator C s a is equivalent to c s c s a
-= Subtract the assignment operator c --a is equivalent to c-c-a
*= Multiplication assignment operator c s a is equivalent to c s c s a
/= Division assignment operator c /-a is equivalent to c-c/a
%= The mold assignment operator c %=a is equivalent to c=c %a
**= Power assignment operator c s a is equivalent to c s c s a
//= Round and divide the assignment operator c //-a is equivalent to c-c /a

The following example demonstrates the operation of all Python assignment operators:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

c = a + b
print "1 - c 的值为:", c

c += a
print "2 - c 的值为:", c 

c *= a
print "3 - c 的值为:", c 

c /= a 
print "4 - c 的值为:", c 

c = 2
c %= a
print "5 - c 的值为:", c

c **= a
print "6 - c 的值为:", c

c //= a
print "7 - c 的值为:", c

The above example output results:

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

Python bit operator

Bit operators are calculated by treating numbers as binary. The bit-by-bit algorithm in Python is as follows:

Operator Describe Instance
& Bits and operators (a and b) Output result 12, binary explanation: 0000 1100
| By bit or operator (a| b) Output 61, binary explanation: 0011 1101
^ By bit difference or operator Output result 49, binary explanation: 0011 0001
~ Take the inverse operator by bit (-a) Output -61, Binary Explanation: 1100 0011, in the form of a complement to a signed binary number.
<< Move the operator to the left a .lt;2 Output 240, Binary Explanation: 1111 0000
>> Move the operator to the right a .gt;2 Output Result 15, Binary Explanation: 0000 1111

The following example demonstrates the operation of all Python bit operators:

#!/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 - c 的值为:", c

c = a | b;        # 61 = 0011 1101 
print "2 - c 的值为:", c

c = a ^ b;        # 49 = 0011 0001
print "3 - c 的值为:", c

c = ~a;           # -61 = 1100 0011
print "4 - c 的值为:", c

c = a << 2;       # 240 = 1111 0000
print "5 - c 的值为:", c

c = a >> 2;       # 15 = 0000 1111
print "6 - c 的值为:", c

The above example output results:

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

Python logical operator

The Python language supports logical operators, assuming that the variable a is 10 and b is 20:

Operator Logical expression Describe Instance
and x and y Boolean "and" - If x is False, x and y return False, otherwise it returns the calculated value of y. (a and b) return 20.
or x or y Boolean "or" - If x is non-0, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.
not not x Boolean "no" - If x is True, false is returned. If x is False, it returns True. Not (a and b) returns False

The above example output results:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 10
b = 20

if ( a and b ):
   print "1 - 变量 a 和 b 都为 true"
else:
   print "1 - 变量 a 和 b 有一个不为 true"

if ( a or b ):
   print "2 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:
   print "2 - 变量 a 和 b 都不为 true"

# 修改变量 a 的值
a = 0
if ( a and b ):
   print "3 - 变量 a 和 b 都为 true"
else:
   print "3 - 变量 a 和 b 有一个不为 true"

if ( a or b ):
   print "4 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:
   print "4 - 变量 a 和 b 都不为 true"

if not( a and b ):
   print "5 - 变量 a 和 b 都为 false,或其中一个变量为 false"
else:
   print "5 - 变量 a 和 b 都为 true"

The above example output results:

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

Python member operator

In addition to some of the above operators, Python supports member operators, and the test instance contains a series of members, including strings, lists, or metagroups.

Operator Describe Instance
in If a value is found in the specified sequence to return True, otherwise It is returned to False. x in the y sequence, if x returns True in the y sequence.
not in If no value is found in the specified sequence to return True, otherwise It returns False. x is not in the y sequence, if x is not returned in the y sequence.

The following example demonstrates the operation of all Python member operators:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
   print "1 - 变量 a 在给定的列表中 list 中"
else:
   print "1 - 变量 a 不在给定的列表中 list 中"

if ( b not in list ):
   print "2 - 变量 b 不在给定的列表中 list 中"
else:
   print "2 - 变量 b 在给定的列表中 list 中"

# 修改变量 a 的值
a = 2
if ( a in list ):
   print "3 - 变量 a 在给定的列表中 list 中"
else:
   print "3 - 变量 a 不在给定的列表中 list 中"

The above example output results:

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

Python identity operator

Identity operators are used to compare storage units for two objects

Operator Describe Instance
is Is is to determine whether two identifiers are referenced from one object x is y, if id(x) is equal to id(y), is returns result 1
is not Is not is to determine whether two identifiers are referenced from different objects x is not y, if id(x) is not equal to id(y). is not returns result 1

The following example demonstrates the operation of all Python identity operators:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 20
b = 20

if ( a is b ):
   print "1 - a 和 b 有相同的标识"
else:
   print "1 - a 和 b 没有相同的标识"

if ( id(a) == id(b) ):
   print "2 - a 和 b 有相同的标识"
else:
   print "2 - a 和 b 没有相同的标识"

# 修改变量 b 的值
b = 30
if ( a is b ):
   print "3 - a 和 b 有相同的标识"
else:
   print "3 - a 和 b 没有相同的标识"

if ( a is not b ):
   print "4 - a 和 b 没有相同的标识"
else:
   print "4 - a 和 b 有相同的标识"

The above example output results:

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

Python operator priority

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

Operator Describe
** Index (highest priority)
~ + - Flip by bit, dollar plus sign and minus sign (the last two methods are named s and - s)
* / % // Multiply, divide, mold and round
+ - Addition and subtract
>> << Move right, move left operator
& Bit 'AND'
^ | Bit operator
<= < > >= Comparison operator
<> == != Equal to the operator
= %= /= //= -= += *= **= The assignment operator
is is not Identity operator
in not in The member operator
not or and The logical operator

The following example demonstrates the operation of all Python operator priorities:

#!/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 运算结果为:",  e

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print "((a + b) * c) / d 运算结果为:",  e

e = (a + b) * (c / d);    # (30) * (15/5)
print "(a + b) * (c / d) 运算结果为:",  e

e = a + (b * c) / d;      #  20 + (150/5)
print "a + (b * c) / d 运算结果为:",  e

The above example output results:

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