python语言数据类型、运算符和表达式_数据类型--运算符和表达式

python语言支持下列类型的运算符

1、算术运算符

2、比较(关系)运算符

3、赋值运算符

4、位运算符

5、逻辑运算符

6、成员运算符

7、身份运算符

8、运算符优先级

算术运算符

以下假设变量a为10,变量b为20

运算符描述实例

+

加 - 两个对象相加

a + b 输出结果 30

-

减 - 得到负数或是一个数减去另一个数

a - b 输出结果 -10

*

乘 - 两个数相乘或是返回一个被重复若干次的字符串

a * b 输出结果 200

/

除 - x除以y

b / a 输出结果 2

%

取模 - 返回除法的余数

b % a 输出结果 0

**

幂 - 返回x的y次幂

a**b 为10的20次方, 输出结果 100000000000000000000

//

取整除 - 返回商的整数部分

9//2 输出结果 4 , 9.0//2.0 输出结果 4.0

1 #!/usr/bin/python

2 a = 21

3 b = 10

4 c =05

6 c=a +b7 print("Line 1 - value of c is",c)8

9 c=a -b10 print("Line 2 - value of c is",c)11

12 c=a *b13 print("Line 3 - values of c is",c)14

15

16 c=a /b17 print("Line 4 - value of c is",c)18

19

20 c= a %b21 print("Line 5 - value of c is",c)22

23

24 a = 2

25 b = 3

26 c = a**b27 print("Line 6 - value if c is",c)28

29 a = 10

30 b = 5

31 c = a//b32 print("Line 7 - value of c is",c)33

34

35

36 输出结果:37 Line 1 - value of c is 31

38 Line 2 - value of c is 11

39 Line 3 - values of c is 210

40 Line 4 - value of c is 2.1

41 Line 5 - value of c is 1

42 Line 6 - value if c is 8

43 Line 7 - value of c is 2

算术运算符练习

比较运算符

以下假设变量a为10,变量b为20

运算符描述实例

==

等于 - 比较对象是否相等

(a == b) 返回 False。

!=

不等于 - 比较两个对象是否不相等

(a != b) 返回 true.

<>

不等于 - 比较两个对象是否不相等(3.0已经取消)

(a <> b) 返回 true。这个运算符类似 != 。

>

大于 - 返回x是否大于y

(a > b) 返回 False。

<

小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。

(a < b) 返回 true。

>=

大于等于 - 返回x是否大于等于y。

(a >= b) 返回 False。

<=

小于等于 - 返回x是否小于等于y。

(a <= b) 返回 true。

1 a = 21

2 b = 10

3 c =04

5 if ( a ==b ):6 print("Line 1 - a is equal to b")7 else:8 print("Line 1 - a is not equal to b")9

10 if ( a !=b ):11 print("Line 2 - a is not equal to b")12 else:13 print("Line 2 - a is equal to b")14

15 #if ( a <> b ):

16 #print("Line 3 - a is not equal to b")

17 #else:

18 #print("Line 3 - a is equal to b")

19

20

21 if ( a

26

27 if ( a >b ):28 print("Line 5 - a is greater than b")29 else:30 print("Line 5 - a is not greater than b")31

32

33 a = 5

34 b = 20

35 if ( a <=b ):36 print("Line 6 - is either less than or equal to b")37 else:38 print("Line 6 - is neither less than nor equal to b")39

40

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

46

47

48 输出显示:49 Line 1 - a is notequal to b50 Line 2 - a is notequal to b51 Line 4 - a is notless than b52 Line 5 - a isgreater than b53 Line 6 - is either less than orequal to b54 Line 7 - is either greater than orequal to b55

56

57

58 注意:59 在3.0以上的python代码中,去掉了不等于"<>"这个比较运算符。60

61 如下:62

63 if ( a <>b ):64 ^

65 SyntaxError: invalid syntax

比较运算符练习

赋值运算符

以下假设变量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 / a

%=

取模赋值运算符

c %= a 等效于 c = c % a

**=

幂赋值运算符

c **= a 等效于 c = c ** a

//=

取整除赋值运算符

c //= a 等效于 c = c // a

1 #!/usr/bin/python

2 a = 21

3 b = 10

4 c =05

6 c = a +b7 print("Line 1 - value of c is", c)8

9 c +=a10 print("Line 2 - value of c is", c)11

12 c *=a13 print("Line 3 - value of c is", c)14

15 c /=a16 print("Line 4 - value of c is", c)17

18 c = 2

19 c %=a20 print("Line 5 - value of c is", c)21

22 c **=a23 print("Line 6 - value of c is", c)24

25 c //=a26 print("Line 7 - value of c is", c)27

28

29

30 输出结果:31 Line 1 - value of c is 31

32 Line 2 - value of c is 52

33 Line 3 - value of c is 1092

34 Line 4 - value of c is 52.0

35 Line 5 - value of c is 2

36 Line 6 - value of c is 2097152

37 Line 7 - value of c is 99864

View Code

位运算符

按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

运算符描述实例

&

按位与运算符

(a & b) 输出结果 12 ,二进制解释: 0000 1100

|

按位或运算符

(a | b) 输出结果 61 ,二进制解释: 0011 1101

^

按位异或运算符

(a ^ b) 输出结果 49 ,二进制解释: 0011 0001

~

按位取反运算符

(~a ) 输出结果 -61 ,二进制解释: 1100 0011, 在一个有符号二进制数的补码形式。

<<

左移动运算符

a << 2 输出结果 240 ,二进制解释: 1111 0000

>>

右移动运算符

a >> 2 输出结果 15 ,二进制解释: 0000 1111

1 #!/usr/bin/python

2 a = 60 #60 = 0011 1100

3 b = 13 #60 = 0000 1101

4 c =05

6 c = a & b; #12 = 0000 1100

7 print("Line 1 - Value of c is", c)8

9 c = a | b; #61 = 0011 1101

10 print("Line 2 - value of c is", c)11

12 c = a ^ b; #49 = 0011 0001

13 print("Line 3 - value of c is", c)14

15 c = ~a; #-61 = 1100 0011

16 print("Line 4 - value of c is", c)17

18 c = a << 2; #240 = 1111 0000

19 print("Line 5 - value of c is", c)20

21 c = a >> 2; #15 = 0000 1111

22 print=("Line 6 - value of c is", c)23

24 输出结果:25 Line 1 - Value of c is 12

26 Line 2 - value of c is 61

27 Line 3 - value of c is 49

28 Line 4 - value of c is -61

29 Line 5 - value of c is 240

位运算符练习

逻辑运算符

以下假设变量a为10,变量b为20:

运算符描述实例

and

布尔"与" - 如果x为False,x and y返回False,否则它返回y的计算值。

(a and b) 返回 true。

or

布尔"或" - 如果x是True,它返回True,否则它返回y的计算值。

(a or b) 返回 true。

not

布尔"非" - 如果x为True,返回False。如果x为False,它返回True。

not(a and b) 返回 false。

1 #!/usr/bin/python

2 a = 10

3 b = 20

4 c =05

6 if ( a andb ):7 print("Line 1 - a and b are true")8 else:9 print("Line 1 - Either a is not true or b is not true")10

11 if( a orb ):12 print("Line 2 - Either a is true or b is true or both are true")13 else:14 print("Line 2 - Neither a is true nor b is true")15

16

17 a =018 if( a andb ):19 print("Line 3 -a and b are true")20 else:21 print("Line 3 - Either a is not true or b is not true")22

23 if ( a orb ):24 print("Line 4 - Either a is true or b is true or both are true")25 else:26 print("Line 4 - Neither a is true nor b is true")27

28 if ( a andb ):29 print("Line 5 - Either a is not true or b is not true or both are not true")30 else:31 print("Line 5 - a and b are true")32

33

34

35

36 输出结果:37 Line 1 - a andb are true38 Line 2 - Either a is true or b is true orboth are true39 Line 3 - Either a is not true or b is nottrue40 Line 4 - Either a is true or b is true orboth are true41 Line 5 - a and b are true

逻辑运算符练习

成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组

运算符描述实例

in

如果在指定的序列中找到值返回True,否则返回False。

x 在 y序列中 , 如果x在y序列中返回True。

not in

如果在指定的序列中没有找到值返回True,否则返回False。

x 不在 y序列中 , 如果x不在y序列中返回True。

1 #!/usr/bin/python

2

3 a = 10

4

5 b = 20

6

7 list = [1,2,3,4,5];8

9

10 if ( a inlist ):11

12 print("Line 1 - a is available in the given list")13

14 else:15

16 print("Line 1 - a is not available in the given list")17

18

19

20 if ( b not inlist ):21

22 print("Line 2 - b is not available in the given list")23

24 else:25

26 print("Line 2 - b is available in the given list")27

28

29

30 a = 2

31

32 if ( a inlist ):33

34 print("Line 3 - a is available in the given list")35

36 else:37

38 print("Line 3 - a is not available in the given list")39

40

41 输出结果:42 Line 1 - a is not available inthe given list43 Line 2 - b is not available inthe given list44 Line 3 - a is available in the given list

成员运算符练习

身份运算符

身份运算符用于比较两个对象的存储单元

运算符描述实例

is

is是判断两个标识符是不是引用自一个对象

x is y, 如果 id(x) 等于 id(y) , is 返回结果 1

is not

is not是判断两个标识符是不是引用自不同对象

x is not y, 如果 id(x) 不等于 id(y). is not 返回结果 1

1 #!/usr/bin/python

2 a = 20

3 b = 20

4

5 if ( a isb ):6 print("Line 1 - a and b have same identity")7 else:8 print("Line 1 - a and b do not have same identify")9

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

15 b = 30

16 if ( a isb ):17 print("Line 3 - a and b have same identity")18 else:19 print("Line 3 - a and b do not have same identity")20

21 if ( a is notb ):22 print("Line 4 - a and b do not have same identity")23 else:24 print("Line 4 - a and b have same identity")25

26

27 显示结果:28 Line 1 - a andb have same identity29 Line 2 - a andb have same identity30 Line 3 - a and b do nothave same identity31 Line 4 - a and b do not have same identity

身份运算符练习

运算符优先级

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

运算符描述

**

指数 (最高优先级)

~ + -

按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)

* / % //

乘,除,取模和取整除

+ -

加法减法

>> <<

右移,左移运算符

&

位 'AND'

^ |

位运算符

<= < > >=

比较运算符

<> == !=

等于运算符

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

赋值运算符

is is not

身份运算符

in not in

成员运算符

not or and

逻辑运算符

1 #!/usr/bin/python

2

3 a = 20

4 b = 10

5 c = 15

6 d = 5

7 e =08

9 e = ( a + b )* c / d #(20+10)*15/5

10 print("value of (a+b)*c/d is", e)11

12 e = (( a + b ) * c) / d #((20+10)*15)/5

13 print("value of (a+b)*c/d is", e)14

15 e = ( a + b ) * ( c / d ) #(20+10)*(15/5)

16 print("value of (a+b)*(c/d) is", e)17

18 e= a + ( b * c ) / d #20 + ( 10 * 15 ) / 5

19 print("value of a+(b*c)/d os", e)20

21

22 输出结果:23 value of (a+b)*c/d is 90.0

24 value of (a+b)*c/d is 90.0

25 value of (a+b)*(c/d) is 90.0

26 value of a+(b*c)/d os 50.0

运算符优先级练习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值