这里所说的公共操作指的是之前学过的序列基本上都反对的一些操作,次要分成三大块来解说,第一块是运算符;第二块是公共办法;第三块是容器类型转换。
一、运算符
二、运算符加号 +(合并)
代码体验:
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) # Trueprint('a' not in str1) # False# 列表 - 数据2是否存在print(2 in list1) # Trueprint(2 not in list1) # False# 元组 - 数据20是否存在print(20 in tuple1) # Trueprint(20 not in tuple1) # False# 字典 - 数据age是否存在print('age' in dict1) # Trueprint('age' not in dict1) # Falseprint('age' in dict1.keys()) # Trueprint('age' in dict1.values()) # False
执行后果:
以上就是Python公共操作中的运算符解说,本人能够多写一些代码尝试一下,不过看下面的代码演示应该也是能够齐全了解的。更多全面的Python解说能够移步去Python自学网。
文章借鉴起源:www.wakey.com.cn/