组合数据类型综合练习

1.组合数据类型练习:

分别定义字符串,列表,元组,字典,集合,并进行遍历。

字符串:

>>> str = 'csdasd'
>>> for i in str:
	print(i)

	
c
s
d
a
s
d

  

  

列表:

>>> ls = ['1','2','3','446']
>>> for i in ls:
	print(i)

	
1
2
3
446

  

元组:

>>> turtle = ('da', 'ds', 'sd')
>>> for i in turtle:
	print(i)

	
da
ds
sd

  

字典:

>>> dict = {'Alice':'123', 'Bac':'113','Casd':'325'}
>>> for i in dict:
	print(i+':'+dict[i])

	
Alice:123
Bac:113
Casd:325

  

集合:

>>> b = set(['sd', 'fd', 'wqe'])
>>> for i in b:
	print(i)

	
fd
sd
wqe

  

总结列表,元组,字典,集合的联系与区别。

列表和元组可以说基本一样,不一样的是列表可以用extend、insert、pop、append来添加,插入,删除元素。

而字典是以键值对来存放数据,集合却是相当于字典的键值“key”。

原文地址:https://www.cnblogs.com/-hjd/p/8626206.html