t = ['abc', 'def', 23]
print("'abc' in t : ", 'abc' in t)
print('23 in t : ', 23 in t)
print('123 in t : ', 123 in t)
print()
d = {'one' : 1, 'two': 2, 'three':3}
print("'one' in d : ", 'one' in d)
print('1 in d : ', 1 in d)
print()
for i in range(5): print(i, end = ' ')
is
¶判断左右操作符是否是同一个对象(是否指向同一块内存)
# 对于整数, python采用了类似java中常量池的方式
# 当两个变量的值处于-5到256之间时, 两个变量实际上指向了同一块内存
line = '-' * 10
print(line, 'int', line)
a = 256
b = 256
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = 257
b = 257
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = -5
b = -5
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = -6
b = -6
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
# 对于字符串, 由于字符串是不可变对象, 当两个字符串变量的值相等时, 他们实际上是指向了
# 内存中的同一个对象
print(line, 'str', line)
a = 'abc'
b = 'abc'
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = '123'
b = str(a)
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
# 对于list, 由于list是可变对象, 不管用中括号[]还是list()函数创建list, 都会创建一个新的list对象
print(line, 'list', line)
a = [1,2,3]
b = [1,2,3]
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = [4,5,6]
b = list(a)
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
# 对于dict, 由于dict是可变对象, 因此使用花括号或者dict()函数创建dict时, 都会创建一个新的dict对象
print(line, 'dict', line)
a = {'one' : 1, 'two' : 2, 'three' : 3}
b = {'one' : 1, 'two' : 2, 'three' : 3}
print('a = ', a)
print('b = ', b)
print('a is b : ', a is b)
a = {'a' : 1, 'b' : 2, 'c' : 3}
b = dict(a)
# print('a = ', a, ', b = ', b, ', a is b : ', a is b)
print('a = ', a)
print('b = ', b)
print('a is b : ', a is b)
# 对于tuple, 由于tuple是不可变对象, 当用小括号()(小括号不是必须的)创建tuple时, 会创建一个新的tuple对象
# 当用tuple()函数创建一个tuple时, 如果传入另一个tuple, 则实际上并没有创建一个新的tuple对象, 只是返回了一个
# 指向参数的变量
print(line, 'tuple', line)
a = (1,2,3)
b = (1,2,3)
print('a = ', a, ', b = ', b, ', a is b : ', a is b)
a = 4,5,6 # 创建一个tuple时, 小括号不是必须的
b = tuple(a)
print('a = ', a, ', b = ', b, ', a is b : ', a is b)