728x90
short-circuit Evaluation
and
나or
로 boolean 값을 다룰 때 연산을 최소화하기 위한 방법and
and
는 두 값이 모두 참일때만True
를 반환하고 나머지 경우에는False
를 반환- (A
and
B)에서 A가False
A가 거짓이면 B 연산 안함→ - A가
True
면 B의 참, 거짓이 (Aand
B)의 참거짓을 결정 B를 반환→
- (A
or
or
은 두 값 모두 거짓일때만False
를 반환하고 나머지 경우에는True
를 반환- (A
or
B)는 둘 중 하나만 참이면True
A가 참이면 B 연산 안함→ - 마찬가지로 A가
False
이면 B의 참, 거짓이 (Aor
B)의 참거짓을 결정 B를 반환→
- (A
Bitwise Operators
- boolean 연산의 두 operand가 function의 결과로 얻어지는 등의 경우와 같이 반드시 연산이 필요한 경우에는 bitwise operator를 사용
- bitwise operator는 양쪽의 operand의 연산을 먼저 수행하고 비교함
- | : bitwise
or
- & : bitwise
and
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters## and vs & print(1==1and15) print(1==1 & 15) >>> 15 >>> True ## or vs | print(1!=1or15) print(1==1|15) >>> 15 >>> False
- | : bitwise
Example
and vs &
- line 2에서
and
의 앞의 operand가True
이기 때문에15
부분은 연산하지 않고 값을 반환 - 반면 line 3에서
&
는 두 operand의 boolean을 연산한 후에 비교해True
를 반환- numeric에 대해서는 0을 제외한 모든 수는 True
or vs |
- line 10에서
or
의 앞의 operand가False
이기 때문에15
부분은 연산하지 않고 값을 반환
'프로그래밍 언어 > python' 카테고리의 다른 글
[python] 객체 지향 프로그래밍 1 - Object oriented python 0 | 2022.08.08 |
---|---|
numpy array broadcasting 0 | 2022.07.26 |
jupyter module autoreload 0 | 2022.07.26 |
*args와 **kwargs 활용하기 0 | 2022.07.26 |
matplotlib 테마 설정하기 0 | 2022.07.26 |