012_list

#!/usr/bin/env python
# Author:liujun

names = []
# Define an empty list

names = ["ZhangYang", "Guyun", "XiangPeng", "XuLiangchen"]
print(names)

print(names[0])
# Take the first element in the list

print(names[1:3])
# Take continuous elements from the first one to the second one(not the third one,left closed and right open)

print(names[-1])
# Take the last element

print(names[-2:])
# Take the last two elements,

print(names[0:3])
print(names[:3])
# Take the first three elements


names.append("LeiHaidong")
# Append a new element

names.insert(1, "ChenRongHua")
# Insert a new element in a specified location

names[2] = "XieDi"
# replace the second element with "XieDi"

names.remove("ChenRongHua")
# remove an element by name
del names[2]
# delete an element by index
names.pop()
# delete the last element if the parameter is not given,or delete an element by index like del names[2]


print(names)


names = ["ZhangYang", "Guyun", "XiangPeng", "XuLiangchen","ZhangYang1", "Guyun1", "XiangPeng1", "XuLiangchen1","XieDi"]


print(names.index("XieDi"))
# Get the first index of a specified element

print(names.count("GuYun"))
# Count the number of specified element

names.clear()
# empty the list

names = ["ZhangYang", "Guyun", "XiangPeng", "XuLiangchen", "ZhangYang1", "Guyun1", "XiangPeng1", "XuLiangchen1", "XieDi"]
names.reverse()
# Reverse the list

names.sort()
# sort the list











猜你喜欢

转载自www.cnblogs.com/liujun5319/p/9578411.html