“笨办法”学Python3--习题24、25 更多的练习

目录

 

函数返回值练习

定义函数的文件 ex1.py

执行文件/Python会话

会话执行结果

pop与split与sorted


函数返回值练习

def secret_formula(start):
    jelly_beans = start*500
    jars=jelly_beans / 1000
    crates = jars / 1000
    return jelly_beans,jars,crates

start_point = 10000
beans,jars,crates = secret_formula(start_point)
print(beans,jars,crates)
print("With a starting point of : {}".format(start_point))

定义函数的文件 ex1.py

#ex1.py
def break_words(stuff):
    """this function will break up words for us."""
    words = stuff.split(' ')
    #通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
    return words

def sort_words(words):
    """sorts the words"""
    return sorted(words)
# sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
# list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,
# 而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
def print_first_word(words):
    """print first word after poping it off."""
    word = words.pop(0)
    # pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
    print(word)

def print_last_word(words):
    """print last word after poping it off."""
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    """take in a full sentence and return the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """print 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会话

# ex2.py
import ex1
sentence="All good things come to those who wait."
words=ex1.break_words(sentence)
words
sorted_words=ex1.sort_words(words)
sorted_words
ex1.print_first_word(words)
ex1.print_last_word(words)
words
ex1.print_first_word(sorted_words)
ex1.print_last_word(sorted_words)
sorted_words
sorted_words=ex1.sort_sentence(sentence)
sorted_words
ex1.print_first_and_last(sentence)
ex1.print_first_and_last_sorted(sentence)

会话执行结果

PS C:\Users\ASUS\Desktop> python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex1
>>> sentence="All good things come to those who wait."
>>> words=ex1.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words=ex1.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex1.print_first_word(words)
All
>>> ex1.print_last_word(words)
wait.
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex1.print_first_word(sorted_words)
All
>>> ex1.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words=ex1.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex1.print_first_and_last(sentence)
All
wait.
>>> ex1.print_first_and_last_sorted(sentence)
All
who

pop与split与sorted

.split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串

.pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

.sort() 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
 list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,
 而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值