Learn Python The Hard Way 习题41详解

Learn Python The Hard Way 习题41详解

标签: Python 博客


博主最近在学习Python,看的书是Learn Python The Hard Way(Third Edition), 前40道习题没有什么难度,但是看到习题41的时候,由于出现了很多新函数和新名字以及印刷错误,竟然没看懂这道题的目的。查询了一些函数的用法之后,现在把这道题搞清楚了,分享出来,希望能对其他新手有所帮助。

1、印刷错误

译者:王巍巍
版次:2014年11月第1版
印刷时间:2015年5月北京第3次印刷
具体错误:


#fake class names

for word in class_names:
    result = result.replace("%%%", word, 1)

# fake other names

for word in other_names:
    result = result.replace("***", word, 1)

# fake parameter lists

for word in param_names:
    result = result.replace("@@@", word, 1)

results.append(result)

上面的一段代码在书中是Line47~Line59,这一段代码应该全部再缩进一格,即这段代码处于for sentence in snippet, phrase:的循环中,具体可以参考Exercise 41: Learning To Speak Object Oriented

2、本节新函数介绍

2.1strip()

首次出现位置:Line30
函数原型:str.strip([chars])
参数:chars – 移除字符串头尾指定的字符,默认为空格
返回值:返回移除字符串头尾指定的字符生成的新字符串,移除的是chars中的任意字符
实例:

>>> a = "     123"
>>> print a.strip()
123
>>> a ="123abc"
>>>print a.strip("1cb")
23a

2.2capitalize()

首次出现位置:Line34
函数原型:str.capitalize()
参数:None
返回值:返回一个首字母大写,其余字母小写的字符串
实例:

>>> a = "this is a book called HARRY POTTER"
>>> print a.capitalize()
This is a book called harry potter

2.3random.sample()

首次出现位置:Line35
函数原型:random.sample(sequence, k)
参数:sequence是一个list,k是一个整数
返回值:返回一个list,该list由sequence中随机的k个元素组成,sequence不变
实例:

>>>list = [1,2,3,4,5,6,7,8,9,10]
>>>slice = random.sample(list,5)
>>>print slice
[3,6,8,2,4]#截取的序列元素并没有顺序

2.4count()

首次出现位置:Line35
函数原型:str.count(sub, start= 0,end=len(str))
参数:

  • sub – 搜索的子字符串
  • start – 字符串开始搜索的位置,默认为第一个字符,第一个字符索引值为0
  • end – 字符串中结束搜索的位置,字符中第一个字符的索引为0,默认为字符串的最后一个位置

返回值:返回子字符串在字符串中出现的次数
实例:

>>>a = "this is a book called HARRY POTTER"
>>>print a.count("i")
2

2.5join()

首次出现位置:Line42
函数原型:str.join(sequence)
参数:sequence – 要连接元素的list
返回值:返回通过str连接序列中元素后生成的字符串
实例:

>>>str = "-"
>>>seq = ["a","b","c"]
>>>print str.join(seq)
a-b-c

2.6replace()

首次出现位置:Line49
函数原型:str.replace(old, new[, max])
参数:

  • old – 将被替换的子字符串。
  • new – 新字符串,用于替换old子字符串。
  • max – 可选字符串, 替换不超过 max 次

返回值:返回以new代替old不超过max次的新字符串
实例:

>>>a = "this is a book called HARRY POTTER"
>>>print a.replace("is", "was", 1)
thwas is a book called HARRY POTTER
>>>print a.replace("is", "was")
thwas was a book called HARRY POTTER

2.7keys()

首次出现位置:Line67
函数原型:dict.keys()
参数:None
返回值:返回一个字典所有的键
实例:

>>>dict = {
  "name":"moverzp", "age":"23", "height":"180"}
>>>print dict.keys()
['age','name','height']

2.8shuffle()

首次出现位置:Line68
函数原型:random.shuffle(lst)
参数:lst – 可以是一个序列或者元组
返回值:None. 直接修改lst的顺序
实例:

>>>list = [1, 2, 3, 4]
>>>random.shuffle(list)
>>>print list
[3, 1, 4, 2]
>>>random.shuffl
  • 40
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 45
    评论
Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called 'The Hard Way,' but it's really quite simple. What's 'hard' is this: it requires discipline, practice, and persistence. Zed A. Shaw teaches the Python programming language through a series of 52 brilliantly-crafted exercises-all formatted consistently, and most no longer than two pages (including 'extra credit'). Just read each exercise, type in its sample code precisely (no copy-and-paste!), and make the programs run. As you read, type, fix your mistakes, and watch the results, you'll learn how software works, how programming works, what good programs look like, and how to read, write, and see code. You'll discover how to spot crucial differences that fundamentally affect program behavior, and you'll learn everything you need to know about Python logic, input/output, variables, and functions. Above all, you'll learn the attention to detail that is indispensable to successful programming (and so much else in life). At first, yes, it can be difficult. But it gets easier. And Shaw offers plenty of extra guidance and insight through 5+ full hours of teaching video on the accompanying CD. As Shaw's thousands of online readers and fans will attest, the moment will come when you just 'get it'-and that moment feels great. Nothing important comes without discipline, practice, and persistence. But, with Learn Python the Hard Way, readers who bring those qualities to programming will master it-and they will reap the rewards, both personally and in their careers.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值