Latest web development tutorials

Python3 operator

What is the operator?

This section mainly explained Python operators. Here is a simple example of 4 + 5 = 9. Example, 4 and 5 are called operands, "+" operator is called.

Python language supports the following types of operators:

Let one operator to learn Python.


Python Arithmetic Operators

The assumption that a variable is 10, the variable b is 21:

Operators description Examples
+ Plus - two objects are added a + b output 31
- Save - get a negative number is subtracted from another number or a - b Output -11
* Multiply - multiply two numbers or returns the string repeated several times a * b output 210
/ In addition - x divided by y b / a 2.1 output
% Modulo - returns the division remainder b% a Output 1
** Power - returns x raised to the power of y a ** b 10 21 th
// Take divisible - Returns the integer part of quotient 9 // 2 output 4, output 9.0 // 2.0 4.0

The following example demonstrates Python all arithmetic operators in action:

#!/usr/bin/python3

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)

Examples of the above output:

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

Python comparison operators

The following assumptions variable a is 10, b is variable 20:

Operators description Examples
== Equal - compare objects for equality (A == b) returns False.
! = It is not equal - compare two objects are not equal (A! = B) returns true.
> 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. This respectively special variables True and False equivalence. Note that these variable name capitalization. (A <b) returns true.
> = Greater than or equal - Returns whether x is greater than or equal y. (A> = b) returns False.
<= Less than or equal - Returns whether x is less than or equal y. (A <= b) returns true.

The following example illustrates the comparison of all Python operations:

#!/usr/bin/python3

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")

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

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

Examples of the above output:

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

Python assignment operator

The following assumptions variable a is 10, b is variable 20:

Operators description Examples
= Simple assignment operator c = a + b a + b operation will assign the result 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 equivalent to c * = a 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
** = Exponentiation assignment operator c ** = a is equivalent to c = c ** a
// = Take divisible assignment operator c // = a is equivalent to c = c // a

The following example demonstrates the assignment operator of all Python operations:

#!/usr/bin/python3

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)

Examples of the above output:

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

Python Bitwise Operators

Bitwise operators are considered binary numbers to make the calculation. Python is Bitwise rule as follows:

As a variable in the following table 60, b 13.

Operators description Examples
& Bitwise and operator: two values ​​involved in operation, if both corresponding bits are 1, the result bit is 1, 0 otherwise (A & b) output 12, binary explanation: 0000 1100
| Bitwise or operator: as long as the two corresponding binary bit is a 1, the resulting bit is 1. (A | b) output 61, binary explanation: 00111101
^ Bitwise exclusive OR operator: When the two corresponding binary bit different and the result is 1 (A ^ b) the output of 49, binary explanation: 00110001
~ Bitwise operators: each binary data bit inversion, that is 1 to 0, the 0 to 1 (~ A) -61 output, binary explanation: 1100 0011 in a symbolic form complement binary number.
<< Left mobile operators: each binary operands all left a number of bits specified by the number "<<" right of the decimal moves, discarding the high and lower 0s. a << 2 outputs 240 Binary interpretation: 11110000
>> Right Mobile operators: to each binary ">>" the left operand Several all right, ">>" on the right the number of bits specified a >> 2 output 15, binary explanation: 0000 1111

The following example demonstrates Python all bitwise operations:

#!/usr/bin/python3

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)

Examples of the above output:

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

Python logical operators

Python language supports logical operators, the assumption that a variable is 10, b 20:

Operators Logical expression description Examples
and x and y Boolean "and" - if x is False, x and y returns False, else it returns evaluation of y. (A and b) returns 20.
or x or y Boolean "or" - If x is True, it returns True, else it returns evaluation of y. (A or b) returns 10.
not not x Boolean "not" - If x is True, it returns False. If x is False, it returns True. not (a and b) returns False

Examples of the above output:

#!/usr/bin/python3

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")

Examples of the above 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 member operator

In addition to some of the above operators, Python also supports member operator, test case contains a number of members, including strings, lists or tuples.

Operators description Examples
in If you find the value in the specified sequence returns True, otherwise False. x in y sequence, returns True if x in y sequence.
not in If the value is not found in the specified sequence returns True, otherwise False. x is not y sequence, if x is not y sequence returns True.

The following example demonstrates all the members of the Python operator actions:

#!/usr/bin/python3

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 中")

Examples of the above output:

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

Python identity operator

Identity operator is used to compare two objects of storage units

Operators description Examples
is is judging the two identifiers are not referenced from an object x is y, if id (x) equalsid (y), is returned 1 results
is not It is not to judge the two identifiers are not referenced from different objects x is not y, if id (x) is not equal toid (y). is not returned 1 results

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

#!/usr/bin/python3

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 有相同的标识")

Examples of the above output:

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

Python operator precedence

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

Operators description
** Index (highest priority)
~ + - Bitwise inversion, unary plus and minus (the last two method named + and @ - @)
* /% // Multiplication, division, and modulo take divisible
+ - Addition Subtraction
>> << Right, left operator
& Bit 'AND'
^ | Bitwise Operators
<= <>> = Comparison
<> ==! = Equality operator
=% = / = @ = - = + = * = * = Assignment Operators
is is not Identity operator
in not in Member operator
not or and Logical Operators

The following example demonstrates all the Python operator precedence action:

#!/usr/bin/python3

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)

Examples of the above output:

(a + b) * c / d 运算结果为: 90.0
((a + b) * c) / d 运算结果为: 90.0
(a + b) * (c / d) 运算结果为: 90.0
a + (b * c) / d 运算结果为: 50.0