习题3 数字和数学计算
#!usr/bin/python
#--coding:utf-8--
print "I will now count my chickens:"
#表达式中只有数学运算符,就输出运算结果,与字符串用逗号分隔
print "Hens", 25.0 + 30 / 6
print "Roosters", 100.0 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3.0 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
#字符串内有数学运算符和逻辑运算符,均不做任何处理,直接输出
print "Is it true that 3 + 2 < 5 - 7?"
#字符串外的表达式中有逻辑运算符,输出逻辑运算结果True/False
print 3.0 + 2 < 5.0 - 7
print "What is 3 + 2?", 3.0 + 2
print "What is 5 - 7?", 5.0 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
运行结果:
I will now count my chickens:
Hens 30.0
Roosters 97.0
Now I will count the eggs:
7.0
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5.0
What is 5 - 7? -2.0
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False
其他:
- 数学运算符号:
+ plus 加法运算
- minus 减法运算
/ slash 取整运算
* asterisk 乘法运算
% percent 取余运算 - 逻辑运算符号:
greater-than
less-than
greater-than-equal
less-than-equal
- 浮点数: