自学内容网 自学内容网

Python入门第二课

#Python 第一课 列表简介
message = ['yy','hdu','dcscxx']
print(message[0].title())
#对比其他不同的语言 神奇在于索引为-1时,它是直接访问最后一个元素,-2表示倒数第二个数,.......以此类推
print(message[-1])
#练习
name = ['wang wu','li si']
wq="hello"
print(name[0])
print(name[1])
print(f"{name[0]} {wq}")
print(f"{name[1]} {wq}")
# 修改
way=['motuo','bike','bus']
print(way)
way[0]='boat'
print(way)
# 添加 末尾添加 使用appdend方法
# 先创建一个空列表 然后用append 依次添加
# 随意插入在列表当中 使用insert 需要提供索引和插入的内容
way=['motuo','bike','bus']
way.append('train')
print(way)
way1 = []
way1.append('n')
way1.append('p')
print(way1)
way1.insert(0,'wangjh')
print(way1)
# 删除元素 可以使用del方法 只需要知道他的索引
# 不过他的使用还不太一样 例 del 列表
# 除此之外 还有一种 pop方法删除元素
way2 = ['laji','hhjj','fshjhf']
print(way2)
del way2[2] #无法表示删除的什么
print(way2)
wang=way2.pop(0)#表示删除什么元素
print(way2)
print(wang)
# 根据值删除元素
# remove(’值‘) 注意只能删除一个值 多个值 要使用for循环
way3 = ['www','qqqqq','rrrrr']
print(way3)
xiaoshi = 'rrrrr'
way3.remove('www')
print(way3)
way3.remove(xiaoshi)
print(way3)
# 练习题
relation = ['dog','cat','pig']
print(relation)
likai = relation.pop(1)
print(f"{likai} is not going to here")
relation.insert(1,'hourse')
print(f"Welcome to here {relation}")
relation.insert(0,'wangwu')
relation.insert(2,'lisi')
relation.append('zhangsan')
print(f"Welcome to here {relation}")
print(f" only is two people")
ll=relation.pop(0)
print(f"Sorry {ll}")
ll=relation.pop(0)
print(f"Sorry {ll}")
ll=relation.pop(0)
print(f"Sorry {ll}")
ll=relation.pop(0)
print(f"Sorry {ll}")
print(f"Welcome to here {relation[0]}")
print(f"Welcome to here {relation[1]}")
del relation[0]
del relation[0]
print(relation)
# 管理列表
# sort()方法 永久的改变 应该是按照一个一个的字母顺序比较的
way4 = ['kssa','fsadfs','fsf']
print(way4)
way4.sort()
print(way4)
# 如果相反排序 使用  reverse=True
way4.sort(reverse=True)
print(way4)
# 临时改变顺序 sorted(way4)
print(sorted(way4))
print(way4)
# 反向打印列表 永久性改变
way4.reverse()
print(way4)
# 列表长度 len(way4) 使用
print(len(way4))
# 例题
trip=['linyi','qingdao','laanzhou','beijing','shanghai']
print(trip)
print(sorted(trip))
print(trip)
print(sorted(trip))
print(trip)
trip.reverse()
print(trip)
trip.reverse()
print(trip)
print(trip.sort())
print(trip.sort(reverse=True))
# 列表结束


原文地址:https://blog.csdn.net/qq_57298535/article/details/140726239

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!