新手学python-笨方法学python-ex7-更多打印

跟着个教程学习,边学边做习题边记录。

新手学python-笨方法学python-ex7-更多打印 - 学习记录

跟着课程练习:

[root@lc test]# vi ex7.py 

print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 # what'd that do?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12
"ex7.py" 21L, 449C written
[root@lc test]# 

运行该程序:

[root@lc test]# python ex7.py 
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
[root@lc test]# 

 

根据提示去掉print里面end6后面的comma逗号看看发生了什么

[root@lc test]# vi ex7-0.py 

print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 # what'd that do?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6
print end7 + end8 + end9 + end10 + end11 + end12
"ex7-0.py" 21L, 448C written
[root@lc test]# 

 

运行该程序

[root@lc test]# python ex7-0.py 
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese
Burger
[root@lc test]# 

有逗号的时候,两个print打印在同一行,中间隔了一个空格;
去掉逗号之后,两个print分别打印了一行。


加分练习,从后往前添加注释:


[root@lc test]# vi ex7-1.py 

#print string "Mary had a little lamb."
print "Mary had a little lamb."
#print string "Its fleece was white as %s." where %s is snow
print "Its fleece was white as %s." % 'snow'
#print string "And everywhere that Mary went."
print "And everywhere that Mary went."
#repeat "." 10 times
print "." * 10 # what'd that do?

#define variable end1~end12
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

# watch that comma at the end. try removing it to see what happens
#print variable end1~end6's value and then a space
print end1 + end2 + end3 + end4 + end5 + end6,
#print variable end7~end12's value
print end7 + end8 + end9 + end10 + end11 + end12
"ex7-1.py" 28L, 732C written
[root@lc test]# 

 

运行该程序:

[root@lc test]# python ex7-1.py 
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
[root@lc test]# 


至此,这一小节的学习结束了。

Wednesday, October 28, 2015 | Python

文章评论

No comments posted yet.

发表评论

Please add 8 and 3 and type the answer here: