《笨方法学python》--习题3

习题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

其他:

  1. 数学运算符号:
    + plus 加法运算
    - minus 减法运算
    / slash 取整运算
    * asterisk 乘法运算
    % percent 取余运算
  2. 逻辑运算符号:
    greater-than
    less-than
    greater-than-equal
    less-than-equal
    1. 浮点数:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值