码迷,mamicode.com
首页 > 编程语言 > 详细

python基础(二)

时间:2016-04-30 14:13:17      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

part1:python中的运算符

1、算数运算符

技术分享

代码说明:

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 #__author__ = "tina"
 4 a = 22
 5 b = 11
 6 c = 0
 7 
 8 c = a + b
 9 print "eg1:Value of c is ", c
10 
11 c = a - b
12 print "eg2:Value of c is ", c
13 
14 c = a * b
15 print "eg3:Value of c is ", c
16 
17 c = a / b
18 print "eg4: Value of c is ", c
19 
20 c = a % b
21 print "eg5: Value of c is ", c
22 
23 a = 3
24 b = 3
25 c = a**b 
26 print "eg6: Value of c is ", c
27 
28 a = 10
29 b = 5
30 c = a//b 
31 print "eg7:Value of c is ", c
32 
33 当执行上面的程序,它会产生以下结果:
34 
35 C:\Python27\python.exe E:/python/day2test.py
36 eg1:Value of c is  33
37 eg2:Value of c is  11
38 eg3:Value of c is  242
39 eg4: Value of c is  2
40 eg5: Value of c is  0
41 eg6: Value of c is  27
42 eg7:Value of c is  2

 

2、比较运算符

技术分享

代码说明:

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 #__author__ = "tina"
 4 
 5 a = 22
 6 b = 11
 7 c = 0
 8 
 9 if ( a == b ):
10   print "eg1: a is equal to b"
11 else:
12   print "eg1: a is not equal to b"
13 
14 if ( a != b ):
15   print "eg2: a is not equal to b"
16 else:
17   print "eg2: a is equal to b"
18 
19 if ( a <> b ):
20   print "eg3:a is not equal to b"
21 else:
22   print "eg3: a is equal to b"
23 
24 if ( a < b ):
25   print "eg4: a is less than b" 
26 else:
27   print "eg4:a is not less than b"
28 
29 if ( a > b ):
30   print "eg5:a is greater than b"
31 else:
32   print "eg5:a is not greater than b"
33 
34 a = 6
35 b = 66
36 if ( a <= b ):
37   print "eg6: a is either less than or equal to b"
38 else:
39   print "eg6: a is neither less than nor equal to b"
40 
41 if ( b >= a ):
42   print "eg7: b is either greater than or equal to b"
43 else:
44   print "eg7: b is neither greater than nor equal to b"
45 
46 执行以上代码,显示结果如下:
47 
48 C:\Python27\python.exe E:/python/day2test.py
49 eg1: a is not equal to b
50 eg2: a is not equal to b
51 eg3:a is not equal to b
52 eg4:a is not less than b
53 eg5:a is greater than b
54 eg6: a is either less than or equal to b
55 eg7: b is either greater than or equal to b

 

3、赋值运算符

技术分享

代码说明:

#!/usr/bin/env python
#-*-coding:utf-8-*-
#__author__="tina"

a = 22
b = 11
c = 0

c = a + b
print "eg1: Value of c is ", c

c += a
print "eg2: Value of c is ", c 

c *= a
print "eg3: Value of c is ", c 

c /= a 
print "eg4: Value of c is ", c 

c = 2
c %= a
print "eg5: Value of c is ", c

c **= a
print "eg6: Value of c is ", c

c //= a
print "eg7: Value of c is ", c

执行以上代码,显示结果如下:

C:\Python27\python.exe E:/python/day2test.py
eg1: Value of c is  33
eg2: Value of c is  55
eg3: Value of c is  1210
eg4: Value of c is  55
eg5: Value of c is  2
eg6: Value of c is  4194304
eg7: Value of c is  190650

 

4、逻辑运算符

技术分享

代码说明:

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 #__author__ = "tina"
 4 a = 22
 5 b = 11
 6 c = 0
 7 
 8 if ( a and b ):
 9   print "eg1:a and b are true"
10 else:
11   print "eg1:Either a is not true or b is not true"
12 
13 if ( a or b ):
14   print "eg2:Either a is true or b is true or both are true"
15 else:
16   print "eg2:Neither a is true nor b is true"
17 
18 
19 a = 0
20 if ( a and b ):
21   print "eg3:a and b are true"
22 else:
23   print "eg3:Either a is not true or b is not true"
24 
25 if ( a or b ):
26   print "eg4:Either a is true or b is true or both are true"
27 else:
28   print "eg4:Neither a is true nor b is true"
29 
30 if not( a and b ):
31   print "eg5:Either a is not true or b is not true"
32 else:
33   print "eg5:a and b are true"
34 
35 
36 执行以上代码,显示结果如下:
37 
38 C:\Python27\python.exe E:/python/day2test.py
39 eg1:a and b are true
40 eg2:Either a is true or b is true or both are true
41 eg3:Either a is not true or b is not true
42 eg4:Either a is true or b is true or both are true
43 eg5:Either a is not true or b is not true

 

5、成员运算符

技术分享

代码说明:

#!/usr/bin/env python
#-*-coding:utf-8-*-
#__author__ = "tina"
a = 2
b = 11
li = [1,2,3,4,5,6]

if(a in li):
    print "eg1:a is available in the given list"
else:
    print "eg1:a is not available in the given list"

if(b not in li):
    print "eg2:b is not available in the given list"
else:
    print "eg2:b is available in the given list"

执行以上代码,显示结果如下:

C:\Python27\python.exe E:/python/day2test.py
eg1:a is available in the given list
eg2:b is not available in the given list

 6、二进制位运算符

技术分享

代码说明:

# !/usr/bin/env python
# -*-coding:utf-8-*-
# __author__ = "tina"

a=60
b=13
c=0

c = a & b
print "eg1:Value of c is ", c

c = a | b
print "eg2:Value of c is ", c

c = a ^ b
print "eg3:Value of c is ", c

c = ~a
print "eg4:Value of c is ", c

c = a << 2
print "eg5:Value of c is ", c

c = a >> 2
print "eg6:Value of c is ", c

执行以上代码,显示结果如下:

C:\Python27\python.exe E:/python/day2test.py
eg1:Value of c is  12
eg2:Value of c is  61
eg3:Value of c is  49
eg4:Value of c is  -61
eg5:Value of c is  240
eg6:Value of c is  15

 

python基础(二)

标签:

原文地址:http://www.cnblogs.com/tina-python/p/5448675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!