#str #类,字符串 #name = "raitorei" #创建一个对象 #list #类,列表 ##############list类中提供的方法(灰魔法)################## # 对象.方法(..) ,li对象调用append方法 # li = [1,2,3,4] # 1在原来值最后追加 # li.append(5) # li.append([11,22]) # li.append("raito") # print(li) # 2.清空列表 # li.clear() # print(li) # 3.拷贝,浅拷贝 # v = li.copy() # print(v) # 4.计算元素出现的次数 # v = li.count(2) # print(v) # 5.扩展愿列表 # li = [1,2,3,4,5] # li.append([4645,"fdafafd"])#[1, 2, 3, 4, 5, [4645, 'fdafafd']] # li.extend([4645,"fdafafd"])#[1, 2, 3, 4, 5, 4645, 'fdafafd'] # li.extend("风动旛动") #[1, 2, 3, 4, 5, '风', '动', '旛', '动'] # print(li) # 6.根据值获取改值索引位置,左边有限 # li = [1,2,3,4,5,2] # v = li.index(2) # print(v) # 7.在指定位置插入元素 # li = [1,2,3,4,5] # li.insert(0,99) # print(li) # 8.删除某个值并获取当前的值(默认最后一个,可以指定索引) # li = [1,2,3,4,5] # v = li.pop() # v = li.pop(1) # print(li) # print(v) # 9.删除列表中指定的值,左边优先 # li = [1,2,3,4,5] # li.remove(2) # print(li) # ps: pop,remove,del,li[0],del li[7:9],clear # 10.讲列表翻转 # li = [1,2,3,4,5] # li.reverse() # print(li) # 11.排序 # li = [1,2,3,4,5] # # li.sort() # li.sort(reverse=True) # print(li) ###############深灰魔法#################### # 1.列表格式:中括号括起来,英文逗号分割每个元素, # 列表中的元素可以使数字,字符串,列表,布尔值 # 集合内部可以防止任何东西 # li = [1,2,"tsukirei",["轰",["Carole","Tuesday"],"绿谷"],"凌江"] # 2.索引取值 # print(li[2]) # 3.切片取值,切片结果也是列表 # print(li[2:5]) # 4.for循环取值 # for i in li: # print(i) # li = [1,2,"tsukirei",["轰",["Carole","Tuesday"],"绿谷"],"凌江"] # 5.索引修改 # li[1] = 120 # print(li) # li[1] = [1,2,3,4] # print(li) # 6.切片修改 # li[0:1] = [10,20] # print(li) # 7.删除 # del li[1] # print(li) # 8.是否存在某值 # li = [1,2,"tsukirei",["轰",["Carole","Tuesday"],"绿谷"],"凌江"] # v = 1 in li # print(v) # 9.索引嵌套取值 # li = [1,2,9,"tsukirei",["轰",["Carole",[12,13],"Tuesday"],"绿谷"],"凌江"] # print(li[4][1][1][0]) # 10.转换(字符串可以转列表,数字不能直接转列表) # s = "qavapkhvakfjdkdf" # li = list(s) # print(li) # s = 123 # li = list(str(s)) # print(li) # 11.列表转字符串 # li = [1,2,3,"123","raito"] # r = str(li) # print(r) #[1, 2, 3, '123', 'raito'] # 如果有数字和字符串只能自己循环 # s = "" # for i in li: # s += str(i) # print(s) # 如果只有字符串,可以用下面的方法 # li = ["123","raito"] # v = "".join(li) # print(v) #补充:字符串创建不可以修改 # li = [1,2,3,4] # print(li[0]) # li[0] = 546 # print(li) s = "raitorei" print(s[0]) s[0] = "e" #报错 print(s)