Python - 基本操作符

Python - 基本操作符 首页 / Python2入门教程 / Python - 基本操作符

运算符是可以操纵操作数值的构造,考虑表达式4 + 5 =9。这里,4和5称为操作数,而+称为运算符。

Python语言支持以下类型的运算符。

  • 算术运算符
  • 比较运算符
  • 分配运算符
  • 逻辑运算符
  • 按位运算符

让无涯教程一一看一下所有运算符。

链接:https://www.learnfk.comhttps://www.learnfk.com/python/python-basic-operators.html

来源:LearnFk无涯教程网

算术运算符

假设变量a=10,变量b=20,则-

运算符描述示例
+相加 a + b=30
-相减 a – b=-10
*相乘 a * b=200
/相除 b/a=2
整除后的余数 b%a=0
**指数(幂)计算 a ** b=10的20次幂
//整除 9 //2=4和9.0 //2.0=4.0

假设变量a=21而变量b=10,则-

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c = a - b
print "Line 2 - Value of c is ", c 

c = a * b
print "Line 3 - Value of c is ", c 

c = a / b
print "Line 4 - Value of c is ", c 

c = a % b
print "Line 5 - Value of c is ", c

a = 2
b = 3
c = a**b 
print "Line 6 - Value of c is ", c

a = 10
b = 5
c = a//b 
print "Line 7 - Value of c is ", c

当您执行上述程序时,它将产生以下结果-

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

比较运算符

这些运算符比较它们两侧的值并确定它们之间的关系。它们也称为关系运算符,假设变量a=10,变量b=20,则-

运算符描述示例
==相等(a == b) is not true.
!=不相等(a != b) is true.
<>不相等(a <> b) is true. This is similar to != operator.
>大于(a > b) is not true.
<小于(a < b) is true.
>=大于或等于(a >= b) is not true.
<=小于或等于(a <= b) is true.

假设变量a=10,变量b=20,则-

#!/usr/bin/python

a = 21
b = 10
c = 0

if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"

if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"

if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"

if ( a < b ):
   print "Line 4 - a is less than b" 
else:
   print "Line 4 - a is not less than b"

if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"

a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"

if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"

当您执行上述程序时,它将产生以下结果-

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b

赋值运算符

假设变量a=10,变量b=20,则-

运算符描述示例
=直接赋值 c=a + b将a + b的值分配给c
+=先相加,再赋值 c +=a -》c=c + a
-=先相减,再赋值 c-= a -》c=c-a
* =先相乘,再赋值 c *=a -》c=c * a
/=先相除,再赋值 c/= a -》c=c/ac/= a -》c=c/a
%=先取模,再赋值 c%= a -》c=c%a
**=先运算,再赋值 c **=a  -》c=c ** a
//=先整除,再赋值 c //= a -》c=c //a

假设变量a=10,变量b=20,则-

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c += a
print "Line 2 - Value of c is ", c 

c *= a
print "Line 3 - Value of c is ", c 

c /= a 
print "Line 4 - Value of c is ", c 

c  = 2
c %= a
print "Line 5 - Value of c is ", c

c **= a
print "Line 6 - Value of c is ", c

c //= a
print "Line 7 - Value of c is ", c

当您执行上述程序时,它将产生以下结果-

Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864

按位运算符

按位运算符对位进行运算并执行逐位运算。假设a=60;并且b=13;现在以二进制格式,它们将如下所示-

a=0011 1100

b=0000 1101

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

a&b=0000 1100

a | b=0011 1101

a ^ b=0011 0001

a=1100 0011

Python语言支持以下Bitwise运算符

