1. is / ==
is
객체의 주소값이 같으면 True를 반환한다.
- Object Identity Operator
- Reference Comparison (참조 비교)
==
객체의 값이 같으면 True를 반환한다.
- Equal Comparison Operator(비교 연산자)
- Value Comparison (값 비교)
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을 사용하기 때문이다.
2. and / &
and
- Logical Operator(논리 연산자)
- True/False 연산
- x and y: if x is false, then x, else y
- First argument(x) is True -> evaluates second argument(y)
- First argument(x) is False -> False
>>> 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
&
- Comparison Operator(비교 연산자)
- Bitwise 연산
>>> 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
3. or / |
or
- Logical Operator(논리 연산자)
- True/False 연산
- x or y: if x is false, then y, else x
- First argument(x) is True
- First argument(x) is False -> evaluates second argument(y)
>>> 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
|
- Comparison Operator(비교 연산자)
- Bitwise 연산
>>> 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
not / ~
not
- Logical Operator(논리 연산자)
- True/False 연산
- if x is false, then True, else False
- Argument(x) is True -> False- Argument(x) is False -> True
>>> print(not(0)) # 0 is False
True
>>> print(not(9)) # 9 is True
False
~
- Comparison Operator(비교 연산자)
- Bitwise 연산
>>> print(~0) # 0b0000의 보수 = 0b1111 = -1
-1
>>> print(~7) # 0b0111의 보수 = 0b1000 = -8
-8
'프로그래밍 언어 > Python' 카테고리의 다른 글
[Python] enum형 (0) | 2024.07.16 |
---|---|
[Python] Encrypt / Decrypt (0) | 2024.07.10 |
[Python] 문자열 관련 함수 총 정리 (0) | 2024.06.14 |
[Python] f-string (문자열) (0) | 2024.03.21 |
[Python] pass, continue, break 차이점 (0) | 2024.03.05 |