객체의 주소값이 같으면 True를 반환한다.
==
객체의 값이 같으면 True를 반환한다.
a = 10 b = 10 print("a is b =", a is b) print("a == b =", a == b) print("id(a) =", id(a)) print("id(b) =", id(b))
a is b = True a == b = True id(a) = 4545772160 id(b) = 4545772160
a = 999 b = 999 print("a is b =", a is b) print("a == b =", a == b) print("id(a) =", id(a)) print("id(b) =", id(b))
a is b = False a == b = True id(a) = 140654371171984 id(b) = 140654371172112
10과 999의 문법적 차이는 존재하지 않음에도 주소값이 다른 이유는 파이썬의 Default 값이 Object Interning을 사용하기 때문이다.
참고자료1 참고자료2
x and y
: if x is false, then x, else y>>> print(1 and 9) # 1 is True -> 9 is True -> print 9 9 >>> print(2 and 9) # 2 is True -> 9 is True -> print 9 9 >>> print(1 and 0) # 1 is True -> 0 is False -> print 0 0 >>> print(0 and 1) # 0 is False -> print 0 0 >>> print(0 and 0) # 0 is False -> print 0 0 >>> print(bin(0b1010 and 0b1111)) # 0b1010 is True -> 0b1111 is True -> print 0b1111 0b1111
>>> print(1 & 9) # True & True -> True is 1 1 >>> print(2 & 9) # True & True -> True is 1 1 >>> print(1 & 0) # True & False -> Fales is 0 0 >>> print(0 & 1) # False & True -> Fales is 0 0 >>> print(0 & 0) # False & False -> Fales is 0 0 >>> print(bin(0b1010 & 0b1111)) # Bitwise calculation 0b1010
x or y
: if x is false, then y, else x>>> print(1 or 9) # 1 is True -> 9 is True -> print 1 1 >>> print(2 or 9) # 2 is True -> 9 is True -> print 2 2 >>> print(1 or 0) # 1 is True -> 0 is False -> print 2 1 >>> print(0 or 1) # 0 is False -> 1 is True -> print 1 1 >>> print(0 or 0) # 0 is False -> 0 is False -> print 0 0 >>> print(bin(0b1010 or 0b1111)) # 0b1010 is True -> 0b1111 is True -> print 1010 0b1010
>>> print(1 | 9) # 0b0001 | 0b1001 = 0b1001 9 >>> print(2 | 9) # 0b0010 | 0b1001 = 0b1011 11 >>> print(1 | 0) # 0b0001 | 0b0000 = 0b0001 1 >>> print(0 | 1) # 0b0000 | 0b0001 = 0b0001 1 >>> print(0 | 0) # 0b0000 | 0b0000 = 0b0000 0 >>> print(bin(0b1010 | 0b1111)) # 0b1010 | 0b1111 = 0b1111 0b1111
>>> print(not(0)) # 0 is False True >>> print(not(9)) # 9 is True False
>>> print(~0) # 0b0000의 보수 = 0b1111 = -1 -1 >>> print(~7) # 0b0111의 보수 = 0b1000 = -8 -8
Decimal value 4bits binary 7 0 111 6 0 110 5 0 101 4 0 100 3 0 011 2 0 010 1 0 001 0 0 000 -1 1 111 -2 1 110 -3 1 101 -4 1 100 -5 1 011 -6 1 010 -7 1 001 -8 1 000