Operator描述示例
和(and)(a & b) (means 0000 1100)
或(or)(a | b)=61 (means 0011 1101)
非(not)(a ^ b)=49 (means 0011 0001)
异或(~a )=-61 (means 1100 0011 in 2's complement form due to a signed binary number.
<<左移a << 2=240 (means 1111 0000)
>> 右移a >> 2=15 (means 0000 1111)
#!/usr/bin/python

a = 60            # 60=0011 1100 
b = 13            # 13=0000 1101 
c = 0

c = a & b;        # 12=0000 1100
print "Line 1 - Value of c is ", c

c = a | b;        # 61=0011 1101 
print "Line 2 - Value of c is ", c

c = a ^ b;        # 49=0011 0001
print "Line 3 - Value of c is ", c

c = ~a;           # -61=1100 0011
print "Line 4 - Value of c is ", c

c = a << 2;       # 240=1111 0000
print "Line 5 - Value of c is ", c

c = a >> 2;       # 15=0000 1111
print "Line 6 - Value of c is ", c

当您执行上述程序时,它将产生以下结果-

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

逻辑运算符

Python语言支持以下逻辑运算符。假设变量a=10,变量b=20

运算符描述示例
and如果两个操作数都为true,则条件为true。(a and b)   is true
or如果两个操作数中的任何一个都不为零,则条件为true。(a or b) is true
not 用于反转其操作数的逻辑状态。not (a and b) is false

Membership运算符

Python的Membership运算符会测试序列中的成员运算,如字符串,列表或元组。

运算符描述示例
in在...中 x在y中,如果x是序列y的成员,则输出为1。
not in 不在...中 x不在y中,如果x不是序列y的成员,则此处t不会为1。
#!/usr/bin/python

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

if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"

if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"

a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"

当您执行上述程序时,它将产生以下结果-

Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list

Identity 运算符

Identity 运算符比较两个对象的内存位置。

运算符描述示例
is如果运算符两侧的变量指向同一对象,则为true;否则为false。 x是y,如果id(x)等于id(y),则 is 的输出为1。
is not如果运算符两侧的变量指向同一个对象,则输出为false,否则为true。 x不是y,如果id(x)不等于id(y),则不是输出为1。
#!/usr/bin/python

a = 20
b = 20

if ( a is b ):
   print "Line 1 - a and b have same identity"
else:
   print "Line 1 - a and b do not have same identity"

if ( id(a) == id(b) ):
   print "Line 2 - a and b have same identity"
else:
   print "Line 2 - a and b do not have same identity"

b = 30
if ( a is b ):
   print "Line 3 - a and b have same identity"
else:
   print "Line 3 - a and b do not have same identity"

if ( a is not b ):
   print "Line 4 - a and b do not have same identity"
else:
   print "Line 4 - a and b have same identity"

当您执行上述程序时,它将产生以下结果-

Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity

运算符优先级

下表列出了从最高优先级到最低优先级的所有运算符。

Sr.No.Operator & Remark
1

**

求幂(提高幂)

2

〜+-

补码,一元加号和减号(最后两个的方法名称为+ @和-@)

3

* /%//

乘,除,模和底除

4

+-

加减

5

>> <<

左右位移

6

&

按位"与"

7

^ |

按位异或或常规OR

8

<= < > >=

比较运算符

9

<> == !=

平等经营者

10

=%=/= //=-= +=*=** =

赋值运算符

11

is not 

Identity 运算符

12

in not in

Membership 运算符

13

not or and 

逻辑运算符

#!/usr/bin/python

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d       #( 30 * 15 )/5
print "Value of (a + b) * c/d is ",  e

e = ((a + b) * c) / d     # (30 * 15 )/5
print "Value of ((a + b) * c)/d is ",  e

e = (a + b) * (c / d);    # (30) * (15/5)
print "Value of (a + b) * (c/d) is ",  e

e = a + (b * c) / d;      #  20 + (150/5)
print "Value of a + (b * c)/d is ",  e

当您执行上述程序时,它将产生以下结果-

Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

机器学习40讲 -〔王天一〕

从0开始学微服务 -〔胡忠想〕

数据中台实战课 -〔郭忆〕

Redis核心技术与实战 -〔蒋德钧〕

技术管理案例课 -〔许健〕

Spring编程常见错误50例 -〔傅健〕

业务开发算法50讲 -〔黄清昊〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

好记忆不如烂笔头。留下您的足迹吧 :)