Table of Contents

What are operators in python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

表格内容

  • 在python中什么是运算?
  • 算术运算符
  • 比较(关系)运算符
  • 逻辑(布尔)运算符
  • 位运算符
  • 赋值运算符
  • 特殊运算符
  1. 身份运算符
  2. 成员运算符

在python中什么是运算?

在python中运算是执行算术和逻辑计算的特殊的符号。这个操作符操作的值叫做操作数。


For example:

>>> 2+3
5

Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

例如:

>>> 2+3
5

这里,+在执行加时是操作符。2和3是操作数,5是操作输出的。


Arithmetic operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

Arithmetic operators in Python

Operator

Meaning

Example

+

Add two operands or unary plus

x + y

+2

-

Subtract right operand from the left or unary minus

x - y

-2

*

Multiply two operands

x * y

/

Divide left operand by the right one (always results into float)

x / y

%

Modulus - remainder of the division of left operand by the right

x % y (remainder of x/y)

//

Floor division - division that results into whole number adjusted to the left in the number line

x // y

**

Exponent - left operand raised to the power of right

x**y (x to the power y)

运算操作符

运算操作符是常用于执行数学操作,像加法,减法,乘法等。

python运算符

操作符

意思

例子

+

两个操作数相加或 一元加

x + y

+2

-

左边运算数减右边运算数或一元减

x - y

-2

*

两个运算数相乘

x * y

/

左边操作数除以右边操作数 (结果总是浮点数)

x / y

%

模式 - 左边操作数除以右边操作数的余数

x % y (x/y的余数)

//

取整除- division that results into whole number adjusted to the left in the number line

x // y

**

指数 - 左边操作数的右次方

x**y (x to the power y)


Example #1: Arithmetic operators in Python

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

When you run the program, the output will be:

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

例子1:在python中的算术运算符 


当你运算这个程序是,它的输出结果是:

