《笨办法学Python3》练习二十五:更多,更多的练习

练习代码

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 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_first_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文件ex5c.py,方便一次出结果查看

import ex25

sentence = "All good things come to those who wait."

words = ex25.break_words(sentence)
print(words)
print("")

sorted_words = ex25.sort_words(words)
print(sorted_words)
print("")

ex25.print_first_word(words)
ex25.print_last_word(words)
print(words)
print("")

ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
print(sorted_words)
print("")

sorted_words = ex25.sort_sentence(sentence)
print(sorted_words)
print("")

ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)

Study Drills

  1. Take the remaining lines of the What You Should See output and figure out what they are doing. Make sure you understand how you are running your functions in the ex25 module.

  2. Try doing this: help(ex25) and also help(ex25.break_words). Notice how you get help for your module and how the help is those odd """ strings you put after each function in ex25? Those special strings are called documentation comments, and we’ll be seeing more of them.

  3. Typing ex25. is annoying. A shortcut is to do your import like this: from ex25 import *.
    This is like saying, “Import everything from ex25.” Programmers like saying things backward. Start a new session and see how all your functions are right there.

  4. Try breaking your file and see what it looks like in python when you use it. You will have to
    quit python with quit() to be able to reload it.

补充

  1. 本章练习实际上介绍了几个常用函数,并对他们进行封装:
  • list.pop(index)

    • index:按索引pop元素,不填时pop最后一个元素
    • pop是在原对象上操作,直接改变原对象
  • string.split(sep=None, maxsplit=-1)
    有两个参数:

    • sep:分隔符,可以是单个字符,也可以是字符串。默认为None,若原字符串由空格分隔时不填时会以空格分隔。
    • maxsplit:最大分隔次数,不填或-1时默认为分隔到底。
    • 举例:
> s = "pen pineapple apple pen"

> s.split()
['pen', 'pineapple', 'apple', 'pen']

> s.split('p')
['', 'en ', 'inea', '', 'le a', '', 'le ', 'en']

> s.split('p', 2)
['', 'en ', 'ineapple apple pen']

> s.split(None, 2)
['pen', 'pineapple', 'apple pen']
  • sorted(iterable, key=None, reverse=False)
    • iterable:可迭代对象,在本练习中是string
    • key:选择排序的参考key,默认是英文字典序
    • reverse: 是否反序,默认正序
    • 使用sorted排序对象,会先拷贝对象,在拷贝上排序,返回,不会对原来的对象有影响。

你可能感兴趣的:(《笨办法学Python3》练习二十五:更多,更多的练习)