02基础的数据类型

数字(Number)
Python 支持三种不同的数值类型:整型(int)、浮点型(float)、复数(complex)
整型:通常称为整数,是整数或者负数,不带小数点。python3整型类型没有大小限制。int型数字:0、10 -780、-089、-0x270、0x90。
浮点型:浮点型由整数部分与小数部分组成。float型数字:0.0、15.2、-2.9、-32.5e10、32+e18。
浮点数可以用科学计算法表示:2.5e2 = 2.5 x 10^2 = 250
复数型:复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。complex:3.14j、9.23e-35j、4.53e-7j。
python数字类型相互转换
int(x) 将x转换为一个整数。

print(int(6.5))
#6

float(x) 将x转换到一个浮点数。

print(float(6))
#6.0

complex(x) 将x转换到一个复数,实数部分为 x,虚数部分为 0。complex(x, y) 将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。x 和 y 是数字表达式。

print(complex()) #空
print(complex(1,2))
print(complex('23'))
print(complex(4+5j))
# 0j
# (1+2j)
# (23+0j)
# (4+5j)

python数字运算
和其他不一样的是: / :返回的是浮点数、// :向下取整数、 ** : 幂运算
•由一个以上的值经过一系列的运算得到 新值 的过程叫做运算
•运算符分裂◾算数运算符
◾比较或者关系运算符
◾赋值运算符
◾逻辑运算符
◾位运算
◾成员运算符
◾身份运算符

# 表达式案例
a = 1+2
print (a)
b = ('i'+'love'+'una')
print (b)
# 算数运算符案例
# 加减乘除跟数学意义上基本一致
a = 8 + 2
print (a)
b = 10 - 2
print (b)
c = 2 * 5
print (c)

#正常意义上的除法
d = 9 / 2
print (d)
#地板除(取整数)
e = 9 // 2
print (e)
# 取余除
f = 9 % 2
print (f)
#指数
g = 4 ** 2
print(g)

比较运算符
•对两个内容进行比较的运算符
•结果一定是布尔值,既视 True(真)/ False(假)

# 在python里面  等于是:==
a = 3 == 4
print (a)
# 不等于 !=
b = 3 != 4
print (b)
# 其他的符号是
# >,<,<=,>=
print (3 >= 4)
print (3 <= 4)

False
True
False
True

赋值运算符
•把一个值放到变量里面去

# 赋值运算=符例子:
a = 9
print (a)
name = 'una'
print(name)

#复杂的赋值
a = b = 10
print (a)
print (b)
c,d = 1,2
print (c)
print (d)


#赋值的缩写
e = 10
f = e + 10
print (f)

#所有运算符都可以缩写
# -=,x=,/=,//=,%=,**=,都是缩写
# 在python里面没有 ++ , -- 运算符
f += 3   #就是 f + 3 的意思
print(f)

逻辑运算符
•对布尔值类型变量或者值 进行运算的符号
•and : 逻辑与
•or : 逻辑或
•not : 逻辑非
•python里面的逻辑运算没有异或
•运算规则:◾and看作乘法, or 看作加法
◾True看作1,False看作0
◾则逻辑运算就能转换成整数数学运算
◾最后结果如果是0则是False,否则为True

•逻辑运算短路问题◾逻辑运算式,按照运算顺序计算,一旦能够确定整个式子未来的值,则不再进行运算,直接返回。

# 逻辑运算举例:
a = True
b = True
c = False

aa = a and b  #左边表达式可以转换为1*1
print (aa)

bb = a and c
print (bb)

cc = 100 and c
print (cc)
# 在python里面 布尔值跟数字转换
#数字转换成布尔值的时候:0 = False , 其余是True
#布尔值转换成数字的时候: True=1 , False=0

字符串(String)
双引号或者单引号中的数据,就是字符串

str1 = "hello,world"
str2 = "Jim"

单字符在python中也是当做一个字符串来使用。
python访问字符串中的值

