欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《笨办法学Python》习题24、25

程序员文章站 2022-07-11 17:55:04
...

习题24:更多的练习

这次练习将前面学习的主要内容都有进行了练习和展示

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 explanation
\n\t\t\twhere is none.
""")
#从制表符里面可以知道\t,会出现8个空格
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)
#将secret_formula返回得到的三个值分别赋值给beans、jars、crates,外面的变量才是持久的,而函数内部的变量是可变的、临时的
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))
#将函数中的三个值分别给了前面,这样子省去了设置新变量进行赋值的过程

结果:

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 explanation

                        where 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.

习题25:更多更多的练习

def break_words(stuff):
   """This function will break up words for us."""#充当帮助文档
   words = stuff.split(' ')#split用于切片
   return(words)
   
def sort_words(words):
   """Sorts the words"""
   return(sorted(words))
#sorted() 作为 Python 内置函数之一,其功能是对序列(列表、元组、字典、集合、还包括字符串)进行排序
#基本语法为:list = sorted(iterable, key=None, reverse=False)
   
def print_first_word(words):
   """Prints the last word after popping it off."""
   word = words.pop(0)
   print(word)
#pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
#语法:list.pop(obj=list[-1])       //默认为 index=-1,删除最后一个列表值
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)
#返回有序的列表的前后值

结果为:

>>> sentence = "All good things come to those who wait."
>>> words = pra25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = pra25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> pra25.print_first_word(words)
All
>>> pra25.print_last_word(words)
wait.
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> pra25.print_first_word(sorted_words)
All
>>> pra25.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = pra25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> pra25.print_first_and_last(sentence)
All
wait.
>>> pra25.print_first_and_last_sorted(sentence)
All
who