关于python:Python公共操作的4个运算符innot-in

0次阅读

共计 880 个字符,预计需要花费 3 分钟才能阅读完成。

这里所说的公共操作指的是之前学过的序列基本上都反对的一些操作,次要分成三大块来解说,第一块是运算符;第二块是公共办法;第三块是容器类型转换。

一、运算符

二、运算符加号 +(合并)

代码体验:

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

执行后果:

以上就是 Python 公共操作中的运算符解说,本人能够多写一些代码尝试一下,不过看下面的代码演示应该也是能够齐全了解的。更多全面的 Python 解说能够移步去 Python 自学网。

文章借鉴起源:www.wakey.com.cn/

正文完
 0