python基础05
python基础05
昨日内容回顾及作业讲解
列表:增 append insert extend
删 remove pop clear del
改 li[索引] = '被修改的内容' li [切片]:'被修改的内容'
查 for循环
range
元祖:
count()
len()
li.index('元素'')
li.sort()
print(li.sort())
reverse()反转
ps:
join : list------->str
S.join(iterable)
li = [1,2,3]
s1 = str(li)
split str------->list
lis = [2,3,'k',['qwe'],20,['k',['tt',3,'1']],89,'ab','adv']
#1)将列表lis中的'tt'变成大写(用两种方式)
#lis[3][2][1][0] = 'TT'
#print(lis)
lis[3][2][1][0] = lis[3][2][1][0].upper()
print(lis2)
#dict
#数据类型划分:可变数据类型,不可变数据类型
#不可变数据类型:元祖,bool,int,str,set 可哈希
#可变数据类型:list,dict 不可哈希
#dict key 必须是不可变数据类型,可哈希
value:任意数据类型
#dict优点:二分查找去查询
储存大量的关系型数据
特点:无序的
#print(dic) dic1 = {'age':18,'name':'jin','sex':'male',} #增: dict['high'] = 185 print(dic1)
#print(dic)
dic1 = {'age' : 18 , 'name' : 'jin', 'sex' : 'male',}
#增dict['high'] = 185 #没有键值对,添加
#dict['age'] = 16 #如果有键,则值覆盖
print(dict)
#dic1.setdefault('weight')#有键值对,不做任何改变,没有才添加
#dic1.setdafault('weight',150)
dic1.setdefault('name','二哥')
print(dic1)
#删
#print(dic1.pop('age'))#有返回值,按键去删除
print(dic1.pop('二哥'))#有返回值,按键去删除
print(dic1)
#删
#print(dic1.pop('age'))#有返回值,按键去删除
#print(dic1.pop('二哥',None))#可设置返回值
#print(dic1)
#dic1.popitem()#随机删除
#print(dic1)
#del dic1['name1']
#print(dic1)
dic1.clear()#清空字典
#del dic1['name']
#print(dic1)
#del dic1
#dic1.clear()#清空字典
#改 update
#dic1['age'] = 16
#dic = {'name':'jin','age':18,'sex':'male'}
#dic2 = {'name':'alex','weight':75}
#dic2.update(dic)#
#
#print(dic)
#print(dic2)
dic1 = {'age':18,'name':jin,'sex':'male'}
#查
print(dic1.keys().type(dic1.key()))
print(dic1.values())
print(dic1.items())
dic1 = {'age':18,'name':'jin','sex':'male',}
#查
#print(dic1.keys().type(dic1.keys()))
#print(dic1.values())
#print(dic1.items())
for i in dic1:
print(i)
for i in dic1:
print(i)
for i in dic1.keys():
print(i)
for i in dic1.values()
print(i)
for i in dic1.values():
print(i)
a,b = 1,2
print(a,b)
#a,b = 1,2
#print(a,b)
a = 1
b = 2
a,b = b ,a
print(a,b)
a,b=[1,2]
print(a,b)
a,b = [1,2,3]
print(a,b)
a,b=[1,2],[2,3]
print(a,b)
for i in dic1.item():
print(i)
for k,v in dic1.item():
print(k,v)
v1 = dic1['name']
print(v1)
v2 = dic1['name']
print(v2)
# v2 = dic1['name1'] #报错
#print(v2)
print(dic1.get('name1','没有这个键'))
dic = {
'name':['alex','wusir','taibai'],
'py9':{
'time':'1213'
'learn_money':19800,
'addr':'CBD',
},
'age':21
}
dic['age'] = 56
print(dic['name'])
dic['name'].append('ritian')
print(dic)
dic['name'][1] = dic['name'][1].upper()
print(dic)
i = {1,2,'wusir'}
l[2] = l[2].upper()
dic['name'][1] = dic['name'][1].upper()
print(dic)
#female :6
dic['py9']['female'] = 6
print(dic)
info = input('>>>')#fhdklah123rfdj12fdjs13
for i in info:
if i.isalpha():
info = info.replace(i," ")
l = info.split()
print(len(1))
info = input('>>>') #fhdklah
for i in info():
if i.isalpha():
info = info.replace(i,"")
l = info.split()
print(len(l))
info = input('>>>')#fhdklah123fdj12fdjs13
for i in info:
if i.isalpha():
info = info.replace(i," ")
l = info.split()
print(len(l))