判断大小写 islower(小写) isupper(大写) is开头起到判断作用 strip去除空格或元素
print('qweqwwrt'.islower()) # 运行结果True
print('QWEREWR'.isupper() ) # 运行结果True
改 upper(把所有字母转为大写)lower(把所有字母转为小写)
print('QWEREWR'.lower() )# 运行结果 qweqwwrt
print('qweqwwrt'.upper() )# 运行结果 QWEQWWRT
strip 去除
a=' 00hello00world0'
print(a.strip('0')) # 运行结果 00hello00world
print(a.strip()) # 运行结果00hello00world0
总结 只能去除左右空格或元素
replace传参(将‘老的字符串’,‘新的字符串’)
a='00hello00world00'
b=a.replace('0','1')
print(b)# 运行结果 11hello11world11
总结 转换参数(必须要有两个参数)中间用 , 转换
a='00hello00world00'
b=a.replace('0','')
print(b)# 运行结果helloworld
总结 当新的 ‘’ 中间为 空 时 replace会将老的字符删除
split(切割)
a='hello world'
print(a.split(' ') )#运行结果['hello', 'world']
print(a.split('o') )# 运行结果['hell', ' w', 'rld']
总结 没有参数默认空格来切 有参数切割参数 切割完成后形成新的参数
散列类型
目的:熟练掌握散列类型的方法,其中字典需要重点掌握,它占据着数据操作的半壁江山
集合(set)=后边的赋值用{}
三大特性(确定性(不可变类型)、互异性(去重、唯一性)、无序性(不能索引))确定性集合的元素不能是可变类型
目的:熟练掌握散列类型的方法,其中字典需要重点掌握,它占据着数据操作的半壁江山
set={1,2,3,4,5,6,7[1,2,3,4,]}
print(set) # 报错 大括号{}的的赋值要有确定性(列表为可变类型)
set={1,2,3,4,5,6,71,2,3,4,}
print(type (set),set) # 运行结果<class 'set'> {1, 2, 3, 4, 5, 6, 71}
a={'45班','你们是最棒的!'} #直接定义
b = set(['a','b','c'])
print(b,a)# 运行结果{'c', 'a', 'b'} {'45班', '你们是最棒的!'} 无序性
set={1,2,3,3,3,4,5,6,6}
print(set)# 运行结果 {1, 2, 3, 4, 5, 6} 互异性(去掉重复的部分)
三种运算方式 &(交集) |(并集) -(差集)
str1={"a","b","c"}
str2={'d','b','c'}
print(str1&str2) # 运行结果{'b', 'c'} 交集 & 只展示重合的 部分
print(str1|str2) # 运行结果{'b', 'a', 'c', 'd'} 并集 | 重合的部分只展示一次
print(str1-str2)# 运行结果{'a'} 去掉重合部分只保留 - 前的部分
字典
增删改
add(增,将元素添加到集合中。)
str1={"a","b","c"}
str1.add('f')
print(str1) #运行结果{'a', 'b', 'c', 'f'}
pop remove (删 )
list={4,5,1,2,3}
print(list.pop())
print(list) #运行结果 1 {2, 3, 4, 5}
list={4,5,1,2,3}
print(list.remove(5) )
print(list)# 运行结果{1, 2, 3, 4}
总结pop删除的元素是随机的 remove删除的元素是指定的
改 update 用本身和其他元素的并集更新集合 ((可迭代对象) 目前理解为序列类型,把每一个元素拆开添加)
a={1,2,3}
a.update([9,8])
print(a) # 运行结果 {1, 2, 3, 8, 9} 也是无序的
查:isdisjoint
a = {7,8,9}
b = {9,10,11}
c = b.isdisjoint(a)
print(c)# False #有交集返回False 没交集返回为True
issubset 判断是否包含于
a={'hello','world'}
b={'hello'}
print(b.issubset(a))# 运行结果 True a包含b b是a的子集
print(a.issubset(b))# 运行结果 False a包含b a不是b的子集
字典(dict)
表现形式 { ' ' : ' ' }
键 对 值
a={'key':'value'}
print(type (a)) #运行结果 <class 'dict'> 字典
增setdefault 有则查,无则增
a={'name':'hello','height':'168'}
a.setdefault('age','男')
print(a) #运行结果 {'name': 'hello', 'height': '168', 'age': '男'} 没有就增加
print(a.setdefault('name') )# 运行结果 hello 有则查 并给出对应的 value
a={'name':'hello','name':'168','name':'world'}
print(a)# 运行结果{'name': 'world'}
a={'name1':'hello','name2':'168','name3':'world'}
print(a) #运行结果 {'name1': 'hello', 'name2': '168', 'name3': 'world'}
总结 key具有唯一性为不可变类型 存在多个key时 只会打印一个 如需都打印时要给每个key标序
改 update 更新字典、将原字典和新字典整合,key将新的覆盖老的 有则改无则增
a={'name':'hello','height':'168'}
a.update({'age':'男'})
print(a)# 运行结果{'name': 'hello', 'height': '168', 'age': '男'} 无则增
a.update({'name':'168'})
print(a)# 运行结果{'name': '168', 'height': '168', 'age': '男'} 有则改
删 pop popitem
a={'name': '168', 'height': '168', 'age': '男'}
print(a.pop('name'))
print(a)# 运行结果 168 {'height': '168', 'age': '男'}
print(a.popitem())
print(type(a),a)# 运行结果('age', '男') <class 'dict'> {'name': '168', 'height': '168'}
总结 pop key找出key的value 并删除整个键值对 popitem以元组的形式找出最后一个键值对并删除 以字典的形式给结果
查 get keys values items
a={'name': 'hello', 'height': '168', 'age': '男'}
print(a.get('name')) #运行结果 hello
print(a.get('sex'))# 运行结果 None
总结 get找出指定key的value值 如果不存在key 会显示None 优势是不会报错 还会输出自定义的值
a={'name': 'hello', 'height': '168', 'age': '男'}
print(a.keys())# 运行结果dict_keys(['name', 'height', 'age'])
keys取出字典里所有key的值
a={'name': 'hello', 'height': '168', 'age': '男'}
print(a.values())# 运行结果dict_values(['hello', '168', '男'])
values 取出字典里所有的value值
a={'name': 'hello', 'height': '168', 'age': '男'}
print(a.items())# 运行结果dict_items([('name', 'hello'), ('height', '168'), ('age', '男')])
总结items 取出字典里所有的键对值
逻辑运算符 and or not
print(1<2 and 2<3) # 运行结果True
print(1<2 and 1>2)# 运行结果False
print(1>2 and 1>3)# 运行结果False
总结 and两边都为True 结果为True 有一边为False结果为False
print(1<2 or 1>2)# 运行结果True
print(1<2 or 1<3)# 运行结果True
print(1>2 or 1>3)# 运行结果False
总结 or的一边为True 结果为Ture
print(not True)# 运行结果False
print(not False)# 结果为Ture
总结not直接定义对错
运算符的优先级
运算符 描述
** 幂运算
+、- 一元运算符(正负号)
%、/、* 算术运算符
+、- 算术运算符
<、>、<=、>=、==、!= 比较运算符
=、/=、-=、+=、*=、%=、**=、//= 赋值运算符
is、is not 身份运算符
in、not in 成员运算符
not > and > or 逻辑运算符
a=3
a**=4
print(a)# 运行结果81
a=3
a/=3
print(a)#运行结果1.0
a=3
a//=3
print(a)#运行结果1
a=3
a%=3
print(a)# 运行结果 0