可变和不可变
按照存值个数划分
存一个值(数字类型/字符串)
存多个值(列表/字典)
列表内置方法
lt=[1,2,3]
1.索引取值/索引修改值
lt[0]=2
print(lt)
2.索引切片
print(lt[0:1])
3.成员运算 in/not in
print(2 in lt)
print(2 not in lt)
4.for循环
for i in lt:
print(i)
5.长度len
print(len(lt))
6.追加值 append()
s.append(4)
print(lt)
7.排序 sort()
lt=[2,3,1]
lt.sort()
print(lt)
lt=['fsffsf','ffefcx','ewrwer']
lt.sort#按照a-z的字母顺序排的
8.反转 reverse()
lt=[1,2,3]
lt.reverse()
print(lt)
9.获取元素索引 index()
print(lt.index(2))
10.清空列表 clear()
lt.clear()
print(lt)
11.复制 copy()
lt=[1,2,3]
lt1=lt.copy()
print(lt1)
12.扩展列表 extend()
lt1=[1,2,3]
lt2=[1,5,6]
lt1.extend(lt2)
13.移除 remove()
lt=[1,2,3]
lt.remove(3)
14.按索引插入值
lt=[1,2,3]
lt.insert(0,1.5)
字典内置方法
banzhuren_info_dic = {'name': 'guolaoshi', 'height': 150, 'weight': 130,'hobby_list': ['jiao', 'tiao', 'music']}
1.按key取值/按key修改值/按key增加值
print(banzhuren_info_dic['height'])
banzhuren_info_dic['height']=banzhuren_info_dic['height']+1
banzhuren_info_dic['age']=30
2.成员运算(比较的是Key)
print('age' in banzhuren_info_dic)
3.for循环(对key循环)
for i in banzhuren_info_dic:
print(i)
4.keys()/valuea()/items()
print(banzhuren_info_dic.keys())
print(banzhuren_info_dic.values())
print(banzhuren_info_dic.items())
5.取值 get()
print(print(banzhuren_info_dic.get('height',150))#找到了就找到了,没找到就返回None,如果给定了150,没有找到就返回150
6.扩展字典 updata()
dic1={'a':1}
dic2={'b':2}
dil1.updata(dic2)
7.setdefault()有则不更改,没有则增加
dil1={'a':1}
dil1.stdefault('a',2)