Python公共操作的4个运算符(+、*、in、not in)
16lz
2022-05-05
这里所说的公共操作指的是之前学过的序列基本上都支持的一些操作,主要分成三大块来讲解,第一块是运算符;第二块是公共方法;第三块是容器类型转换。
一、运算符
二、运算符加号 +(合并)
代码体验:
str1 = 'aaa'
str2 = 'bbb'
list1 = [1, 2]
list2 = [3, 4]
tuple1 = (10, 20)
tuple2 = (30, 40)
# 字符串合并
print(str1 + str2)
# 列表合并
print(list1 + list2)
# 元组合并
print(tuple1 + tuple2)
执行结果:
三、运算符乘号 *(复制)
代码体验:
str1 = 'aaa'
list1 = [1, 2]
tuple1 = ('hello',)
# 字符串复制
print(str1 * 3)
# 列表复制
print(list1 * 5)
# 元组复制
print(tuple1 * 5)
执行结果:
四、判断数据是否存在(in 、 not in)
代码体验:
str1 = 'abcd'
list1 = [1, 2, 3, 4]
tuple1 = (10, 20, 30, 40)
dict1 = {'name': 'Python自学网', 'age': 30}
# 字符串 - 字符b是否存在
print('a' in str1) # True
print('a' not in str1) # False
# 列表 - 数据2是否存在
print(2 in list1) # True
print(2 not in list1) # False
# 元组 - 数据20是否存在
print(20 in tuple1) # True
print(20 not in tuple1) # False
# 字典 - 数据age是否存在
print('age' in dict1) # True
print('age' not in dict1) # False
print('age' in dict1.keys()) # True
print('age' in dict1.values()) # False
执行结果:
更多相关文章
- 将服务器端字符读取至android的文本控件,换行符变成了黑方块的原
- .net平台借助第三方推送服务在推送Android消息(极光推送)
- Android常用的intent action汇总
- 字符串学习
- 实例演示字符串函数
- MySQL主从复制配置
- php中.=什么意思
- Android(安卓)KTX举例
- js-基础(六)字符串和数组的api