笨办法学Python ex03

数字和数学计算


  • 输入:
# -- coding: utf-8 --

print "I will now count my chickens:"
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 equla?", 5 <= -2
  • 运行:
笨办法学Python ex03_第1张图片

附加题


  • 浮点数(floating point number)

“浮点数”的定义是相对于“定点数”来说的。
在计算机内部,数据以二进制的形式存储和运算,而计算机内表示的数,又被分成整数和实数两大类。
其中整数一般用定点数表示,定点数指小数点在数中有固定的位置(一般来说整数的小数点都在最末位数后)。
实数一般用浮点数表示,因为它的小数点位置不固定。浮点数是既有整数又有小数的数,纯小数可以看作实数的特例。

  • 使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。
# -- coding: utf-8 --

print "I will now count my chickens:"
print "Roosters", 100.0- 25.0* 3.0 % 4.0

print "Now I will count the eggs:"

print 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0

print "Is it true that 3.0 + 2.0 < 5.0 - 7.0?"

print 3.0 + 2.0 < 5.0 - 7.0

print "What is 3.0 + 2.0?", 3.0 + 2.0
print "What is 5.0 - 7.0?", 5.0 - 7.0

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5.0 > -2.0
print "Is it greater or equal?", 5.0 >= -2.0
print "Is it less or equla?", 5.0 <= -2.0
笨办法学Python ex03_第2张图片
Paste_Image.png

你可能感兴趣的:(笨办法学Python ex03)