|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
>>> "123" + "456"
'123456'
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> "123" * 3
'123123123'
>>> s = [1, 2, 3]
>>> id(s)
20318792
>>> s *= 2
>>> s
[1, 2, 3, 1, 2, 3]
>>> id(s)
20318792
>>> t = (1, 2, 3)
>>> id(t)
20317160
>>> t *= 2
>>> t
(1, 2, 3, 1, 2, 3)
>>> id(t)
20284200
>>> # 虽然可变序列和不可变序列看上去都是 “可变” 的,但实现原理却是天壤之别:可变序列是在原位置修改 “扩容”,而不可变序列则是将内容 “扩容” 后再放到一个新的位置上去。
>>> # 是(is)和不是(is not)被称之为同一性运算符,用于检测两个对象之间的 id 值是否相等:
>>> x = "FishC"
>>> y = "FishC"
>>> x is y
True
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x is not y
True
>>> id(x)
20317064
>>> id(y)
20318760
>>> x is y
False
>>> a = "abc"
>>> b = "abc"
>>> id(a)
7509920
>>> id(b)
7509920
>>> a is b
True
>>> # in 运算符是用于判断某个元素是否包含在序列中的,而 not in 则恰恰相反:
>>> "FishC" in "FishC"
True
>>> "鱼" in "鱼C"
True
>>> "C" not in "FishC"
False
>>> # del 语句用于删除一个或多个指定的对象:
>>> x = "FishC"
>>> y = [1, 2, 3]
>>> del x, y
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined |
|