# 数据类型(8) # int #123,456 # float #2.3 # str #strs = 'heLLo world' #大小写 # 转小写 #print(strs.lower()) # 转大写 #print(strs.upper()) # 首字母大写 #print(strs.title()) # 大小写互转 #print(strs.swapcase()) #字符串替换 #print(strs.replace('world','Tom')) # 字符串拆分 #print(strs.split('')) #字符串去除左右特殊符号或空格 #print(strs.strip(),rstrip('G')) #strs = '中国' #GBK #utf8 #print(strs.encode('utf8').decode('utf8')) #print(strs.encode('GBK').decode('utf8'))#'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte #print(strs.encode('GBK').decode('GBK')) #print(b'\xe4\xb8\xad\xe5\x9b'.decode('utf8',errors='ignore')) # #bool # list 可变 lists = [1,2,3,4] # 增 #方法1 #lists.append(5) #print(lists) #方法2 #print(lists+[5]) #方法3 #lists.extend([5]) #print(lists) #方法4 #lists.insert(2,'index') #print(lists)#[1, 2, 'index', 3, 4] #删 #方法1 #data = lists.pop() #print(data,lists)#4 [1, 2, 3] #data = lists.pop(2) #print(data,lists)#3 [1, 2, 4] #方法2 #del lists[2] #print(lists)#[1, 2, 4] #方法3 #lists.clear() #print(lists)#[] #方法4 #lists[:] = '' #print(lists)#[] #方法5 #lists.remove(3) #print(lists)#[1, 2, 4] #改 #lists[2] = 'hello world' #print(lists)#[1, 2, 'hello world', 4] #查 #print(lists[2])#3 #print(lists[100])#IndexError: list index out of range #print(lists[1:3])#[2, 3] #print(lists[100:300])#[] #for i in lists: # print(i) #for i in range(len(lists)): # print(lists[i]) # dict 可变 #dicts ={'hello':'world',(1,2,3):122,234:{1:144},'hello':444} dicts ={'hello':'world',(1,2,3):122,234:{1:144}} #print(dicts)#{'hello': 444, (1, 2, 3): 122, 234: {1: 144}} #增 #方法1 #dicts['lalala']= 'hahaha' #方法2 #dicts.update({'lalala':'hahaha'}) #删 #方法1 #data = dicts.pop('hello') #方法2 #del dicts['hello'] #改 #['hello'] = 'nihao' #查 #print(dicts.keys(),dicts.values()) #for i in dicts.keys(): # print(i) #print(list(dicts.keys())[0]) #方法一 #for i in dicts: # print(i,dicts[i]) #方法二 #for key,values in dicts.items(): # print(key,values) #print(dicts.items()) #list1 = [[1,2],[3,4],[5,6]] #for a,b in list1: # print(a,b) # set 可变 sets = {1,2,3,4,'hello',3,3} #增 #sets.add(123) #删 #方法1 #sets.pop()#{2, 3, 4, 'hello'} #sets.pop(2)#TypeError: pop() takes no arguments (1 given) #方法2 #sets.remove(2)#{1, 3, 4, 'hello'} #查 #set1 ={1,2,3} #set2 ={2,3,4} #差集 #print(set1-set2) #并集 #print(set1|set2) #补集 #print(set1^set2) #交集 #print(set1&set2) # tuple #tuple1 = (1,2,3,[1,2,3]) #print(tuple1,type(tuple1)) #data = tuple1[0] #print(data) #如果tuple1[0]为4#tuple1[0] = 123#TypeError: 'tuple' object does not support item assignment #tuple1[-1][0] = 123#(1, 2, 3, [123, 2, 3]) #print((1),type((1)))#1 <class 'int'> #print((1,),type((1,)))#(1,) <class 'tuple'> #字符串格式化 strs = '你好%s,你的支付宝已逾期'
TJ20240604
于 2024-06-05 18:49:16 首次发布