Python實戰(六)
阿新 • • 發佈:2018-12-22
一 實戰——恭喜你,可以進行一次考試了
1 ex26.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) print "Let's practice everything." print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' poem = """ \tThe lovely world with logic so firmly planted cannot discern \n the needs of love nor comprehend passion from intuition and requires an explantion \n\t\twhere there is none. """ print "--------------" print poem print "--------------" five = 10 - 2 + 3 - 5 print "This should be five: %d" % 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 jeans, %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 crabapples." % secret_formula(start_point) sentence = "All good things come to those who wait." words = break_words(sentence) sorted_words = sort_words(words) print_first_word(words) print_last_word(words) print_first_word(sorted_words) print_last_word(sorted_words) sorted_words = sort_sentence(sentence) print sorted_words print_first_and_last(sentence) print_first_and_last_sorted(sentence)
2 執行結果
PS E:\Python\exercise> python ex26.py Let's practice everything. You'd need to know 'bout escapes with \ that do newlines and tabs. -------------- The lovely world with logic so firmly planted cannot discern the needs of love nor comprehend passion from intuition and requires an explantion where there is none. -------------- This should be five: 6 With a starting point of: 10000 We'd have 5000000 jeans, 5000 jars, and 50 crates. We can also do that this way: We'd have 500000 beans, 500 jars, and 5 crabapples. All wait. All who ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] All wait. All who
二 實戰——布林表示式
1 執行結果
PS E:\Python\exercise> python Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> True and True True >>> False and True False >>> 1 == 1 and 2 == 1 False >>> "test" == "test" True >>> 1 == 1 or 2 != 1 True >>> True and 1 == 1 True >>> False and 0 != 0 False >>> True or 1 == 1 True >>> "test" == "testing" False >>> 1 != 0 and 2 == 1 False >>> "test" != "testing" True >>> "test" == 1 False >>> not (True and False) True >>> not (1 == 1 and 0 != 1) False >>> not (10 == 1 or 1000 == 1000) False >>> not (1 != 10 or 3 == 4) False >>> not ("testing" == "testing" and "Zed" == "Cool Guy") True >>> 1 == 1 and (not ("testing" == 1 or 1 == 0)) True >>> "chunky" == "bacon" and (not (3 == 4 or 3 == 3)) False >>> 3 == 3 and (not ("testing" == "testing" or "Python" == "Fun")) False >>> False and 1 False >>> True and 1 1 >>> 1 and True True >>> 1 and False False >>> "test" and "test" 'test'
三 實戰——IF 語句
1 ex29.py
people = 20
cats = 30
dogs = 15
if people < cats:
print "Too many cats! The world is doomed!"
if people > cats:
print "Not many cats! The world is saved!"
if people < dogs:
print "The world is drooled on!"
if people > dogs:
print "The world is dry!"
dogs += 5
if people >= dogs:
print "People are greater than or equal to dogs."
if people <= dogs:
print "People are less than or equal to dogs."
if people == dogs:
print "People are dogs."
2 執行結果
PS E:\Python\exercise> python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
四 實戰——Else 和 If
1 ex30.py
people = 30
cars = 40
trucks = 15
if cars > people:
print "We should take the cars."
elif cars < people:
print "We should not take the cars."
else:
print "We can't decide."
if trucks > cars:
print "That's too many trucks."
elif trucks < cars:
print "Maybe we could take the trucks."
else:
print "We still can't decide."
if people > trucks:
print "Alright, let's just take the trucks."
else:
print "Fine, let's stay home then."
2 執行結果
PS E:\Python\exercise> python ex30.py
We should take the cars.
Maybe we could take the trucks.
Alright, let's just take the trucks.