python 入门到实践第三章

name = ["alex","crya","toto","hanlun"]
message = "i ilke you , we are fimey "
print(name)
print(name[1])
print(name[2])
print(name[-1])
'''展示裂变内的值'''
print(name[0].title() + message )
print(name[1].title() + message )
print(name[2].title() + message )
print(name[3].title() + message )
'''新增修改删除列表'''
name[0] = "crystal"#需改列表中第一个元素的内容
print(name)
name.append("timi")#列表末尾新增一个元素
print(name)
number = [] # 定义一个空的列表
number.append("0")#新增number列表中的第一个元素
number.append("1")
number.append("2")
number.append("3")
number.append("4")
print(number)
number.append(5)#在列表末尾新增一个元素
print(number)
print(number[-1],number[-2])#打印列表末尾和倒数第二位的元素
motorcycles = ["honda","yamaha","suzuki"]
print(motorcycles)
motorcycles.insert(0,"dycati")#在指定的元素前面插入的元素
print(motorcycles)
del motorcycles[0] #删除指定元素
print(motorcycles)
'''POP()方法删除元素并接着使用'''
motorcycles = ["honda","yamaha","suzuki"]
print(motorcycles)
popdel = motorcycles.pop()# 删除列表最后一条元素,并把值赋给popdel这个变量
print(motorcycles) #打印列表
print(popdel) #打印取出的变量值
motorcycles = ["honda","yamaha","suzuki"]
first_ownde = motorcycles.pop(0) #弹出列表第一个元素,并赋值first_ownde
print("the first motocrcle i owned was a " + first_ownde + ".")
print(motorcycles)
'''根据值删除元素'''
motorcycles = ["honda","yamaha","suzuki"]
print(motorcycles)
motorcycles.remove("yamaha")#根据值删除指定的列表元素
print(motorcycles)
'''列表的排序'''

cars=["bwm","audi","toyota","subaru"]
print(cars)
cars.sort()#列表永久排序
print(cars)
cars.sort(reverse=True)#列表永久排序降序得参数
print(cars)
'''临时排序展示不影响列表顺序'''
print(sorted(cars))#临时排序展示不影响列表顺序
print(sorted(cars,reverse=True))#临时排序展示不影响列表顺序,传入降序参数
'''倒着打印列表'''
print(cars)
cars.reverse()#让列表得顺序相反,是相反不是按字母排序
print(cars)
'''确定列表的长度'''
a=len(cars)
print(a)
 
posted @ 2020-06-16 11:01  网工学搬砖  阅读(113)  评论(0编辑  收藏  举报