一、代码分析
# -- coding: utf-8 --
print "I will now count my chickens:"
print "Hens",25 + 30 / 6
#%是求余数
print "Roosters",100 - 25 * 3 % 4
print "Now i will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
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
print "5 / 3=",5 / 3
程序中依然遵循四则运算的运算顺序:先乘除求余后加减。
%是求余数运算,也就是两数相除以后余数是多少。
注意代码最后一行,5除以3应该约等于1.6666666,但输出显示5 / 3 = 1。整数除整数,结果也取整,若将最后一行分子或分母改为浮点型数字:
print "5 / 3=",5.0 / 3
则结果将是:
5 / 3= 1.66666666667
二、扩展学习
1.算术运算
2.逻辑运算
3.比较(关系)运算
4.赋值运算
5.位运算
6.成员运算
7.身份运算
运算符优先级