这次的习题主要介绍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那么除法后的结果就会是浮点型的数字(包含小数)