Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

ex24.py

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

secret_formula(started)函数的返回值为jelly_beans, jars, crates

所以它到底是个啥,

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

然后是这么个东西:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

这是个元组, 元素不可修改。

结论: return 几个值, 相当于返回了一个包含这些元素的元组。






ex25.py

一:

函数清单:

(1)split()

(2)sorted()

(3)pop()

(4)help()

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

首先进入python:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

载入ex25.py:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

构造一个变量赋值:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

函数:

1、print_last_word(words)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

直观的观察将一个字符串分割返回一个列表。

split()参数

        seq : 可选参数,指定的分隔符,即遇到这个符号就会被分割。不填默认为空格、\n、\t等。

        count : 可选参数, 分割次数, 不填默认将所有分隔符处分割。

spolit()函数的返回值是一个列表。

列表:”列表是Python中存放有序对象的容器,可以容纳任何数据类型:数值、布尔型、字符串等。”

测试split():

(1)默认参数

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)


(2)指定seq参数,默认count参数

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

可以观察出,当某字符被指定为分隔符后,就不被打印。


(3)指定seq参数,指定count参数

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

可以观察到,分割两次,一个string变成了3部分。


(4)不指定seq参数,指定count参数

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

会报错

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

所以split函数指定count参数,必须要指定seq参数。


遇到的其他问题:

(1)ex25.py修改并保存后,在python里引用还是未修改之前的情况。quit()后再进入import ex25即为修改后的。

(2)python似乎有严格的缩进,定义函数时,在制表符后多打了一个空格,会报错:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)



2、sort_words(words)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

观察是输入一个列表,然后按首字母排序,返回值也为一个列表。

sorted(): 参考https://blog.****.net/myarrow/article/details/51200167



3、print_first_word(words)  and  print_last_word(words)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

pop()参数obj :要移除列表元素的对象,0为首对象,-1为尾对象,默认为尾对象。

返回值为要移除的对象。所以再打印列表words如下:

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

首位元素被移除列表。



4、sort_sentence(sentence)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

其实就是先用了split()函数将sentence break并返回一个列表。然后用sorted()函数返回一个排过序的列表。



5、print_first_and_last(sentence)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

无需多言。



6、print_first_and_last_sorted(sentence)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

无需多言.


7、help()

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

    Exercise24、25(return一个元组, split, sorted, pop, help, triple-quotes)

可见为脚本中"""号中的内容。所以""" 可以用来写脚本的帮助信息( documentation comments)。