from
copy
import
*
#String 浅复制
aList
=
[
3
,
5
,
7
]
bList
=
aList
[
:
:
]
flag
=
aList
==
bList
print
(
flag
)
flag
=
aList
is
bList
print
(
flag
)
#Answer
#True
#False
#切片
aList
=
[
3
,
5
,
7
,
9
]
aList
[
:
3
]
=
[
]
#删除列表中前3个元素
print
(
aList
)
#Answer:
#[9]
aList
=
[
3
,
5
,
7
,
9
,
11
]
del
aList
[
:
3
]
#同上
print
(
aList
)
#Answer:
#[9, 11]
#String
#is 用于判断 id是否相同;== 用于判断 值是否相同
astr
=
"good"
bstr
=
"good"
cstr
=
astr
+
""
flag_ab
=
astr
is
bstr
print
(
flag_ab
)
flag_ab
=
astr
==
bstr
print
(
flag_ab
)
flag_ac
=
astr
is
cstr
print
(
flag_ac
)
flag_ac
=
astr
==
bstr
print
(
flag_ac
)
#Answer:
#True
#True
#True
#True
#基本数据类型
a
=
128
b
=
128
c
=
a
+
0
d
=
b
+
0
e
=
a
<<
1
f
=
e
>>
1
print
(
id
(
a
)
)
print
(
id
(
b
)
)
print
(
id
(
c
)
)
print
(
id
(
d
)
)
print
(
id
(
e
)
)
print
(
id
(
f
)
)
#Answer:
#1514041536
#1514041536
#1514041536
#1514041536
#1514045632
#1514041536
a
=
[
'a'
,
'b'
,
'c'
,
'd'
]
b
=
copy
(
a
)
#等价于 b = a[:]
a
[
0
]
=
'A'
print
(
'a:'
,
a
)
print
(
'b:'
,
b
)
#Answer:浅拷贝! 链表是可变对象,如果不特殊强调,使用的是深拷贝。
#a: ['A', 'b', 'c', 'd']
#b: ['a', 'b', 'c', 'd']
#Answer(如果不使用copy,直接用b=a):深拷贝!
#a: ['A', 'b', 'c', 'd']
#b: ['A', 'b', 'c', 'd']
#不可变对象用于保障数据的稳定!基本数据类型&String&元组,这些数据时可以用来哈希的
#可变对象用于节省内存!链表、字典、集合之类!