>>> x = 15
>>> y = 4
>>> print('x + y = ',x+y)
x + y =  19
>>> print('x - y = ',x-y)
x - y =  11
>>> print('x * y =',x*y)
x * y = 60
>>> print('x / y =',x/y)
x / y = 3.75
>>> print('x // y =',x//y)
x // y = 3
>>> print('x ** y =',x**y)
x ** y = 50625

Comparison operators

Comparison operators are used to compare values. It either returns True or False according to the condition.

Comparision operators in Python

Operator

Meaning

Example

>

Greater that - True if left operand is greater than the right

x > y

<

Less that - True if left operand is less than the right

x < y

==

Equal to - True if both operands are equal

x == y

!=

Not equal to - True if operands are not equal

x != y

>=

Greater than or equal to - True if left operand is greater than or equal to the right

x >= y

<=

Less than or equal to - True if left operand is less than or equal to the right

x <= y


比较运算符

 比较运算符常用来比较值。它根据条件要么返回True或False。

python比较运算符

操作符

意思

例子

>

更大的 - 如果左边大于右边的操作数则正确

x > y

<

更小的 - 如果左边小于右边的操作数则正确

x < y

==

等于 - 如果两个操作数相当则正确

x == y

!=

不相当 - 如果两个操作数不相等则正确

x != y

>=

更大或相等的 - 左边大于或等于右边的操作数则正确

x >= y

<=

小于或等于 - 左边小于或等于右边的操作数则正确

x <= y

Example #2: Comparison operators in Python

x = 10
y = 12

# Output: x > y is False
print('x > y  is',x>y)

# Output: x < y is True
print('x < y  is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

例子2:在python中的比较运算符

>>> x = 10
>>> y = 12
>>> print('x > y ',x>y)
x > y  False
>>> print('x < y ',x<y)
x < y  True
>>> print('x == y ',x==y)
x == y  False
>>> print('x >= y ',x>=y)
x >= y  False
>>> print('x <= y ',x<=y)
x <= y  True

Logical operators

Logical operators are the andornot operators.

Logical operators in Python

Operator

Meaning

Example

and

True if both the operands are true

x and y

or

True if either of the operands is true

x or y

not

True if operand is false (complements the operand)

not x

逻辑运算符

逻辑运算符是and,or,not操作符

在python中逻辑运算符

操作符

意思

例子

and

如果两个操作数是正确的则正确

x and y

or

如果两个操作数有一个是正确的则正确

x or y

not

如果操作数是错误的则正确 (补充操作数)

not x


Example #3: Logical Operators in Python

x = True
y = False

# Output: x and y is False
print('x and y is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

Here is the truth table for these operators.

例子3:python的逻辑操作数

>>> x = True
>>> y = False
>>> print('x and y is',x and y)
x and y is False
>>> print('x or y is',x or y)
x or y is True
>>> print('not x is', not x)
not x is False

 这里是操作符的真值表


Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Bitwise operators in Python

Operator

Meaning

Example

&

Bitwise AND

x& y = 0 (0000 0000)

|

Bitwise OR

x | y = 14 (0000 1110)

~

Bitwise NOT

~x = -11 (1111 0101)

^

Bitwise XOR

x ^ y = 14 (0000 1110)

>>

Bitwise right shift

x>> 2 = 2 (0000 0010)

<<

Bitwise left shift

x<< 2 = 40 (0010 1000)

位运算符

 位运算符作用于操作数就好像是二进制字符串。它逐位操作,因此而得名。

在下面表格中:设x = 10 (0000 1010 在二进制)和y = 4(0000 0100 在二进制)

在python位运算符

操作数

意思

例子

&

位和

x& y = 0 (0000 0000)

|

位或

x | y = 14 (0000 1110)

~

位不

~x = -11 (1111 0101)

^

位异或

x ^ y = 14 (0000 1110)

>>

逐位右移

x>> 2 = 2 (0000 0010)

<<

逐位左移

x<< 2 = 40 (0010 1000)


Assignment operators

Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variablea on the left.

There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

Assignment operators in Python

Operator

Example

Equivatent to

=

x = 5

x = 5

+=

x += 5

x = x + 5

-=

x -= 5

x = x - 5

*=

x *= 5

x = x * 5

/=

x /= 5

x = x / 5

%=

x %= 5

x = x % 5

//=

x //= 5

x = x // 5

**=

x **= 5

x = x ** 5

&=

x &= 5

x = x & 5

|=

x |= 5

x = x | 5

^=

x ^= 5

x = x ^ 5

>>=

x >>= 5

x = x >> 5

<<=

x <<= 5

x = x << 5

 赋值操作

在python中赋值操作符用于赋值给变量。

a = 5是简单的赋值操作,在右边的5赋值给左边的变量。

在python中这里有复杂操作符像a += 5,添加到变量中,然后再进行相同的赋值。它是相当于a = a + 5.

python赋值运算

操作符

例子

相当于

=

x = 5

x = 5

+=

x += 5

x = x + 5

-=

x -= 5

x = x - 5

*=

x *= 5

x = x * 5

/=

x /= 5

x = x / 5

%=

x %= 5

x = x % 5

//=

x //= 5

x = x // 5

**=

x **= 5

x = x ** 5

&=

x &= 5

x = x & 5

|=

x |= 5

x = x | 5

^=

x ^= 5

x = x ^ 5

>>=

x >>= 5

x = x >> 5

<<=

x <<= 5

x = x << 5


Special operators

Python language offers some special type of operators like the identity operator or the membership operator. They are described below with examples.

特殊操作符

python语言提供了一下不同类型的操作符像身份运算符或成员运算符。它们描述在下面例子。


Identity operators

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Identity operators in Python

Operator

Meaning

Example

is

操作符是相同的则为True (引用相同的对象)

x is True

is not

操作数不相同为True (引用对象不相同)

x is not True

身份运算符

在python中is和is not是单位运算符 。他们用于检查如果两个值(或变量)是位于相同的内存位置。两个变量相当不意味着他们是相同的。

python身份运算符

操作符

意思

例子

is

True if the operands are identical (refer to the same object)

x is True

is not

True if the operands are not identical (do not refer to the same object)

x is not True


Example #4: Identity operators in Python

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

# Output: False
print(x1 is not y1)

# Output: True
print(x2 is y2)

# Output: False
print(x3 is y3)

Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).

But x3 and y3 are list. They are equal but not identical. It is because interpreter locates them separately in memory although they are equal.

例子4:python的身份运算符

>>> x1 = 5
>>> y1 = 5
>>> x2 = 'Hello'
>>> y2 = 'Hello'
>>> x3 = [1,2,3]
>>> y3 = [1,2,3]
>>> print(x1 is not y1)
False
>>> print(x2 is y2)
True
>>> print(x3 is y3)
False

这里,我们可以看到x1和y1是相同的整数,所以他们的身份是相当的。相同的情况有x2和y2(字符串)。

但是x3和y3是列表。他们是相等的,但是身份不同。尽管他们是相等的,因为他们解释器在内存中分别定位。


Membership operators

in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (stringlisttupleset and dictionary).

In a dictionary we can only test for presence of key, not the value.

Operator

Meaning

Example

in

True if value/variable is found in the sequence

5 in x

not in

True if value/variable is not found in the sequence

5 not in x

成员操作符

in和not in是成员操作符在python中。他们用于测试在一个序列中一个字或是变量是否被找到(字符串,列表,元组,集合和字典)。

在字典中,我们仅仅可以对键测试,不能对值。

操作符

 

意思

例子

in

如果值或变量在序列里边被找到则是True

5 in x

not in

如果值或变量没有在序列被找到则是True

5 not in x


Example #5: Membership operators in Python

x = 'Hello world'
y = {1:'a',2:'b'}

# Output: True
print('H' in x)

# Output: True
print('hello' not in x)

# Output: True
print(1 in y)

# Output: False
print('a' in y)

例子5:python成员运算符 

>>> x = 'Hello world'
>>> y = {1:'a',2:'b'}
>>> print('H' in x)
True
>>> print('hell' not in x)
True
>>> print(1 in y)
True
>>> print('a' in y)
False

Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive). Similary, 1 is key and 'a' is the value in dictionary y. Hence, 'a' in y returns False.

这里,'H' 是在x,'hello'现在不是在x(记住,python 是大小敏感的)。同样的,1是键,在字典y里'a'是值,'a'在y里返回False.


Check out these examples to learn more: