《笨办法学python3-Learn Python 3 the HARD WAY》-习题25 更多更多的练习

本文介绍了Python3的学习,包括如何进入和退出Python环境,以及split(), sorted(), pop()等关键函数的使用方法和示例。split()用于按指定分隔符分割字符串,sorted()对列表进行排序,pop()用于从列表或字典中删除并返回元素。" 127104524,8195692,Qt实现无边框窗口带阴影效果,"['Qt开发', 'UI界面', '图形渲染']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习内容:

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)
 

运行结果:
进入Python的方法是直接在控制台输入"Python"→回车
退出Python的方法是直接在控制台输入"quit()"→回车
在Python内调用这个脚本的时候输入脚本时不需要在出入.py,因为本身已经在Python内
里面有出错的地方(懒得再运行一遍了)
在这里插入图片描述
知识点:
1.split():
分割字符传,可以在()内指定分隔符。分隔符把字符串分割成若干份,放在list中。需要注意的时分隔符会消失被舍弃掉。
例:split(‘a’, 2)[1]:以a为分隔符,分割2次(2次后后面有a也不再分割)。[1]的意思是取第一个值。2和[1]非必填项。
2.sorted():
Python内置函数,作用是对list进行排序。
①语法(两种都可以):
sorted(interable, cmp = None, key = None,reverse = False)
sorted(interable[, cmp[, key[,reverse]]])
interable:可选迭代对象
cmp:比较函数,具有两个参数,参数的值都是从可迭代对象中取出。
需遵循的原则:大于则返回1,小于则返回-1,等于则返回0.
key:主要用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse:排序规则。reverse为Ture:降序;reverse为False:升序(默认)。
②与list.sort()的区别:
sorted():新建一个新的list;list.sort():对已有的list本身进行操作。
3.pop():
删除字典给定键key所对应的值,反馈值为被删除的值。key必须给出,否则反馈default值。
语法:pop(key[, default])
key:要删除的键值 default:如果没有key,返回default值。
()内没有参数,表示删除list数据组中最后一个元素。
用法:
例:

list1 = ['milk', 'egg', 'bread']
list_pop = list1.pop(0)
print ("Delete the:", list_pop)
print ("The rest of the is:", list1)

结果:
在这里插入图片描述
注释:第2行中,pop(0):指删除第一项,从0开始算
第1行中,创建列表用[],用逗号隔开,每个元素用’'或者"“括起来。
若是要提取最后一个字符可以用pop(-1)
4.”#"加注释解释每个函数的作用

# 分割句子
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) # 调用函数break_words,分割句子
    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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值