复习PythonDay23
发布日期:2021-07-01 03:03:01
浏览次数:3
分类:技术文章
本文共 3000 字,大约阅读时间需要 10 分钟。
复习Python的第二十三天
习题 24: 更多练习
本节没有什么新的东西,是一个比较长的练习
The Codes
print "Let's practice everything." print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' poem = """ \tThe lovely world with logic so firmly planted cannot discern \n the needs of love nor comprehend passion from intuition and requires an explanation \n\t\twhere there is none. """ print "--------------" print poem print "--------------" five = 10 - 2 + 3 - 6 print "This should be five: %s" % five def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates start_point = 10000 # 调用secret_formula函数,返回三个值,分别赋予beans,jars,cratesbeans, jars, crates = secret_formula(start_point) print "With a starting point of: %d" % start_point print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) start_point = start_point / 10 print "We can also do that this way:" # 下面的函数返回值分别对三个格式化字符print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
习题25:更多更多的练习
这节比较有趣。
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)
本节知识:
- import的时候不用加“.py”的后缀,此时作为模块的程序文件被编译成为字节码,后缀变为“.pyc”;
- 使用ex25调用函数时用. (dot, period)调用ex25模块中的函数,如:
ex25.break_words
; - 列表是一组元素的有序集合,属于序列,用[ ]引起;
- sorted()是内置的排序函数,传入可迭代对象,reverse可以选择是否翻转序列;
Help on built-in function sorted in module builtins:sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
- 错写成wrods的提示信息
NameError
很常见,这里不做叙述; - pop()函数意为删除指定位置的元素并返回该元素;
Help on built-in function pop:pop(...) method of builtins.list instance L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
- 帮助文档是定义函数时放在 “”” 之间的东西,一般跟在函数定义的下一行,它们也被称作 documentation comments (文档注解),我们使用help()时看到的文档就来自这里;
- from ex25 import * 会挤占命名空间,也会让人不清楚哪个函数出自哪个模块,会使程序逻辑不清晰,不建议使用。
转载地址:https://memcpy0.blog.csdn.net/article/details/79831932 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月20日 12时01分53秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Scala学习第十一天 Scala中的apply实战详解
2019-05-03
Scala学习第七天 Scala类的属性和对象私有字段实战详解
2019-05-03
Scala学习第六天 Map、Tuple、Zip实战解析
2019-05-03
Scala学习第四天 Scala的For与Function进阶实战、Lazy的使用
2019-05-03
Scala学习第三天 Tuple、Array、May与文件操作入门实战
2019-05-03
Scala学习第二天 Scala函数定义、流程控制、异常处理
2019-05-03
Scala学习第五天 Scala数组操作实战详解
2019-05-03
基于key-value的存储系统Redis
2019-05-03
Scala学习第十二天 Scala中的继承:超类的构造、重写字段、重写方法代码实战
2019-05-03
Scala学习第十三天 抽象类、抽象字段、抽象方法
2019-05-03
Scala学习第十四天 Scala中作为接口的trait、在对象中混入trait代码实战
2019-05-03
Scala学习第十五天 Scala多重继承、多重继承构造器执行顺序及AOP实现
2019-05-03
剑指offer:栈的压入、弹出序列(java)
2019-05-03
剑指offer:字符串的组合(java)
2019-05-03
剑指offer:数组中出现次数超过一半的数字(java)
2019-05-03
实时开发框架Meteor API解读系列<二>Core
2019-05-03
实时开发框架Meteor API解读系列<六> DDP
2019-05-03
实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
2019-05-03
实时开发框架Meteor API解读系列<七> Collection --01
2019-05-03
启用fcitx-qimpanel面板程序
2019-05-03