var1 = "hello world"
var2 = "python3"
print("var1[0]",var1[0]   #var1[0] h
print("var2[3:6]",var2[3:6])   #var2[3:6] hon

python字符串的分片
通过上边界不包括在内。

str = "string"
str[1:3] # "tr" 
#获取从偏移为1到偏移为3的字符串,不包括偏移为3的字符
str[1:] # "tring"
#获取从偏移为1到最后的一个字符,不包括最后一个字符
str[:3] #"str"
# 获取从偏移为0的字符一直到偏移为3的字符串,不包括偏移为3的字符串
str[:-1] #strin"
#获取从偏移为0的字符一直到最后一个字符(不包括最后一个字符串)     
str[:] #"string"
#获取字符串从开始到结尾的所有元素   
str[-3:-1] #"in"
#获取偏移为-3到偏移为-1的字符,不包括偏移为-1的字符 
str[::-1] # "gnirts"
#反转输出

“+”:实现字符串的拼接

string = "hello"+"world"  #"hello world"

列表(List)
当我们需要存储一组数据时候,就需要用序列,序列给每个元素都分配一个索引,第一个是0,第二个是1,依次论推。常用的序列有:列表和元组,当我们需要改变序列的元素时候,就用列表,因为某些原因,序列不能修改时候,使用元组更加合适。
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。列表的数据项不需要具有相同的类型。

基本数据类型列表list(增删改查)
•list 专门是用来存储大量的数据的数据类型
•在python中使用[]中括号来表示列表
•列表是可变的数据类型,内部元素没有任何要求
•列表: 能装东西的东西

lis = ["张三","李四","wangwu",{1,2,3},["uan","gpc"]]
print (lis)

[‘张三’, ‘李四’, ‘wangwu’, {1, 2, 3}, [‘uan’, ‘gpc’]]
添加元素
•append()追加
•insert()插入
•extend()迭代添加

# 1 添加元素
# append()追加

a = ["唐僧","沙悟净","猪八戒"]
print (a)
a.append("孙悟空")
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’]

# 2,插入 insert()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.insert(1,"孙悟空")
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘孙悟空’, ‘沙悟净’, ‘猪八戒’]

# 迭代添加 extend ()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.extend(["孙悟空","白龙马"])
print(a)
a.extend("葛鹏程")
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’, ‘葛’, ‘鹏’, ‘程’]

删除元素
•remove()删除一个元素
•pop(1) 索引指标删除0,1,2,3 当()括号为空时默认删除最后一个
•clear() 清空
•del删除

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.remove("唐僧")
print (a)
a.remove("如来")#字符不存在就报错
print (a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]


ValueError Traceback (most recent call last)
in
3 a.remove(“唐僧”)
4 print (a)
----> 5 a.remove(“如来”)#字符不存在就报错
6 print (a)

