组合数据类型综合练习

1.组合数据类型练习:

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

a="I am a good boy"
b="12143"
#字符串
str0 = a;
print("字符串:",str0)
for i in str0:
    print(i)

#列表
str1=a.split()
print("列表")
for i in str1:
    print(i)

#元组
str2=tuple(str1)
print("元组:",str2)
for i in str2:
    print(i)

#字典
str3=dict(zip(a.split(),list(b)))
print("字典",str3)
for i in str3:
    print(i)

#集合
str4=set(str3.keys())
print("集合",str4)
for i in str4:
    print(i)

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

      列表是可变的对象,可进行动态的增加、删除、更新,即str.pop()和str.extend()等操作,用[]表示。

      元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。

      字典是存储键值对数据的对象,字典的元素都是无序的,且键不能相同,可以通过键,找到值,字典最外面用大括号,每一组用冒号连接起来。

     集合相当于字典的键,也是一个无序不重复的元素集,用一对{}表示。

posted @ 2018-03-22 15:03  201李桂源  阅读(130)  评论(0编辑  收藏  举报