标签:let none tab pytho 基础 style ant cap can
你离这本书第一部分的结尾已经不远了,你应该已经具备了足够的 Python 基础知识,可以继续学习一些编程的原理了,但你应该做更多的练习。这个练习的内容比较长,它的目的是锻炼你的毅力,下一个习题也差不多是这样的,好好完成它们,做到完全正确,记得仔细检查。
1 print "Let‘s practice everything." 2 print ‘You\‘d need to know \‘bout escapes with \\ that do \n newlines and \t tabs.‘ 3 4 poem = """ 5 \tThe lovely world 6 with logic so firmly planted 7 cannot discern \n the needs of love 8 nor comprehend passion from intuition 9 and requires an explanation 10 \n\t\twhere there is none. 11 """ 12 13 print "--------------" 14 print poem 15 print "--------------" 16 17 18 five = 10 - 2 + 3 - 6 19 print "This should be five: %s" % five 20 21 def secret_formula(started): 22 jelly_beans = started * 500 23 jars = jelly_beans / 1000 24 crates = jars / 100 25 return jelly_beans, jars, crates 26 27 28 start_point = 10000 29 beans, jars, crates = secret_formula(start_point) 30 31 print "With a starting point of: %d" % start_point 32 print "We‘d have %d beans, %d jars, and %d crates." % (beans, jars, crates) 33 34 start_point = start_point / 10 35 36 print "We can also do that this way:" 37 print "We‘d have %d beans, %d jars, and %d crates." % secret_formula(start_point)
1.
请留意最后一行,出现了新的用法,那就是不经过变量,直接以 %+函数 的形式格式化输出函数的结果,请记住这种用法,可以简化你的代码。
2.
函数内部变量的作用于仅仅是函数内,对函数外或者其他函数内部则没有影响,因此可以有相同的变量名。请根据实际情况取舍,有时候相同的变量名会更简便,有时候则会引起混淆。
标签:let none tab pytho 基础 style ant cap can
原文地址:http://www.cnblogs.com/yllinux/p/7434958.html