python编程:从入门到实践习题第三章3-8~3-10

    xiaoxiao2022-07-12  148

    3-8 放眼世界: 想出至少 5个你渴望去旅游的地方。

     将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。

     按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始 Python列表。

     使用 sorted()按字母顺序打印这个列表,同时不要修改它。

     再次打印该列表,核实排列顺序未变。

     使用 sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它。

     再次打印该列表,核实排列顺序未变。

     使用 reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。

     使用 reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来 的排列顺序。

     使用 sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺 序确实变了。

     使用 sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表, 核实排列顺序确实变了。

    destination=[‘daqing’,‘haerbin’,‘yichun’,‘shanghai’,‘kedong’]

    print(destination)

    print(sorted(destination))

    print(destination)

    print(sorted(destination,reverse=True))

    print(destination)

    destination.reverse()

    print(destination)

    destination.reverse()

    print(destination)

    destination.sort()

    print(destination)

    destination.sort(reverse=True)

    print(destination)

    3-10 尝试使用各个函数: 想想可存储到列表中的东西,如山岳、河流、国家、城 市、语言或你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列表, 然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表。

    things_I_love=[‘eat’,‘drink’,‘sleep’]

    things_I_love.append(‘china’)

    print(things_I_love)

    things_I_love.insert(4,‘shit’)

    print(things_I_love)

    del things_I_love[4]

    print(things_I_love)

    most_favorite=things_I_love.pop()

    print(most_favorite)

    print(things_I_love)

    hahaha=things_I_love.pop(1)

    print(hahaha)

    print(things_I_love)

    things_I_love.remove(‘eat’)

    print(things_I_love)

    things_I_love.insert(0,‘china’)

    print(things_I_love)

    things_I_love.insert(0,‘drink’)

    print(things_I_love)

    things_I_love.insert(0,‘eat’)

    print(things_I_love)

    things_I_love.sort()

    print(things_I_love)

    things_I_love.sort(reverse=True)

    print(things_I_love)

    print(sorted(things_I_love))

    print(sorted(things_I_love,reverse=True))

    things_I_love.reverse()

    print(things_I_love)

    print(len(things_I_love))

    最新回复(0)