Python List+Tuple+Dict+Set小结

创建List:L = [‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘]

显示List:L[0]

遍历List:print (L)和for循环

更新List:append()和insert()和直接赋值

删除List:pop()和pop(n)

List特点:

可以添加删除

Li = [‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘]

print (Li)

for name in Li:
	print(name)

  

 

创建Tuple:Tu = (‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘)

显示Tuple:Tu[0]

遍历Tuple:print (Tu)和for循环

更新Tuple:不可以更新

删除Tuple:不可以删除

Tuple特点:不可以改变,如果创建之后一个元素的Tuple时,要加","

Tu = (‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘)

print (Tu)

for name in Tu:
	print(name)

  

创建Dict:Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}

显示Dict:Di[‘Adam‘]和Di.get(‘Adam‘)

遍历Dict:print(Di)和for循环

 

Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}

for key in Di:
	print (key)

  

Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}

for key in Di:
	print (Di[key])

  

更新Dict:对应位置赋值即可

删除Dict:

Dict特点:

1:查找速度快

2:浪费空间

3:key不可以重复,且不可变

4:数据无序排放

 

创建set:s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])

显示set:

s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])

if ‘Test‘ in s:
	print (‘Test‘)
else:
	print (‘No‘)

  

遍历set:print (s)

s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])

for name in s:
	print (name)

  

更新set:s.add()

删除set:s.remove()

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。