读书笔记 | Python学习之旅 Day11

Python学习之旅 读书笔记系列

Day 11

《Python编程从入门到实践》

复盘:第一部分基础知识(第1章~11章)

今天继续动手做练习题。

练习题

第三章 列表

1.names.py

通过应用列表,获取相对索引位置的值(要留意索引从0开始计数)

names = ["smile","eric","johnny","mark"]
message = names[0].title()
print(message)
message = names[3].upper()
print(message)
message = "Hello,my boss is " + names[1].title() + "."
print(message)
message = "Hello,my boss is " + names[2].title() + "."
print(message)

trafic_type = ["bike","car","walk","Motorcyle"]
#一定要留意,索引是从0开始计数的
message = "I would like to own a Honda " + trafic_type[3].lower()
print(message)

输出结果如下:


day11_01.png

2.guests_list.py

通过操作列表,删除添加以及弹出操作,同时套用后面的循环练习

guests_list = ["wendy","smile","yan.zou","guiming.shen","q.wang"]
#留意\n之前是“\“
print(guests_list)
print("-------")
guests_cancel = guests_list.pop(3)
#留意del,pop,remove后面索引值用小括号即可
message = guests_cancel.title() + " can't intend the dinner."
print(message)
print(guests_list)
print("-------")

guests_list.insert(3,"zuntao.qin")
while guests_list:
    guest = guests_list.pop()
    message = "Welcome " + guest + " to have dinner."
    print(message)
print("-------")

guests_list = ["wendy","smile","yan.zou","guiming.shen","q.wang"]
guests_list.insert(0,"minghong.tang")
guests_list.insert(2,"Tom")
guests_list.append("Last guest")
guests_list.remove("guiming.shen")
while guests_list:
    guest = guests_list.pop()
    message = "Welcome " + guest + " to have dinner."
    print(message)
print("-------")

输出结果如下:


day11_02.png

3.travel_world.py

travel_world = ["Taiwan","Europe","Yunnan","Xizang","Guizhou"]
print(travel_world)
travel_world.sort()
print(travel_world)
travel_world.sort(reverse=True)
print(travel_world)
#reverse与上面参数设定的区别在于原顺序的倒序,非字母的倒序
travel_world.reverse()
print(travel_world)
travel_world.reverse()
print(travel_world)
travel_world.sort()
print(travel_world)
length_test = len(travel_world)
print(length_test)

输出结果如下:


day11_03.png
其他
  • 感受
    • 写的时候容易出错,一些细节以为自己看书掌握了,实际出错几率高
    • 很多知识在屏幕上实现是另一种感受,跟看的区别很大

你可能感兴趣的:(读书笔记 | Python学习之旅 Day11)