《Python编程从入门到实践》第三章列表简介 笔记及练习

练习

# 练习 3.1
names = ['pig', 'dog', 'cat', 'tiger']
print(names[0])
print(names[1])
print(names[2])
print(names[-1])

for name in names:
    print(name)
# 练习3.2
for name in names:
    print(name+':\ti am so happy')

# 练习3.3
trans = ['by', 'car', 'subway', 'bus']
print(trans[2]+'\tis so convenient')

# 练习3.4
names2 = ['s', 'm', 'l', 'xl', 'xxl']
for name in names2:
    print(f"Dear{name} \n I want to invite you to participate my wedding")

# 练习3.5 修改嘉宾名单
print(names2[-1])
name3 = names2.pop()
print(name3)
names2.insert(4, 'xxxl')
print(names2)


# 练习3.6 添加嘉宾
print('I find a larger table.')
names2.insert(0,'ss')
names2.insert(3,'lx')
names2.append(' xxxxl ')
print(names2)
for name in names2:
    print(f"Dear {name} \n I want to invite you to participate my wedding")

# 练习3.7 缩短名单
print('i am sorry about that i could  invite only two guests')
while(len(names2)>2):
    name4=names2.pop()
    print(f"Dear {name4} i am sorry about that i could invite  only two guests")
# 练习3.8
place = ['Rumo', 'Beijing', 'Washington', 'Huh hot', 'Xi an']
print(place)
print(sorted(place))
print(place)
print(sorted(place,reverse=True))
print(place)
place.reverse()
print(place)
place.reverse()
print(place)
place.sort()
print(place)
place.sort(reverse=True)
print(place)

# 练习3.9
len(place)
# 练习3.10
roommates = ['Dg', 'By', 'Dj', 'Cj']        
print(roommates[3])                   # 读取指定位置元素
roommates[0] = 'Dq'
roommates.append('Mn')                # 在列表末尾插入元素
print(roommates)
roommates.insert(1, 'Cjy')            # 在指定位置插入元素
print(roommates)
del roommates[4]                      # 删除指定位置元素
print(roommates)
roommates.pop()                       # 弹出末尾元素
print(roommates)
roommates.remove('Cjy')               # 移除指定元素
print(roommates)
roommates.append('Cjy')               # 末尾加入指定元素
print(roommates)
print(sorted(roommates))              # 按首字母排序但不改变原序列
print(roommates)
roommates.sort(reverse=True)          # 按首字母逆序排序 改变原序列
print(roommates)
roommates.reverse()                   # 反向打印列表
print(roommates)
print(len(roommates))                 # 输出列表长度

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值