《笨方法学Python》加分题38

加分练习
将每一个被调用的函数以上述的方式翻译成 python 实际执行的动作。例如 ' '.join(things) 其实是 join(' ', things) 。
将这两种方式翻译为自然语言。例如 ' ".join(things) 可以翻译成 “用 ’ ’ 连接(join) things”,而 join(' ', things) 的意思是 “为 ’ ’ 和 things 调用 join 函数”。
在网上阅读一些关于 “面向对象编程 (Object Oriented Programming)” 的资料。不要怕看着晕,其实大家开始都晕,而《笨办法学 python》 会教给我们足够的知识逐步掌握它
了解 Python 的 class 是什么东西。一定是 Python 的哦
了解 dir(something) 和 something 的 class 有什么关系?
Zed 建议,如果实在皋懂 面向对象编程(OOP)可以学习以下 “函数编程 (functional programming)”

我的答案

38.0 基础练习

 1 ten_things = "Apples Oranges Crows Telephone Light Sugar"
 2 
 3 print("Wait there are not 10 things in that list. Let's fix that.")
 4 
 5 # 字符串的 split 方法会把字符串按照参数隔开生成一个列表
 6 stuff = ten_things.split(' ')
 7 # print(stuff)
 8 more_stuff = ["Day", "Night", "Song", "Frisbee",
 9                 "Corn", "Banana", "Girl", "Boy"]
10 
11 # print(">>>>>> ten_things length:", len(ten_things))
12 
13 # 不断从 more_stuff 中取出元素加入 stuff 中直到
14 # stuff 的长度等于10,确认 while 循环会正确结束
15 while len(stuff) != 10:
16     next_one = more_stuff.pop()
17     # print(more_stuff)
18     print("Adding: ", next_one)
19     stuff.append(next_one)
20     print(f"There are {len(stuff)} items now.")
21 
22 print("There we go: ", stuff)
23 
24 print("Let's do some things with stuff.")
25 
26 print(stuff[1])
27 print(stuff[-1])   #whoa! fancy
28 print(stuff.pop())
29 print(' '.join(stuff))  # what? cool!
30 print('#'.join(stuff[3:5]))  # super stellar!

运行结果

38.1 + 38.2 翻译函数语句
stuff = ten_things.split(' ') 翻译: ten_things 使用空格 分割为列表
实际执行 stuff = split(ten_things, ' ' ) 翻译: 为 ten_things 和 调用 split 函数

next_one = more_stuff.pop() 翻译:more_stuff 使用 抛出方法
实际执行 pop(more_stuff) 翻译:调用 pop 函数,参数是 more_stuff

stuff.append(next_one) 翻译:为 stuff 末尾添加 next_one
实际执行 append(stuff, next_one) 翻译:为 stuff 和 next_one 调用 append 方法

stuff.pop() 翻译: 为 sutff 调用 pop 方法
实际执行 pop(suff) 翻译:调用 pop 函数,参数 suff

原文地址:https://www.cnblogs.com/python2webdata/p/10813772.html