笨办法学python习题3 数字和数学计算

这次的习题主要介绍python里的数学运算符号。

首先对此次习题会接触到的运算符说明一下:

+

算数运算

加法运算

-

减法运算

/

除法运算

*

乘法运算

%

求余运算

<

布尔运算

小于

>

大于

<=

小于等于

>=

大于等于

接下来,我们还是结合代码对以上运算符号进行熟悉:

#start count chickens
print"I will now count my chickens:"
#count hens & Roosters quality
print"Hens",25 + 30/6
print"Roosters",100 - 25*3%4
#start count eggs
print"Now I will count the eggs:"
# function of % & /
print 3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6
# bool 
print" Is it true that 3 + 2 < 5 -7 ?"

print 3 + 2 < 5 - 7

print"What is 3 + 2?",3+2
print"What is 5 - 7?",5-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
运行结果如下:

mystuff simengred$ python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
 Is it true that 3 + 2 < 5 -7 ?
False
What is 3 + 2? 5
What is 5 - 7? -2
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,print后方“ ”后的,不要忘记否则在运行时会发生错误。

2,1/4求商的时候会将小数部分丢弃,如果将

print 3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6 修改为
print 3 + 2 + 1 - 5 + 4 % 2 -1.0 / 4 + 6那么除法后的结果就会是浮点型的数字(包含小数)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值