编码:
1,is ==
== 比较的是值.
is 内存地址.
对于int str 来说:存在小数据池的概念.
为什么存在小数据池? 节省内存.
int -5 ~ 256
str 全部字母,数字与单个字母想乘 数字不超过20.
其他数据类型:没有小数据池的概念,
l1 = [1] l2 = [1]
大环境:python3 :
python3 中:str 内存中的编码 unicode
如果我想存储,或者传输字符串(文件)能直接用str? 不能
解决: 出现了bytes类型.
对于英文:
str: 表现形式: s = ‘alex‘
内存编码:unicode
bytes: 表现形式: s = b‘alex‘
内存编码:非unicode(可以是utf-8,gbk,gb2312...)
对于中文:
str: 表现形式: s = ‘中国‘
内存编码:unicode
bytes: 表现形式: s = b‘xe3\xf2\......‘
内存编码:非unicode(可以是utf-8,gbk,gb2312...)
你想存储,传输一个文件,或者字符串:str ---> bytes
str.encode(‘utf-8‘) ----bytes
内容补充:
int、str、tuple:
#int
# str
s = ‘ ‘ #只能是以至少一个空格组成的字符串(全空格)
# print(s.isspace())
#tuple
# tu = (1)
# tu1 = (1,)
# print(tu,type(tu))
# print(tu1,type(tu1))
# tu = (‘alex‘)
# tu1 = (‘alex‘,)
# print(tu,type(tu))
# print(tu1,type(tu1))
tu = ([1,2,3])
tu1 = ([1,2,3],)
print(tu,type(tu))
print(tu1,type(tu1))
#当元组只有一个元素组成并且没有‘,‘
# 则该元素是什么数据类型,整体就是什么数据类型.
tu.append(‘333‘)
print(tu)
list、dict:
#list
l1 = [111,222,333,444,555]
# for i in range(len(l1)):
# # i = 0 i = 1 i = 2 i = 3
# print(l1)
# del l1[i]
# print(l1) # [222,333,444,555] [222,444,555] [222,444]
# print(l1)
# l1 = [111,222,333,444,555] 将索引为奇数位的元素删除.
# l1.pop(222)
# l1.remove(1)
# 方法一:
# l1 = [111,222,333,444,555]
# l2 = []
# for i in range(len(l1)):
# if i % 2 == 0:
# l2.append(l1[i])
# l1 = l2
# print(l1)
#方法二:
# l1 = [111,222,333,444,555]
# for i in range(1,len(l1)):
# if i <= 2 :
# l1.pop(i)
# print(l1)
#方法二:
# l1 = [111,222,333,444,555,666,777]
# #
# del l1[1::2]
# print(l1)
# l1 = [111,222,333,444,555]
# for i in range(len(l1)-1,0,-1):
# if i % 2 == 1:
# del l1[i]
# print(l1)
#在循环一个列表时,如果要是删除某些或者某类元素,容易出错.
#fromkeys()
# dic = dict.fromkeys(‘abc‘,‘alex‘)
# print(dic)
# dic1 = dict.fromkeys([1,2,3],[])
# print(dic1)
# dic1[1].append(‘alex‘)
# print(dic1)
dic = {‘k1‘:‘alex‘,‘k2‘:‘太白‘,‘k3‘:‘日天‘,‘name‘:‘wusir‘}
#不可变的数据类型:可哈希
# for i in dic:
# if ‘k‘ in i:
# del dic[i]
# l1 = []
# for i in dic:
# if ‘k‘ in i:
# l1.append(i)
# print(l1)
#
# for k in l1:
# del dic[k]
# print(dic)
#在循环一个字典时,如果删除某些键值对,可能会报错.
#数据类型之间的转化:
#int <---> str
#int <---> bool
#str <--->.bool
# split
#str <------> list
# join
# tuple <---> list
# l1 = [1,2,3]
# tu = tuple(l1)
# l2 = list(tu)
# print(tu,l2)
#dict专属类型:与list转换
# dic = {‘k1‘:‘alex‘,‘k2‘:‘太白‘,‘k3‘:‘日天‘,‘name‘:‘wusir‘}
# print(list(dic.keys()))
# print(list(dic.values()))
# print(list(dic.items()))
深浅COPY:
# l1 = [1,2,3,4]
# l2 = l1
# l1.append(5)
# print(l1,l2)
#对于赋值运算,就是共同指向一个内存地址.
# s1 = ‘alex‘
# s2 = s1
# s1 = ‘alex2‘
# print(s1,id(s1))
# print(s2,id(s2))
#copy
# l1 = [1,2,3]
# l2 = l1.copy()
# l1.append(666)
# print(l1,id(l1))
# print(l2,id(l2))
# l1 = [1,[22,33],2,3]
# l2 = l1.copy()
# l1.append(666)
# print(l1,id(l1))
# print(l2,id(l2))
# l1 = [1,[22,33,],2,3]
# l2 = l1.copy()
# l1[1].append(666)
# print(l1,id(l1),id(l1[1]))
# print(l2,id(l2),id(l2[1]))
#对于浅copy来说,第一层都是独立的内存地址,从第二层开始,都是指向同一个内存地址,一变都变.
# import copy
#
# l1 = [1,[22,33,],2,3]
# l2 = copy.deepcopy(l1)
# l1.append(777)
# l1[1].append(666)
# print(l1,id(l1),id(l1[1]))
# print(l2,id(l2),id(l2[1]))
#对于深copy,无论多少层,在内存中都是两个独立的内存地址.
l1 = [1,[1,2],2,3]
l2 = l1[:] # 是浅copy
l1[1].append(111)
print(l1,l2)