這個系列的部落格是學習《Learn Python the Hard Way》中做的筆記。
1. 由於今天需要看的five chapters是複習性質的內容,把前邊的內容綜合了一下,所以沒有新內容
只把代碼貼在這
View Code
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 wordwith logic so firmly plantedcannot discern \n the needs of lovenor comprehend passion from intuitionand requires an explanation\n\t\twhere there is none."""print "-------------------"print poemprint "-------------------"five = 10 - 2 + 3 -6print "This should be five: %s" % fivedef secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, cratesstart_point = 10000beans, jars, crates = secret_formula(start_point)print "With a starting point of: %d" % start_pointprint "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)start_point = start_point / 10print "We can also do that this way:"print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
輸出
View Code
E:\SkyDrive\python\the hard way to learn python>python ex24.pyLet's practice everything.You'd need to know 'bout escapes with \ that do newlines and tabs.------------------- The lovely wordwith logic so firmly plantedcannot discern the needs of lovenor comprehend passion from intuitionand requires an explanation where there is none.-------------------This should be five: 5With a starting point of: 10000We'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這個檔案裡
View Code
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(" ") return wordsdef 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 worddef print_last_word(words): """Prints the last word after popping it off.""" word = words.pop(-1) print worddef 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.pyE:\SkyDrive\python\the hard way to learn python>pythonPython 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32Type "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.>>> wrodsTraceback (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)Allwait.
值得注意的是兩點
- import ex25和from ex25 import *的區別,調用函數ex25.break_words(sentence)和直接調用break_words(sentence)
- list可以pop任意位置