这个系列的博客是学习《Learn Python the Hard Way》中做的笔记。
1. 由于今天需要看的five chapters是复习性质的内容,把前边的内容综合了一下,所以没有新内容
只把代码贴在这

print "Let's practice everything." print "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs." poem = """ \t The lovely word with logic so firmly planted cannot discern \n the needs of love nor comprehend passion from intuition and requires an explanation \n\t\twhere there is none. """ print "-------------------" print poem print "-------------------" five = 10 - 2 + 3 -6 print "This should be five: %s" % five def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates start_point = 10000 beans, jars, crates = secret_formula(start_point) print "With a starting point of: %d" % start_point print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) start_point = start_point / 10 print "We can also do that this way:" print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
输出

E:\SkyDrive\python\the hard way to learn python>python ex24.py Let's practice everything. You'd need to know 'bout escapes with \ that do newlines and tabs. ------------------- The lovely word with logic so firmly planted cannot discern the needs of love nor comprehend passion from intuition and requires an explanation where there is none. ------------------- This should be five: 5 With a starting point of: 10000 We'd have 5000000 beans, 5000 jars, and 50 crates. We can also do that this way: We'd have 500000 beans, 500 jars, and 5 crates.
2. 把自己写的函数放在一个文件里,然后import这个文件,即可在代码里使用这些函数
把函数放在ex25.py这个文件里

def break_words(stuff): """This function will break up words for us.""" words = stuff.split(" ") return words def sort_words(words): """Sorts the words.""" return sorted(words) def print_first_word(words): """Prints the first word after popping it off.""" word = words.pop(0) print word def print_last_word(words): """Prints the last word after popping it off.""" word = words.pop(-1) print word def sort_sentence(sentence): """Takes in a full sentence and returns the sorted words.""" words = break_words(sentence) return sort_words(words) def print_first_and_last(sentence): """Prints the first and last words of the sentence.""" words = break_words(sentence) print_first_word(words) print_last_word(words) def print_first_and_last_sorted(sentence): """Sorts the words then Prints the first and last one.""" words = sort_sentence(sentence) print_first_word(words) print_last_word(words)
在python命令行里执行如下命令
E:\SkyDrive\python\the hard way to learn python>python ex25.py E:\SkyDrive\python\the hard way to learn python>python Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import ex25 >>> sentence = "All good things come to those who wait." >>> words = ex25.break_words(sentence) #使用ex25里的函数需要这样。如果嫌麻烦,可以在开始from ex25 import *,这样这行就可以这么写words = break_words(sentence) >>> words ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.'] >>> sorted_words = ex25.sort_words(words) >>> sorted_words ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] >>> ex25.print_first_word(words) All >>> ex25.print_last_word(words) wait. >>> wrods Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'wrods' is not defined >>> words ['good', 'things', 'come', 'to', 'those', 'who'] >>> ex25.print_first_word(sorted_words) All >>> ex25.print_last_word(sorted_words) who >>> sorted_words ['come', 'good', 'things', 'those', 'to', 'wait.'] >>> sorted_words = ex25.sort_sentence(sentence) >>> sorted_words ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] >>> ex25.print_first_and_last(sentence) All wait.
值得注意的是两点
- import ex25和from ex25 import *的区别,调用函数ex25.break_words(sentence)和直接调用break_words(sentence)
- list可以pop任意位置