练习3--数字和数学计算

1 常见的数学符号

  • + plus,加号
  • - minus,减号
  •  / slash,斜杠
  • * asterisk,星号
  • % percent,百分号
  • < less-than,小于号
  • > greater-than,大于号
  • <= less-than-equal,小于等于号
  • >= greater-than-equal,大于等于号

2  一个例子:

python2语法下的代码:

   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

python3语法下的代码:

   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

3 运行结果:

python2语法下执行结果:

     $ python ex3.py
     I will now count my chickens:
     Hens 30
     Roosters 97
     Now I will count the eggs:
     7Is 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
     $
 
python3语法下执行结果:
     I will now count my chickens:
     Hens 30.0
     Roosters 97
     Now I will count the eggs:
     6.75
     Is it true that 3 + 2 < 5 - 7?
     False
     What is 3 + 2? 5
     What is 5 - 7? -2
     Oh, that is why it's False.
     How about some more.
     Is it greater? True
     Is it greater or equal? True
     Is it less or equal? False

4 扩展

  1. python2里面”/“运算小数点后的数会被丢掉,python3里面”/“运算可以输出小数点后的值,所以例子里面“鸡蛋的数量”运算结果不一样,一个是7,一个是6.75。(注意:可能python3自己完成了整数和浮点数的转换?)
  2. 运算顺序:括号内优于(乘、除、取余%)优于(加、减),即PE(M&D)(A&S)
  3. %号:表示取余数
posted @ 2020-06-09 15:15  洛兰123  阅读(164)  评论(0编辑  收藏  举报