python算数运算符
~/code ⮀ cat yunsuanfu1.py
#!/usr/bin/env python
# -*- encoding: UTF-8 -*-
'''
python算数运算符
'''
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
~/code ⮀
Python赋值运算符的操作与练习
~/code ⮀ cat yunsuanfu2.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
Python赋值运算符的操作与练习
'''
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
比较运算符
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
python 比较运算符
'''
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"
if (a > b):
print "5 - a 大于 b"
else:
print "5 - a 小于等于 b"
'''
修改变量a 和 b 的值
'''
a = 5;
b = 20;
if (a <= b):
print "6 - a 小于等于 b"
else:
print "6 - a 大于 b"
if (b >=a):
print "7 - b 大于等于 b"
else:
print "7 - b 小于 b"
~/code ⮀
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
a = 60
b = 13
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;
print "4 - c 的值为:", c
c = a << 2;
print "5 - c 的值为:", c
c = a >> 2;
print "6 - c 的值为:", c
~/code ⮀ cat yunsuanfu5.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
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"
~/code ⮀ cat yunsuanfu6.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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中"
~/code ⮀ cat yunsuanfu7.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
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有相同的标识"
~/code ⮀
~/code ⮀ cat yunsuanfu8.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a+b) * c / d
print "(a + b) * c / d 运算结果为: ", e
e = ((a + b) * c) / d
print "((a + b) * c) / d 运算结果为: ", e
e = (a + b) * (c / d);
print "(a + b) * (c /d) 运算结果为: ", e
e = a + (b * c) / d;
print "a + (b*c) / d 运算结果为: ", e
See also