“笨办法”学Python 3 ——练习25 更更多练习

练习25 源代码

def break_words(stuff):  #定义函数break_words,作用--返回以空格为分隔符切片后的文字列表
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words): #定义sort_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)

将ex25.py作为自定义模块调用,输入如下:

import ex25  #调用自定义模块ex25
sentence = "All good things comes to those who wait." #原始字符串
words = ex25.break_words(sentence)  #将字符串变量以空格为分隔符切片成字符串列表
print(words) #打印字符串列表
sorted_words = ex25.sort_words(words)  #字符串列表按字母排序
print(sorted_words) #打印排序后的字符串列表
ex25.print_first_word(words) #打印字符串列表的第一个字符串
ex25.print_last_word(words) #打印字符串列表的最后一个字符串
print(words)  #打印字符串列表
ex25.print_first_word(sorted_words) #打印排序后的字符串列表的第一个字符串
ex25.print_last_word(sorted_words) #打印排序后的字符串列表的最后一个字符串
print(sorted_words) #打印排序后的字符串列表
sorted_words = ex25.sort_sentence(sentence) #切片且排序后的字符串列表
print(sorted_words) #打印切片且排序后的字符串
ex25.print_first_and_last(sentence) #打印字符串的第一个和最后一个字符串
ex25.print_first_and_last_sorted(sentence) #打印切片且排序后的字符串的第一个和最后一个字符串

输出结果

['All', 'good', 'things', 'comes', 'to', 'those', 'who', 'wait.']
['All', 'comes', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
['good', 'things', 'comes', 'to', 'those', 'who']
All
who
['comes', 'good', 'things', 'those', 'to', 'wait.']
['All', 'comes', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
All
who

知识点:

  1. 如何调用自定义模块? 将自定义函数保存在单独py文件中,再在另外的文件中import自定义模块的文件名。
    具体操作如上例子。
  2. spilt函数:
    (1)作用:通过指定的分隔符和切片数目来对字符串进行切片,分隔成“切片数目+1”个字符串,分隔符数目小于要求的切片数目时,以分隔符为准;
    (2)语法:split(“分隔符”,切片数目),分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。切片数目默认为 -1, 即分隔所有

示例:

a = "It's ***a cute dog."
b = "1+2+34+567+89"

print(a.split()) #分隔符为空格,分割所有
print(a.split("***",5)) #分隔符为***,分割成6个字符串,但是分隔符就一个,按分隔符数量分割

print(b.split())# 分隔符为空格,分割所有
print(b.split("+",3)) #分隔符为+,分割成4个字符串

输出结果

["It's", '***a', 'cute', 'dog.']
["It's ", 'a cute dog.']
['1+2+34+567+89']
['1', '2', '34', '567+89']
  1. pop函数
    (1) 作用:移除列表中的一个元素(默认最后一个元素),并且返回该元素的值;还有针对字典的,这里没涉及;
    (2) 语法:list.pop([index=-1]),参数是要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
    示例:
c = ["apple","Cat","dog","pear","water","coka cola"]

print(c.pop()) #默认移除最后一个
print(c.pop(0))  #根据索引移除第一个
print(c.pop(-2)) #根据索引移除倒数第二个,在移除最后一个的基础上

输出结果

coka cola
apple
pear
  1. sorted函数
    (1)作用:对所有可迭代的对象进行排序操作;
    (2)语法:sorted(iterable, cmp=None, key=None, reverse=False),iterable是可迭代对象,cmp和key先不管。reverse是排序规则,reverse = True是降序 ,False是升序(默认)。
    示例:
d = (1,5,3,2,6,9,7,0,5)
e = ["apple","Cat","dog","pear","water","juice"]
print(sorted(d)) #按顺序排序
print(sorted(e)) #按字母顺序排序
print(sorted(d,reverse = True)) #按逆序排序
print(sorted(e,reverse = True)) #按字母逆序排序

输出结果:

[0, 1, 2, 3, 5, 5, 6, 7, 9]
['Cat', 'apple', 'dog', 'juice', 'pear', 'water']
[9, 7, 6, 5, 5, 3, 2, 1, 0]
['water', 'pear', 'juice', 'dog', 'apple', 'Cat']

附加练习

1.弄明白“你会看到”中各行的作用是什么,确保你理解你是如何在 ex25 模块中运行你的函数的。

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32 #python版本信息
Type "help", "copyright", "credits" or "license" for more information. #可输入help、copyright等函数获取python的更多信息。

2.试试输入 help(ex25) 以及 help(ex25.break_words)(要在交互练习后输入,否则无法成功运行)。注意你是如何获取到关于这个模块的帮助的,以及帮助是如何放在 ex25 的每一个函数后面的 “”" 字符串里的。 这些特殊的字符串被称为文件注释,我们会在后面看到更多。
输入内容:

import ex25
help(ex25)
help(ex25.break_words)

输出结果:

Help on module ex25:

NAME
    ex25 - Created on Tue Jan  3 10:03:33 2023

DESCRIPTION
    @author: ****

FUNCTIONS
    break_words(stuff)
        This function will break up words for us.
    
    print_first_and_last(sentence)
        Prints the first and last words of the sentence.
    
    print_first_and_last_sorted(sentence)
        Sorts the words then prints the first and last one.
    
    print_first_word(words)
        prints the first word after popping it off.
    
    print_last_word(words)
        Prints the last word after popping it off.
    
    sort_sentence(sentence)
        Takes in a full sentence and returns the sorted words.
    
    sort_words(words)
        Sorts the words.

FILE
    c:\users\***\desktop\python3_exercises\ex25.py


Help on function break_words in module ex25:

break_words(stuff)
    This function will break up words for us.
  1. 输入 ex25. 很无聊,可以走个捷径:from ex25 import * ,意思就是从 ex25 导入所有东西,程序员总喜欢倒着说。打开一个新会话,看看你的函数会如何。
    import ex25from ex25 import * 使用方法是一样的,使用方式将以上的源代码

常见问题

  1. 在函数里我什么时候应该用 print 而不是 return 呢?
    通过函数,return 能够给调用这个函数的那行代码返回一个结果,你可以把函数当成通过参数获取输入通过 return 返回输出。print 跟这个就完全不相关了,它只是把输出结果打印到终端。
    解释:
    (1)使用return返回的自定义函数可以赋值给别的变量,但是无法打印出结果,例如:
#在ex25.py中的函数
def sort_words(words): #定义sort_words函数,作用---返回按一定顺序显示的文字列表,并不会实际上改变列表的顺序
    """Sorts the words."""
    return sorted(words)
#调用ex25模块时
sorted_words = ex25.sort_words(words)  #字符串列表按字母排序
print(sorted_words) #打印排序后的字符串列表

(2)未使用return,仅使用print的函数,能够直接打印结果,但是无法赋值给别的变量,可以理解为无实际输出。
解释:

#在ex25.py中的函数
def print_first_word(words): #打印列表中的第一个字符串
    """prints the first word after popping it off."""
    word = words.pop(0)
    print(word)
#调用ex25模块时
ex25.print_first_word(words) #打印字符串列表的第一个字符串
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值