Python学习_2_常用数据类型_List_Tuple_Dict

#1. 元组

#1: 关键字:tuple  符号:()

#2: type(变量名/值) ------> 就可以获取数据的类型

#3: 如果你的元组只有一个数据,请在后面加一个逗号

#4: 元组的数据可以是任何类型

#5: 元组里面的数据是根据逗号来区分的

a = (1, 'hello', (2,3), 3.5)        #元组里是有4个元素

#6: 元组取值

#1) 单个取值: 同字符串的取法 -----> 元组名[所取元素索引]

1 a = (1, 'hello', (2,3), 3.5)    
2 
3 取其中一个元素如:(2,3):
4 
5 print(a[2])
6 print(a[-2])
7 print(a[2,3])

#2)切片:元组进行切片后的数据类型:还是元组

#切片的方法同 字符串

a = (1, 'hello', (2,3,4,5,6), 3.5)    

输出6这个值,怎样切片取?
print(a[2][4])    #正向取值
print(a[-2][-1])   #反向取值

#7: 元组里面的值一旦确定了就不能做修改

a[0] = 2    会报错, 元组不能修改

TypeError: 'tuple' object does not support item assignment

 

#2:列表

#1: 关键字:list  符号:[]

a = [1, 'hello', (2,3), 3.5]   这是列表

a = '[1, 'hello', (2,3), 3.5]'   这是字符串

#2: type(变量名/值) ------> 就可以获取数据的类型

#3: 如果你的列表只有一个数据

a = [1]  仍然是列表

#4: 列表的数据可以是任何类型

#5: 列表里面的数据是根据逗号来区分的, len(变量名/值)

a = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python']   列表有5个元素 
print(len(a))    #结果为:5

#6: 列表取值

#1) 单个取值: 同字符串的取法 -----> 元组名[所取元素索引]

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python'] 
取最后一个元素
print(b[-1])
print(b[4])
print(b[len[b]-1]

#2)切片:列表进行切片后的数据类型:还是列表

#切片的方法同 字符串

#1) 取值: 同字符串的取法 -----> 列表名[所取元素索引]

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python']]
print(b[3:5])
print(b[3:])
print(b[3:6])    #切片不会越界, 没有元素可切了就切空刀~~~~
取出列表中最后一个元素的p字母
print(b[-1][-1][0])

#7: 列表里面的元素支持增删改

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python'] ]
b[2] = 'Tiger' 
修改6所在的位置
b[2][-1] = 'Tiger1’        # 元组不能修改
######
列表里面的元组的元素不能进行修改,可以整体替换掉

a = (1,'hello', (2,3,4,5,6),3.5,[1,2,3])
a[-1] = 'Tiger]
元组里面的列表元素的子元素可以进行修改

#列表的增加元素 列表名.append(你要增加的值) ------>默认加在最后的位置

#insert 插入到指定位置

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python'] ]
b.append('白浅’)
print(b)      #[1, 'hello', (2, 3, 4, 5, 6), 3.5, ['hello', 'python'], '白浅']
b.insert(0,'你好’)
print(b)      #['你好', 1, 'hello', (2, 3, 4, 5, 6), 3.5, ['hello', 'python'], '白浅']

#在最后一个元素列表里插入一个元素
b[-2].insert(-1,'花朵')
print(b)      #['你好', 1, 'hello', (2, 3, 4, 5, 6), 3.5, ['hello', '花朵', 'python'], '白浅']

#删除 列表名. pop()  默认删除最后一个元素  列表名.pop(指定位置)

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python'] ]
b.pop()
print(b)      #[1, 'hello', (2, 3, 4, 5, 6), 3.5]
#指定位置,删除对应位置的元素
b.pop(2)
print(b)       #[1, 'hello', 3.5]

#修改元素  用赋值符号 = 重新赋值修改

b = [1, 'hello', (2,3,4,5,6), 3.5,['hello','python'] ]
b[-1] = 'python学习’
print(b)      #[1, 'hello', (2, 3, 4, 5, 6), 3.5, 'python学习']

 

#字典

#1: 关键字:dict dictionary  符号:{}

c = {'age':18, 'name':'sha'}
c = {"age":18, "name": "sha"}

#字典的key是字符串(str)类型,所以可以用单引号,也可以用双引号)

#2: type(变量名/值) ------> 就可以获取数据的类型

#3: 如果你的字典没有数据或只有一个数据的字典

c = {}      #空字典,也是字典
c = {'age':18}  #只有一个元素的字典

#4: 字典的数据组成: key: value  键值对

#5:字典的数据值:value :可以是任何类型的

#6: 字典里面的数据是根据逗号来区分的,可用 len(变量名/值)求长度

c = {'age':18, 'name':'sha','friends':['li','chenran','xiaohua'], 'score':{'Chinese':99,'math':98,'music':89}}
print(len(c)    #结果为:4

#7: 无切片的用法, 因为字典是无序的

#8: 字典支持增删改

#增加: 字典名[key] = value   不存在的key

#修改: 字典名[key] = value  存在的key就是修改

c = {'age':18, 'name':'sha','friends':['li','chenran','xiaohua'], 'score':{'Chinese':99,'math':98,'music':89}}
c['high'] = 178
print(c)    #{'age': 18, 'name': 'sha', 'friends': ['li', 'chenran', 'xiaohua'], 'score': {'Chinese': 99, 'math': 98, 'music': 89}, 'high': 178}
c['score'] = 100
print(c)    #{'age': 18, 'name': 'sha', 'friends': ['li', 'chenran', 'xiaohua'], 'score': 100, 'high': 178}

#删除   字典名.pop(key)  删除指定的键值对       字典名.clear()  清空字典

c = {'age': 18, 'name': 'sha', 'friends': ['li', 'chenran', 'xiaohua'], 'score': 100, 'high': 178}
c.pop('friends')
print(c)    #{'age': 18, 'name': 'sha', 'score': 100, 'high': 178}

c.clear()
print(c)    #{}

#能根据 value 取 key吗? -----> 不可以

#字典: key是唯一的, valume 不一定, 所以如果写2个相同的key会自动去重,任一取一个值

 

posted @ 2018-07-12 16:05  小白人  阅读(167)  评论(0编辑  收藏  举报