您的位置:首页 > 编程语言 > Python开发

笨方法学Python 习题 3: 数字和数学计算

2017-07-26 13:05 501 查看
#!usr/bin/python
# -*- coding:utf8 -*-
#  打印功能 我将数我的小鸡们
print ("I will now count my chickens")
# 打印功能 母鸡20+30*6
print ("Hens" , 25 + 30 * 6)
# 打印功能 Roosters公鸡 100-25*3%4
print ("Rppsters" , 100 - 25 * 3 % 4)
# 打印功能 现在我要开始输鸡蛋了:
print ("Now i will count the eggs:")
# 打印功能 3+2+1-5-4%2-1/4-6
print (3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6)
# 打印功能 Is it true that 3+2<5-7?
print ("Is it true that 3 + 2 < 5 - 7?")
# 打印功能 3+2<5-7
print (3 + 2 < 5 - 7)
# 打印功能 What is 3+2? 5
print ("What is 3 + 2 ?" , 3 + 2)
# 打印功能 What is 5-7?-2
print ("What is 5 - 7 ?" , 5 - 7)
# 打印功能 Oh , that's why it's False.
print ("Oh , that's why it's False.")
# 打印功能 How about some more.
print ("How about some more. ")
# 打印功能 Is it greater? False
print ("Is is greater?" , 5 > -2)
# 打印功能 Is it greater or equal? True
print ("Is it greater or equal?" , 5 >= -2)
# 打印功能 Is it less or equal? False
print ("Is it less or equal?" , 5 <= -2)
先贴代码,随后运行结果如下:python ex3.pyI will now count my chickens:Hens 30Roosters 97Now I will count the eggs:7Is it true that 3 + 2 < 5 - 7?FalseWhat is 3 + 2? 5What is 5 - 7? -2Oh, that's why it's False.How about some more.Is it greater? TrueIs it greater or equal? TrueIs it less or equal? False加分习题①使用 # 在代码每一行的前一行为自己写一个注解,说明一下这一行的作用。如上②记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行起来,然后使用刚才学到的运算符号,把Python当做计算器玩玩。不另外贴代码③自己找个想要计算的东西,写一个 .py 文件把它计算出来。④有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。Python的浮点数就是数学中的小数,类似C语言中的double。  在运算中,整数与浮点数运算的结果是浮点数。浮点数也就是小数。⑤使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。常见问题回答为什么 % 是求余数符号,而不是百分号?很大程度上只是因为涉及人员选择了这个符号而已。一般而言它是百分号没错,就跟 100% 表示百分之百一样。在编程中除法我们用了 /,而求余数又恰恰选择了 % 这个符号,仅此而已。% 是怎么工作的?换个说法就是“X 除以 Y 还剩余 J”,例如“100 除以 16 还剩 4”。 % 运算的结果就是 J 这部分。运算优先级是什么样子的?美国我们用 PEMDAS 这个简称来辅助记忆,它的意思是“括号、指数、乘、除、加、减”——Parentheses Exponents Multiplication Division Addition Subtraction ——这也是 Python 里的运算优先级。为什么 / 除法算出来的比实际小?其实不是没算对,而是它将小数部分丢弃了,试试 7.0 / 4.0 和 7 / 4 比较一下,你就看出不同了。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息