본문 바로가기
Python

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

by ds31x 2023. 7. 28.

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를 반환함.

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

같이 보면 좋은 자료들

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

 

[Python] Control Structure and Control Flow

Control structure와 Control Flow란 무엇인가? http://ds31x.blogspot.com/2023/07/basic-control-structures-and-control.html Basic : Control Structures and Control Flow Control Structure 프로그램을 구성하는 statement (=excutable code)들이 실

ds31x.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