[Python] 비슷한 연산자의 차이(is, ==, and, &, or, |)

kkiyou·2021년 5월 24일
4

Python

목록 보기
3/12

1. 'is' and '=='

1.1. 'is'

객체의 주소값이 같으면 True를 반환한다.

  • Object Identity Operator
  • Reference Comparison (참조 비교)

1.2. ==

객체의 값이 같으면 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을 사용하기 때문이다.
참고자료1 참고자료2



2. 'and' and '&'

2.1. '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

2.2. '&'

  • 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' and '|'

3.1. '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

3.2. '|'

  • 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


4. 'not' and '~'

4.1. '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

4.2. '~'

  • Comparison Operator(비교 연산자)
  • Bitwise 연산
>>> print(~0) # 0b0000의 보수 = 0b1111 = -1
-1
>>> print(~7) # 0b0111의 보수 = 0b1000 = -8
-8

Decimal value4bits binary
70 111
60 110
50 101
40 100
30 011
20 010
10 001
00 000
-11 111
-21 110
-31 101
-41 100
-51 011
-61 010
-71 001
-81 000


참고자료1

0개의 댓글