본문 바로가기
목차
Python

[Python] Boolean Operators, Relational Operators, Membership Operators and Identity Operator

by ds31x 2023. 7. 28.
728x90
반응형

Python Operators

 

https://dsaint31.tistory.com/516

 

[Python] Arithmetic in Python and Augmented Assignment

1. Precedence of Arithmetic OperationsHigher     ** > -(negation) > * = / = // = % > + = -(subtraction)     Lower우선순위를 기억하는 것도 중요하지만, 헷갈리면 그냥 parentheses로 묶어주면 된다. (가독성을 위해서도

dsaint31.tistory.com


Boolean Operators

기본 boolean operator는 다음 3가지로 구성됨.

  • and : and 연산자. (binary op.) : C언어 등에선 &&에 해당.
  • or : or 연산자. (binary op.) : || 에 해당.
  • not : not 연산자. (unary operator로 operand가 하나임.) : !에 해당.

참고로 boolean operator로 묶여 있는 combining comparisons의 경우, short-circuit evaluation이 수행됨.

  • or의 경우 왼쪽에서 오른쪽으로 evaluation이 이루어지는 도중
    하나라도 True가 나오면 뒤에 대해 evaluation을 하지않고 True를 반환.
  • and의 경우는
    하나라도 Flase가 나오면 이후 evaluation을 수행하지 않고 False를 반환함.

 

Boolean 이 아니더라도 T/F 로 판정되는 Truthiness에 대해 기억할 것!

https://ds31x.blogspot.com/2023/07/python-truthiness.html

 

Python : Truthiness

 

ds31x.blogspot.com


Relational Operators (관계연산자, 비교연산자)

  • > : greater than
  • < : less than
  • >= : greater than or equal to
  • <= : less than or equal to
  • == : equal to
  • != : not equal to

반환하는 값은 Boolean type이지만, operand는 numeric type 또는 string일 수 있음.
일반적으로 lexicographic order (=dictionary ordering, 알파벳순)에 따라 앞에 있는 철자가 작게 처리됨
대문자가 소문자보다 작은 값을 가지며, 다른 글자들이 다 같을 경우, 긴문자가 큰 값을 가짐.
다음 참조.

>>>   'A'   <   'a'   
True   
>>>   'A'   >   'z'   
False   
>>>   'abc'   <   'abd'   
True   
>>>   'abc'   <   'abcd'   
True

Membership Operator

Membership 이라는 이름에서 알 수 있듯이 포함 여부를 확인하는 연산자임.

 

in 의 뒤에 주어진 collection ( list, dict, str 등)에, 앞에 주어진 object가 포함되어 있는지를 체크함.

 

membership operator인 in
boolean type을 반환값으로 사용한다.

>>>   'a'   in   'abc'   
True

Identity Operators

isis not 으로 같은 객체를 참조하고 있는지를 확인함.

값이 같더라도 다른 메모리에 할당된 다른 객체이면 False가 반환됨.

# 예시로 차이점 확인
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)  # True  (값이 같음)
print(a is b)  # False (다른 객체)
print(a is c)  # True  (같은 객체)

print(id(a))   # 예: 140234567890
print(id(b))   # 예: 140234567912 (다른 주소)
print(id(c))   # 예: 140234567890 (같은 주소)

같이 보면 좋은 자료들

2023.10.06 - [Pages] - [Python] Control Structure and Control Flow

 

[Python] Control Structure and Control Flow

Control structure와 Control Flow란 무엇인가?2025.04.23 - [Python] - [Programming] Control Flow 와 Control Structure [Programming] Control Flow 와 Control StructureAbstraction(추상화)을 통한 이해프로그래밍 언어에서 Abstraction은

ds31x.tistory.com

2025.03.26 - [Python] - [Py] Bitwise Operator

 

[Py] Bitwise Operator

Operand로 int 와 bool , 또는 byte와 bytesarrays의 각 요소만 사용할 수 있다.bytes 나 bytearrays는 직접 사용이 안되고 각 요소 단위로 꺼내서 처리해야 한다.참고로,Bitwise Operator는 Tensor 등에서 Element-wise로

ds31x.tistory.com

https://dsaint31.tistory.com/516

 

[Python] Arithmetic in Python and Augmented Assignment

1. Precedence of Arithmetic OperationsHigher ** > -(negation) > * = / = // = % > + = -(subtraction) Lower우선순위를 기억하는 것도 중요하지만, 헷갈리면 그냥 parentheses로 묶어주면 된다. (가독성을 위해서도 이를 추천)

dsaint31.tistory.com

 


 

728x90

'Python' 카테고리의 다른 글

[Python] while statement, break and continue:  (0) 2023.07.28
[Python] if, elif, else statements  (0) 2023.07.28
[Python] List's methods  (0) 2023.07.17
[Python] Closure  (0) 2023.07.15
[Python] scope와 키워드 global, nonlocal  (0) 2023.07.15