Learn Python the Hard Way 5: Ex03 Numbers and Math
2016-11-15 22:32
537 查看
5 Ex03 Numbers and Math
很多编程语言中, 运算符是必不可少的. (当当当… 数学课)5.1 运算符
+plus 加
-minus 减
/slash 斜杠(取商)
*asterisk 星号(乘)
%percent 百分号(取余)
<less-than 小于
>greater-than 大于
<=less-than-equal 小于等于
>=greater-than-equal 大于等于
# -*- coding: utf-8 -* 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 "It's 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
这是你应该要看到的输出:
5.2 研究学习
请解释每一行运算符是如何工作的.重写整个程序,请需要的地方使用浮点类型数据.
# -*- coding: utf-8 -* print "Hens", 25 + 30 / 6 #取商/优先级高于+,先算 30 / 6, 然后25 + 5 = 30 print "Roosters", 100 - 25 * 3 % 4 #*和取余%优先级高于-, 先算25 * 3 = 75, 然后75 / 4 = 13余3,所以75 % 4 = 3, 最后算100 - 3 = 97 print "Now I will count the eggs:" print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 #先算4 % 2 = 0 然后算 1 / 4 = 0, 然后从做到右3 + 2 + 1 - 5 + 0 - 0 + 6 = 7 print "It's true that 3 + 2 < 5 - 7?" print 3 + 2 < 5 - 7 #先算 3 + 2 = 5, 然后算 5 - 7 = -2, 最后计算逻辑 5 < -2 print "what is 3 + 2?", 3 + 2 #3 + 2 = 5 print "What is 5 - 7?", 5 - 7 #5 - 7 = -2 print "Oh, that's why it's false." print "How about some more." #下面均是逻辑运算,输出真(True)或假(False) #注意Python中的真假Ture,False需要大写首字母 print "Is it greater?", 5 > -2 print "Is it greater or equal?", 5 >= -2 print "Is it less or equal?", 5 <= -2
# -*- coding: utf-8 -* print "Hens", 25.0 + 30.0 / 6.0 print "Roosters", 100.0 - 25 * 3 % 4 print "Now I will count the eggs:" print 3 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4.0 + 6 #这里需要用浮点型 print "It's 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
相关文章推荐
- Learn Python the Hard Way 8: Ex06 Strings and Text
- Learn Python the Hard Way 6: Ex04 Variables and Names
- Learn Python the Hard Way 4: Ex02 Comments and Pound Characters
- read the python code and predict the results --- from <Learn Python The Hard Way>
- Learn Python the Hard Way 7: Ex05 More Variables and Printing
- Learn Python The Hard Way学习(13) - 参数,解包,变量
- Learn Python The Hard Way学习(4) - 变量和命名
- Learn Python The Hard Way学习(0) - 安装
- Learn Python The Hard Way学习(6) - 字符串和文本
- Learn Python The Hard Way学习(5) - 更多的变量和打印
- Learn Python The Hard Way学习(2) - 注释和#号
- Learn Python The Hard Way学习(12) - 输入提示
- Learn Python The Hard Way学习(23) - 读一些代码
- 一个老程序员的建议(《Learn Python The Hard Way, 2nd Edition》尾声)
- Learn Python The Hard Way学习(7) - 更多的打印
- Learn Python The Hard Way学习(3) - 数字和数学计算
- Learn Python The Hard Way学习(21) - 函数可以返回信息
- Learn Python The Hard Way学习(18) - 命名,变量,代码,函数
- 笨办法学 Python (Learn Python The Hard Way)
- Learn Python The Hard Way学习(10) - 那是什么?