广告区
广告区
关注本站官方公众号:程序员总部,领取三大福利!
福利一:python和前端辅导
福利二:进程序员交流微信群,专属于程序员的圈子
福利三:领取全套零基础视频教程(python,java,前端,php)
发布于2020-03-12 16:40 阅读(1110) 评论(0) 点赞(20) 收藏(0)
源码
- #命名函数
- def break_words(stuff):
- """This function will break up words for us."""
- #设置变量words 使用.split()命令分割字符串
- words = stuff.split(' ')
- #返回
- return words
-
- def sort_words(words):
- """Sorts the words."""
- #返回 使用sorted()命令升序排序
- return sorted(words)
-
- def print_first_word(words):
- """Prints the first word after popping it off."""
- #.pop(0)命令删除并返回第一个元素
- word = words.pop(0)
- print(word)
-
- def print_last_word(words):
- """Prints the last word after poping it off."""
- #.pop(-1)命令删除并返回最后一个元素
- word = words.pop(-1)
- print(word)
-
- def sort_sentence(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 自带shell回显
- Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
- Type "help", "copyright", "credits" or "license()" for more information.
-
- >>> import sys
- >>> sys.path.append(r"C:\Users\gw\test")
- >>> sys.path
- ['', 'C:\\Python37\\Lib\\idlelib', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages', 'C:\\Users\\gw\\test']
- >>> import ex25
- >>> sentence = "All good things come to those who wait."
- >>> words = ex25.break_words(sentence)
- >>> words
- ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
- >>> sorted_words = ex25.sort_words(words)
- >>> sorted_words
- ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
- >>> ex25.print_first_word(words)
- All
- >>> ex25.print_last_word(words)
- wait.
- >>> words
- ['good', 'things', 'come', 'to', 'those', 'who']
- >>> ex25.print_first_word(sorted_words)
- All
- >>> ex25.print_last_word(sorted_words)
- who
- >>> sorted_words
- ['come', 'good', 'things', 'those', 'to', 'wait.']
- >>> sorted_words = ex25.sort_sentence(sentence)
- >>> sorted_words
- ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
- >>> ex25.print_first_and_last(sentence)
- All
- wait.
- >>> ex25.print_first_and_last_sorted(sentence)
- All
- who
- >>>
关注本站官方公众号:程序员总部,领取三大福利!
福利一:python和前端辅导
福利二:进程序员交流微信群,专属于程序员的圈子
福利三:领取全套零基础视频教程(python,java,前端,php)
关注公众号回复python,免费领取 全套python视频,回复充值+你的账号,免费为您充值1000积分
作者:听爸爸的话
链接:https://www.pythonheidong.com/blog/article/254428/d0eecd4d86b47cd0a74e/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!