写给某人:有一位同学天天在我耳边念叨Python语言如何如何好(请恕我词穷不知如何复述那个如何如何到底是有多如何如何),所以下定决心要从头开始学习Python了。写代码一直是我的软肋,希望这次能坚持学习,边学边记。
跟着笨方法学python个教程学习,边学变做习题边记录。
笨方法学英语-ex5-更多的变量和打印 - 学习记录
教程说明:
我们现在要键入更多的变量并且把它们打印出来。这次我们将使用一个叫“格式化字符串(format string)”的东西. 每一次你使用 " 把一些文本引用起来,你就建立了一个字符串。 字符串是程序将信息展示给人的方式。你可以打印它们,可以将它们写入文件,还可以将它们发送给网站服务器,很多事情都是通过字符串交流实现的。
字符串是非常好用的东西,所以再这个练习中你将学会如何创建包含变量内容的字符串。使用专门的格式和语法把变量的内容放到字符串里,相当于来告诉 Python :“嘿,这是一个格式化字符串,把这些变量放到那几个位置。”
跟着练习:
[root@lc test]# vi ex5.py
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." %(my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky,try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)
"ex5.py" 17L, 595C written
[root@lc test]#
运行该程序:
[root@lc test]# python ex5.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
[root@lc test]#
加分习题
1.修改所有的变量名字,把它们前面的``my_``去掉。确认将每一个地方的都改掉,不只是你使用``=``赋值过的地方。
[root@lc test]# vi ex5-1.py
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 inches tall." % height
print "He's %d pounds heavy." % weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." %(eyes, hair)
print "His teeth are usually %s depending on the coffee." % teeth
# this line is tricky,try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (age, height, weight, age + height + weight)
"ex5-1.py" 17L, 538C written
[root@lc test]#
运行该程序:
2.试着使用更多的格式化字符。例如 %r 就是是非常有用的一个,它的含义是“不管什么都打印出来”。
[root@lc test]# vi ex5-2.py
words = 'Hello, World!'
print "The 1st test %s." % words
print "The 2nd test %r." % words
[root@lc test]#
运行该程序:
3.在网上搜索所有的 Python 格式化字符。
格式符为真实值预留位置,并控制显示的格式。格式符可以包含有一个类型码,用以控制显示的类型,如下:
%s 字符串 (采用str()的显示)
%r 字符串 (采用repr()的显示)
%c 单个字符
%b 二进制整数
%d 十进制整数
%i 十进制整数
%o 八进制整数
%x 十六进制整数
%e 指数 (基底写为e)
%E 指数 (基底写为E)
%f 浮点数
%F 浮点数,与上相同
%g 指数(e)或浮点数 (根据显示长度)
%G 指数(E)或浮点数 (根据显示长度)
%% 字符"%"
可以用如下的方式,对格式进行进一步的控制:
%[(name)][flags][width].[precision]typecode
(name)为命名
flags可以有+,-,' '或0。+表示右对齐。-表示左对齐。' '为一个空格,表示在正数的左侧填充一个空格,从而与负数对齐。0表示使用0填充。
width表示显示宽度
precision表示小数点后精度
比如:
print("%+10x" % 10)
print("%04d" % 5)
print("%6.3f" % 2.3)
上面的width, precision为两个整数。我们可以利用*,来动态代入这两个量。比如:
print("%.*f" % (4, 1.2))
Python实际上用4来替换*。所以实际的模板为"%.4f"。
总结
Python中内置的%操作符可用于格式化字符串操作,控制字符串的呈现格式。Python中还有其他的格式化字符串的方式,但%操作符的使用是最方便的。
4.试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成。
[root@lc test]# vi ex5-4.py
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d cm tall." % my_height * 2.54
print "He's %d pounds heavy." % my_weight
print "He's %d kg heavy." % my_weight * 0.4535924
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." %(my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky,try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)
"ex5-4.py" 19L, 689C written
[root@lc test]#
运行该程序:
[root@lc test]# python ex5-4.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
Traceback (most recent call last):
File "ex5-4.py", line 11, in <module>
print "He's %d cm tall." % my_height * 2.54
TypeError: can't multiply sequence by non-int of type 'float'
[root@lc test]#
返回错误,修改如下:
[root@lc test]# vi ex5-4.py
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d cm tall." % (my_height * 2.54)
print "He's %d pounds heavy." % my_weight
print "He's %d kg heavy." % (my_weight * 0.4535924)
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." %(my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky,try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)
"ex5-4.py" 19L, 693C written
[root@lc test]#
再次运行
[root@lc test]# python ex5-4.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 187 cm tall.
He's 180 pounds heavy.
He's 81 kg heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
[root@lc test]#
成功
至此,这一小节的学习结束了。
[root@lc test]# python ex5-2.py
The 1st test Hello, World!.
The 2nd test 'Hello, World!'.
[root@lc test]#
[root@lc test]# python ex5-1.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
[root@lc test]#