python 学习 “笨办法学python”(随书补充)

习题 3: 数字和数学计算

这章练习里有很多的数学运算符号。我们来看一遍它们都叫什么名字。你要一边写一边念出它们的名字来,直到你念烦了为止。名字如下:

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

有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。——>精确计算使用浮点数,如20.0

 

习题 4: 变量(variable)和命名

记住 = 的名字是等于(equal),它的作用是为东西取名。
记住 _ 是下划线字符(underscore)。

 

习题 5: 更多的变量和打印

在网上搜索所有的 Python 格式化字符——>

 



整型数:%d
无符号整型数:%u
八进制:%o
十六进制:%x %X
浮点数:%f
科学记数法: %e %E
根据数值的不同自动选择%e或%f: %g
根据数值的不同自动选择%E或%f: %G

试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成——
_name = 'Zed A. Shaw'
_age = 35 #not a lie
_height = 74 #inches
_weight = 180 #lbs
_eyes = 'Blue'
_teeth = 'White'
_hair = 'Brown'

print "Let's talk about %s." % _name
print "He's %d pounds heavy." %_weight
print "He's %.2f kilogrammes heavy." %0.45359 * _weight
print "He's %d inches tall." %_height
print "He's %.2f centimeters tall." %_height * 2.54

上面的程序有错误!
正确的:
_name = 'Zed A. Shaw'
_age = 35 #not a lie
_height = 74 #inches
_weight = 180 #lbs
_eyes = 'Blue'
_teeth = 'White'
_hair = 'Brown'

print "Let's talk about %s." % _name
print "He's %d pounds heavy." %_weight
print "He's %.2f kilogrammes heavy." %(0.45359 * _weight)
print "He's %d inches tall." %_height
print "He's %.2f centimeters tall." %(_height * 2.54)

注意括号的运用!

 

posted @ 2017-11-22 17:47  cynthialy  阅读(250)  评论(0编辑  收藏  举报