Python organization list

Copyright: from: Jihome https://blog.csdn.net/jihome/article/details/80758987

The list do something.

#coding=utf-8

family=['father','mother','sister','grandfather','grandmother']
print(family)
print(family[1])
print(family[2].title())
print(family[-1])   #负数代表倒数

message="my best love is "+family[2].title()
print(message)
family[2]='yun'
print(family)
family.append('wife')   #在列表最后添加
print(family)
family.insert(3,'me')	#插入一个元素
print(family)
del family[3]	#删除元素
print(family)

old_family=family.pop()	#弹出最后一个元素
print(family)
print(old_family)
family.insert(1,'me')
print(family)
me=family.pop(1)	#弹出任意位置的
print(family)
family.append('wife')
print(family)
family.remove('wife')	#移除指定数据
print(family)

family.sort()	#按字母顺序排序(永久)
print(family)
family.sort(reverse=True)	#字母倒序排列(永久)
print(family)	
family.append('wife')
print(family)
print(sorted(family))	#暂时排序,原顺序不变
print(family)
family.reverse()	#倒序
print(family)
print(len(family))	#列表长度

for home in family:		#千万要记得打冒号
	print(home)				#简单循环
for home in family:
	print(home+" is my family")		#缩进里面的都是for语句
We can see from the code, the basic operation of the list, sort of like ah, finally, there is a simple loop learning, there is depth study of the circulation of the back.

Guess you like

Origin blog.csdn.net/jihome/article/details/80758987