ValueError: list.remove(x): x not in list

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.pop()
print (a)
a.pop(1)
print (a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’]
[‘唐僧’, ‘猪八戒’, ‘孙悟空’]

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.clear()
print (a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[]

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
del a[0]
print (a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

修改
•列表通过索引修改

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a[0]="如来"
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘如来’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a[1:3]=["如来"]
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘如来’, ‘孙悟空’, ‘白龙马’]

a = ["唐僧","沙悟净",["马化腾","ma云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
a[2][1] = "马云"
print(a)

[‘唐僧’, ‘沙悟净’, [‘马化腾’, ‘马云’, ‘葛鹏程’], ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

a = ["唐僧","沙悟净",["马化腾","ma云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
a[2][1] = a[2][1].replace("ma","马")
print(a)

[‘唐僧’, ‘沙悟净’, [‘马化腾’, ‘马云’, ‘葛鹏程’], ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

查询
•列表通过索引和切片进行查询
•使用for循环进行查询

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
print(a[2::2])
print (a[1])
print(a[::-2])

[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘猪八戒’, ‘白龙马’]
沙悟净
[‘白龙马’, ‘猪八戒’, ‘唐僧’]

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
for b in a:
    print (b)

唐僧
沙悟净
猪八戒
孙悟空
白龙马

a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
for b in a:
    for c in b:
        print (c)














a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a[3][0])

a = ["唐僧","沙悟净",["马化腾","马云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
print (a[2][0])
print (a[2][0][1])

马化腾

# range 数数
for i in range (0,11):
    print (i)

0
1
2
3
4
5
6
7
8
9
10

for i in range (10):
    print ("我爱你",i)

我爱你 0
我爱你 1
我爱你 2
我爱你 3
我爱你 4
我爱你 5
我爱你 6
我爱你 7
我爱你 8
我爱你 9

a = ["唐僧","沙悟净","马化腾","马云","葛鹏程","猪八戒","孙悟空","白龙马"]
for b in range(len(a)):
    print (a[b])

唐僧
沙悟净
马化腾
马云
葛鹏程
猪八戒
孙悟空
白龙马

a = ["唐僧",",,","沙悟净","马化腾","马云","葛鹏程","猪八戒","孙悟空","白龙马"]
print(a)
a[1]=a[0]
print (a)

[‘唐僧’, ‘,’, ‘沙悟净’, ‘马化腾’, ‘马云’, ‘葛鹏程’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘唐僧’, ‘沙悟净’, ‘马化腾’, ‘马云’, ‘葛鹏程’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

# 3,replace ( ) 字符串替换
test = "我 、喜 、欢 打  游戏"
print(test)
test1=test.replace("游戏","看书")
print (test1)
test2 = test.replace(" ","")  #去掉所有空格
print (test2)
test2 = test.replace("、","\n") 
print (test2)

我 、喜 、欢 打 游戏
我 、喜 、欢 打 看书
我、喜、欢打游戏


欢 打 游戏

a = ["呵呵","哈哈","嘻嘻","嘿嘿"]
b = ["3","4","5","6"]
d = ["呵呵11","哈哈11","嘻嘻11","嘿嘿11"]
c =[]
c.extend(d)
for i in range (4):

    text =a [i]
    times=int(b[i])

    for time in range(times):
        gpc1 = (text+str(time+1))
        c.append(gpc1)
        

print(c)    

[‘呵呵11’, ‘哈哈11’, ‘嘻嘻11’, ‘嘿嘿11’, ‘呵呵1’, ‘呵呵2’, ‘呵呵3’, ‘哈哈1’, ‘哈哈2’, ‘哈哈3’, ‘哈哈4’, ‘嘻嘻1’, ‘嘻嘻2’, ‘嘻嘻3’, ‘嘻嘻4’, ‘嘻嘻5’, ‘嘿嘿1’, ‘嘿嘿2’, ‘嘿嘿3’, ‘嘿嘿4’, ‘嘿嘿5’, ‘嘿嘿6’]

# 迭代添加 extend ()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.extend(["孙悟空","白龙马"])
print(a)

[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]

元组
题目1
•敏感词判断 li = [“苍井空”,“波多野结衣”,“武藤兰”,“东京热”]
•将用户输入的敏感词替换成*
•敏感词的长度等于* 如 苍井空等于***
•输入内容 gpc特别喜欢苍井空,偶尔也喜欢波多野结衣,自从放弃了东京热,生活乐无边

content = input (">>>")
li = ["苍井空","波多野结衣","武藤兰","东京热"]
for ci in li:
    if ci in content: #判断ci是否出现content输入中
        content = content.replace(ci,"*"*len(ci))
print (content)

gpc特别喜欢苍井空,偶尔也喜欢波多野结衣,自从放弃了东京热,生活乐无边
gpc特别喜欢***,偶尔也喜欢*****,自从放弃了***,生活乐无边

元组
•元组的不可变指的是:内部元素的指向不可发生改变

t = (1,2,3,4,5)
t[1]= 66 #  'tuple' object does not support item assignment
print (t)

TypeError Traceback (most recent call last)
in
1 t = (1,2,3,4,5)
----> 2 t[1]= 66 # ‘tuple’ object does not support item assignment
3 print (t)

TypeError: ‘tuple’ object does not support item assignment

t = (1,2,[6,7,8,9],3,4,5)
t[2]= 66 #  'tuple' object does not support item assignment
print (t)

TypeError Traceback (most recent call last)
in
1 t = (1,2,[6,7,8,9],3,4,5)
----> 2 t[2]= 66 # ‘tuple’ object does not support item assignment
3 print (t)

TypeError: ‘tuple’ object does not support item assignment

# 元组的不可变指的是:内部元素的指向不可发生改变 但是列表内的元素是能改变的
t = (1,2,[6,7,8,9],3,4,5)
t[2][0]= 66 
print (t)

(1, 2, [66, 7, 8, 9], 3, 4, 5)

# len:获取元组的长度
t = (1,2,3,4,5,6,7)
len (t)
# max,min 最大最小值
print(max(t))
print (min (t))
```.

```python
# tuple 转化或创建元组
l = [1,2,3,4,5]
t = tuple (l)
print (t)
t = tuple()
print (t)
# count 计算制定数据中出现的次数
t = (1,2,3,4,4,4,4,4,33,3,2,2,)
print (t.count(4))
# 元组变量交换
a = 1
b = 2
a,b=b,a
print (a)
print (b)

集合-set
•一堆确定的无序的唯一的数据,集合中每个数据成为一个元素

# 集合定义
s = set()
print (type(s))
print (s)
# 如果只是用大括号定义,则定义的是一个dict类型
d ={}
print (type(d))
print (d)

集合的特征
•集合内数据无序,即无法使用索引和分片
•集合内部数据元素具有唯一性,可以用来排除重复数据
•集合内的数据,str,int,float,tuple,冰冻集合等,即内部只能放置哈希数据

# 集合序列操作
# 成员检测    in , not in
s ={4,5,"i","love","una"}
print (s)
if "love" in s:
    print ("在的")
else:
    print ("no")

{4, 5, ‘una’, ‘love’, ‘i’}
在的

# 集合遍历操作
# for循环
s ={4,5,"i","love","una"}
for i in s:
    print (i,end=" ")

4 5 una love i

# 带有元组的集合遍历
s = {(1,2,3),("i","love","una"),(4,5,6)}
for a,b,c in s:
    print (a,"--",b,"__",c)
print ("__"*40)
for k in s:
    print (k)

4 – 5 __ 6
i – love __ una
1 – 2 __ 3


(4, 5, 6)
(‘i’, ‘love’, ‘una’)
(1, 2, 3)

#普通集合内涵
# 一下集合在初始化后会自动过滤掉重复元素
s = {1,2,3,44,5,6,6,6,6,6,6,3,32,2,2}
print (s)

print ("__"*40)

ss = {i for i in s}
print (ss)

{32, 1, 2, 3, 5, 6, 44}


{32, 1, 2, 3, 5, 6, 44}

# 带有条件的集合内涵
sss = {i for i in s if i%2==0}
print (sss)

{32, 2, 44, 6}

# 多循环集合内涵
s1 = {1,2,3,4}
s2 = {"i","love","una"}
s = {m*n for m in s2 for n in s1}
print (s)

print ("__"*40)

ss  = {m*n for m in s2 for n in s1 if n ==2}
print (ss)

{‘iiii’, ‘ii’, ‘una’, ‘lovelove’, ‘unaunauna’, ‘lovelovelove’, ‘lovelovelovelove’, ‘unauna’, ‘love’, ‘unaunaunauna’, ‘i’, ‘iii’}


{‘unauna’, ‘lovelove’, ‘ii’}

# 集合函数/关于集合函数
# len ,max ,min 更其他函数基本上一致

s = {111,2223,222,222,333,4444,4444}
print (len(s))
print (max(s))
print (min(s))

5
4444
111

# set 生成一个集合
l = [1,2,3,4,5,5,5,5,4,3,2,1]
s = set (l) #生成集合初始化排重
print (s)

{1, 2, 3, 4, 5}

#add 向集合内添加一个元素
s = {1}
s.add(2)
print (s)

{1, 2}

# clear 清空
s = {1,2,3,4,5}
print (id(s))
print (s)
print ("__"*40)
s.clear()
print (id(s))
print (s)

2336048934696
{1, 2, 3, 4, 5}


2336048934696
set()

# copy 拷贝
# remove 移除指定值,直接改变原有值面如果删除的值不存在,则报错
# discard 移除集合中指定的值
s = {1,2,3,4,45,5,6,7,7,7,7,60}
s.remove(4)
print (s)

print ("__"*40)

s.discard(5)
print (s)

print ("__"*40)

s.remove(1000)#删除的值不存在,则报错
print (s)
s.discard(5000)
print (s)

{1, 2, 3, 5, 6, 7, 45, 60}


{1, 2, 3, 6, 7, 45, 60}



KeyError Traceback (most recent call last)
in
13 print ("__"*40)
14
—> 15 s.remove(1000)#删除的值不存在,则报错
16 print (s)
17 s.discard(5000)

KeyError: 1000

# pop 随机移除一个元素
a = {1,2,3,4,5,6}
d = a.pop()# d = a.pop()的意思是 d就是 随机移除那个值
print (d)
print (a)

1
{2, 3, 4, 5, 6}

intersection 交集

difference 差集

union 并集

issubset 检察一个集合是否为另一个的子集

issuperset 检查是否为一个超集

s1 = {1,2,3,4,5,6}
s2 = {4,5,6,7,8,9}
s_1 = s1.intersection(s2)
print (s_1)

print ("__"*40) 

s_2 = s1.difference(s2)
print (s_2)

print ("__"*40)

s_3 = s1.union (s2)
print (s_3)

print ("__"*40)

s_4 = s1.issubset(s2)
print (s_4)

print ("__"*40)

s_5 = s1.issuperset (s2)
print (s_5)

{4, 5, 6}


{1, 2, 3}


{1, 2, 3, 4, 5, 6, 7, 8, 9}


False


False

# 集合的数学操作
s1 = {1,2,3,4,5,6}
s2 = {4,5,6,7,8,9}
s_1 = s1 - s2
print (s_1)
#unsupported operand type(s) for +: 'set' and 'set'
s_2 = s1 + s2
print (s_2)

{1, 2, 3}


TypeError Traceback (most recent call last)
in
5 print (s_1)
6
----> 7 s_2 = s1 + s2
8 print (s_2)

TypeError: unsupported operand type(s) for +: ‘set’ and ‘set’

frozenset 冰冻集合
•冰冻集合就是不可以进行任何修改的集合

s = frozenset ()
print (type(s))
print (s)

<class ‘frozenset’>
frozenset()

字典(重点
•字典中存储的数据要求必须是 key:value
•在字典中,你需要拿到key就一定能拿到value
•查找效率高
•字典语法:◾{key:value,key2:value2,key3:value3…}
◾key:必须是可哈希的数据类型(不可变的数据类型)◦不可变的有 : 元组,字符串,数字,bool值
◾value :什么都可以装

d = {"jj":"林俊杰","jay":"周杰伦","uan":"杨雨","gpc":"葛鹏程"}
print (d)

{‘jj’: ‘林俊杰’, ‘jay’: ‘周杰伦’, ‘uan’: ‘杨雨’, ‘gpc’: ‘葛鹏程’}

d = {"jj":"林俊杰","jay":"周杰伦","uan":"杨雨","gpc":"葛鹏程"}
print (d["jj"])
print (d["jjj"]) # 如果给的key不存在就会报错KeyError: 'jjj'
# 字典没有索引切片

林俊杰


KeyError Traceback (most recent call last)
in
1 d = {“jj”:“林俊杰”,“jay”:“周杰伦”,“uan”:“杨雨”,“gpc”:“葛鹏程”}
2 print (d[“jj”])
----> 3 print (d[“jjj”]) # 如果给的key不存在就会报错KeyError: ‘jjj’
4 # 字典没有索引切片

KeyError: ‘jjj’

# 1,增加
lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print(lol)
lol ["艾希"]="寒冰射手"
print(lol)
lol.setdefault("流浪法师","瑞兹")
print(lol)
lol.setdefault("亚索","阿里卡通")#如果key存在了就不新增了
print(lol)

{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’, ‘亚索’: ‘疾风剑豪’}
{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’, ‘亚索’: ‘疾风剑豪’, ‘艾希’: ‘寒冰射手’}
{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’, ‘亚索’: ‘疾风剑豪’, ‘艾希’: ‘寒冰射手’, ‘流浪法师’: ‘瑞兹’}
{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’, ‘亚索’: ‘疾风剑豪’, ‘艾希’: ‘寒冰射手’, ‘流浪法师’: ‘瑞兹’}

# 2, 修改 直接使用原有key 就能修改原有的元素
lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
lol ["亚索"]="阿里卡通"
print (lol)

{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’, ‘亚索’: ‘阿里卡通’}

# 3,删除 pop(key)根据key来删除一个键值对
lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
lol.pop("亚索")
print (lol)

{‘盖伦’: ‘德玛西亚之力’, ‘皇子’: ‘嘉文四世’}

# 4 ,查询
lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.get("亚索"))
print (lol.get("亚瑟"))#如果key不存在,返回None
print(lol.get ("亚瑟","没有检测到当前key"))#当key不存在得时候返回默认值
print (lol["亚瑟"])#如果key不存在就报错

疾风剑豪
None
没有检测到当前key


KeyError Traceback (most recent call last)
in
4 print (lol.get(“亚瑟”))#如果key不存在,返回None
5 print(lol.get (“亚瑟”,“没有检测到当前key”))#当key不存在得时候返回默认值
----> 6 print (lol[“亚瑟”])#如果key不存在就报错

KeyError: ‘亚瑟’

# 5 , 字典的常用操作
# keys()  key的集合
# values() value的集合
# items() key:value的集合
lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.keys())
for i in lol.keys():
    print (i)

dict_keys([‘盖伦’, ‘皇子’, ‘亚索’])
盖伦
皇子
亚索

lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.values())
for i in lol.values():
    print (i)

dict_values([‘德玛西亚之力’, ‘嘉文四世’, ‘疾风剑豪’])
德玛西亚之力
嘉文四世
疾风剑豪

lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.items())
for i in lol.items():
    print (i)

dict_items([(‘盖伦’, ‘德玛西亚之力’), (‘皇子’, ‘嘉文四世’), (‘亚索’, ‘疾风剑豪’)])
(‘盖伦’, ‘德玛西亚之力’)
(‘皇子’, ‘嘉文四世’)
(‘亚索’, ‘疾风剑豪’)

lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.items())
for i in lol.items():
    print (i[0])
    print (i[1])

dict_items([(‘盖伦’, ‘德玛西亚之力’), (‘皇子’, ‘嘉文四世’), (‘亚索’, ‘疾风剑豪’)])
盖伦
德玛西亚之力
皇子
嘉文四世
亚索
疾风剑豪

lol = {"盖伦":"德玛西亚之力","皇子":"嘉文四世","亚索":"疾风剑豪"}
print (lol.items())
for k,v in lol.items():#解包
    print (k,v)

dict_items([(‘盖伦’, ‘德玛西亚之力’), (‘皇子’, ‘嘉文四世’), (‘亚索’, ‘疾风剑豪’)])
盖伦 德玛西亚之力
皇子 嘉文四世
亚索 疾风剑豪

# 字典嵌套
wangfeng = {
    "name":"汪峰",
    "age":"58",
    "wife":{
        "name":"章子怡",
        "nick_name":"国际章",
        "hobby":["唱歌","跳舞","演戏"]
    },
    "children":[
        {"name":"汪大娃","age":18},
        {"name":"汪二娃","age":15}
    ]
}

# 汪大娃年龄多少?
print (wangfeng["children"][0]["age"])
wangfeng["wife"]["hobby"].append("读书")
print (wangfeng)
# 给二娃增加 2 岁
wangfeng["children"][1]["age"] = wangfeng["children"][1]["age"] + 2
print (wangfeng)

18
{‘name’: ‘汪峰’, ‘age’: ‘58’, ‘wife’: {‘name’: ‘章子怡’, ‘nick_name’: ‘国际章’, ‘hobby’: [‘唱歌’, ‘跳舞’, ‘演戏’, ‘读书’]}, ‘children’: [{‘name’: ‘汪大娃’, ‘age’: 18}, {‘name’: ‘汪二娃’, ‘age’: 15}]}
{‘name’: ‘汪峰’, ‘age’: ‘58’, ‘wife’: {‘name’: ‘章子怡’, ‘nick_name’: ‘国际章’, ‘hobby’: [‘唱歌’, ‘跳舞’, ‘演戏’, ‘读书’]}, ‘children’: [{‘name’: ‘汪大娃’, ‘age’: 18}, {‘name’: ‘汪二娃’, ‘age’: 17}]}

相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页