
一、总结
不可变的数据类型 | 可变的数据类型 |
---|---|
int、float、bool、str、元组 | 列表、字典 |
二、公共方法
运算符 | Python表达式 | 结果 | 描述 | 支持的数据类型 |
---|---|---|---|---|
+ |
[1, 2]+[3, 4] |
[1, 2, 3, 4] |
合并 | 字符串、列表、元组 |
* |
['Hi']*4 |
['Hi', 'Hi', 'Hi', 'Hi'] |
复制 | 字符串、列表、元组 |
in |
3 in (1, 2, 3) |
True | 元素是否存在 | 字符串、列表、元组、字典 |
not in |
4 not in (1, 2, 3) |
True | 元素是否不存在 | 字符串、列表、元组、字典 |
2. 1 +
注意顺序、类型的统一
eg:
- 列表
my_list1 = [1, 2]
my_list2 = [3, 4]
my_list = my_list1+my_list2
print(my_list)
结果:[1, 2, 3, 4]
- 字符串
name = "小明"
res = "我叫"+name
print(res)
结果:我叫小明
- 元组
res = (1, 4) + (6, 9)
print(res)
结果:(1, 4, 6, 9)
2.2 *
- 字符串
my_str = '='
res = my_str*40
print(res)
结果:========================================
- 列表
my_list = ['hello']*5
print(my_list)
结果:['hello', 'hello', 'hello', 'hello', 'hello']
print(my_list)
结果:['hello', 'world', 'hello', 'world']
- 元组
my_tuple = (11,)*5
print(my_tuple)
结果:(11, 11, 11, 11, 11)
更新中......
网友评论