이 포스트는 개인적으로 공부한 내용을 정리한 글입니다.
자세한 내용은 아래 참고자료를 봐주시면 감사하겠습니다.
파이썬의 bool
은 엄밀히 따지면 논리 자료형이지만, 내부적으로 1(True) 0(False)로 처리되는 int의 서브클래스이다. int는 object의 하위클래스이기에 결국
object > int > bool
라고 한다. 출처 - 파이썬 알고리즘 인터뷰 108p
기본적인 사용법은 다음과 같습니다.
>>> True
True
>>> False
False
>>> True == False
False
>>> True == 1
True
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False
>>> True or True
True
>>> True or False
True
>>> False or True
True
>>> False or False
False
맞다 - 맞다
맞는게 아니다 - 아니다
맞는게 아닌게 아니다 - 맞다
맞는게 아닌게 아닌게 아니다 - 아니다
>>> True
True
>>> not True
False
>>> not not True
True
>>> not not not True
False
bool 형변환
bool()
은 0일때 False, 1일때 True 이며, 값이 있으면 True, 없으면 False입니다.
>>> not True
False
>>> not not True
True
>>> not not not True
False
>>> bool(True)
True
>>> bool(False)
False
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool([])
False
>>> bool([1, 3])
True
>>> bool("False")
True