# 列表 [ ] 索引取值:取下不取上
# 可重复
list_1 = [1,2,3] list_2 = ["蟹老板","章鱼哥","海绵宝宝",True,list_1,"蟹老板"] print(list_2) print(list_2) # 有序 print(list_2[0]) print(list_2[2]) print(list_2[-1]) print(list_2[0:4]) print(list_2[:-3]) print(list_2[2:]) print(list_2[:]) print("=============") print(list_2[-2][1]) # 取两层列表的值 # 获取长度 list_len = len(list_2) print(list_len) # 取最后一个值 -- 通过长度 print(list_2[list_len - 1]) # 添加值 list_2.append("蛋清") # 插入 list_2.insert(1,"蛋黄") print(list_2) # 修改值 list_2[3] = False print(list_2) # 删除值 list_2.remove(list_1) list_2.remove("蟹老板") print(list_2) # 删除所有 # list_2.clear() # print(list_2) # --> [] list_3 = ["尤加利","露莲"] print(list_2 + list_3) print(list_3 + list_2) print(list_3*2)
print(a.swapcase()) # 大小写互换 print(a.lower()) # 大写变小写 print(a.upper()) # 小写变大写
# 字典 {} 键名一般为字符串,且不能重复
DongDong = {"name":"东东","age":28,"height":188,"sex":"男","mood":"happy"} # 无序 print(DongDong) print(DongDong) # 读取数据 print(DongDong["height"]) print(DongDong.get("height")) # 修改数据 DongDong["name"] = "大东" print(DongDong) # 增加数据 -- 键值对 DongDong["study"] = "Java" print(DongDong) # 获取字典的长度 print(len(DongDong)) # 字典组合 NianNian ={"who":"念念","like":"shutterbug"} DongDong.update(NianNian) print(DongDong)
# 在列表里的字典里添加一个值 # 第一种 list_last = ["hello","python",{"sex":"girl"}] print(list_last) list_last[-1]["name"] = "拍打星" print(list_last) # 第二种 B = list_last[-1] B["name"] = "拍打星" print(list_last) print(B) # 第三种 A = [1,3,5,"哈哈"] dict_A = {"hello":"你好"} A.append(dict_A) dict_A["嘿"] = "你好" print(A) # id 为内存地址 这个与下面不一致 print(id(A)) # 下面两个的id 是一致的 print(id(dict_A)) print(id(A[-1])) # 下面的内存地址 都一致 a = 1 b = a c = b d = 1 e = c print(id(a)) print(id(b)) print(id(c)) print(id(d)) print(id(e)) # 字典组合 # 第一种 B = {"name":"xiaojian","age":28,"is_marry":False} print(B) C ={"role":"teacher","class":"tescher"} B.update({"role":"teacher","class":"tescher"}) print(B) # 第二种 D = dict(B,**C) # dict(B,**C)是两个字典组合 print(D)
# 获取字典value里的列表添加某一个值 print("---------------------") E = {"role":"teacher","class":"tescher","hobby":["chifan","wan"]} print(E) E["hobby"].append("sleep") print(E)
# 元祖 () 有序 只读 不能增删改 不能二次赋值