[Python] == 비교, is 비교
파이썬의 값비교, 참조 비교에 대해 알아보자
값 비교 : ==
참조 비교 : is
x = 15 y = 25 print(f'x == y : { x == y}') # 값을 비교 print(f'x is y : { x is y}') # 객체(ID)를 비교
x == y : False
x is y : False
x = ['orange', 'banana', 'apple'] y = x print(f'x == y : {x == y}') print(f'x is y : {x is y}') print(f'x value, id : {x}, {hex(id(x))}') print(f'y value, id : {y}, {hex(id(y))}')
x == y : True
x is y : True
x value, id : [‘orange’, ‘banana’, ‘apple’], 0x188444eb440
y value, id : [‘orange’, ‘banana’, ‘apple’], 0x188444eb440
x = ['a', 'b', 'c'] y = ['a', 'b', 'c'] print(x == y) print(x is y) print(hex(id(x)), hex(id(y)))
True
False
0x188444eb480 0x188444e9880
c = [] d = c e = c + d print( d == e) print(f'{ c is d}') print(f'{ d is e}') print(id(c), id(d), id(e))
True
True
False
1684773189312 1684773189312 1684772959552
최신 댓글