데이터분석/첫째주

파이썬 Python : 논리 연산자

핑크댕댕이 2023. 9. 20. 03:30
728x90

 

논리 연산자

논리 연산자란, 피연산자의 논리(True, False)를 이용한 연산이다.

 and 연산     

  • [   A and B   ]   A와 B가 모두 True 인 경우에만 결과값이 True 이다.
# 파이썬

print('[and 연산]')
print('{} and {}\t결과값: {}'.format(True, True, True and True))
print('{} and {}\t결과값: {}'.format(True, False, True and False))
print('{} and {}\t결과값: {}'.format(False, True, False and True))
print('{} and {}\t결과값: {}'.format(False, False, False and False))
-- 출력 --

[and 연산]
True and True 결과값: True
True and False 결과값: False
False and True 결과값: False
False and False 결과값: False

 

 or 연산     

  • [   A or B   ]   A와 B중 어느 하나만 True 이면, 결과값이 True 이다.
# 파이썬

print('[or 연산]')
print('{} or {}\t결과값: {}'.format(True, True, True or True))
print('{} or {}\t결과값: {}'.format(True, False, True or False))
print('{} or {}\t결과값: {}'.format(False, True, False or True))
print('{} or {}\t결과값: {}'.format(False, False, False or False))
-- 출력 --

[or 연산]
True or True 결과값: True
True or False 결과값: True
False or True 결과값: True
False or False 결과값: False

 

 not 연산 (부정 연산자)     

  • [   not A   ]    A의 상태를 부정하는 결과를 나타낸다.
# 파이썬

print('[not 연산]')
print('not {}\t결과값: {}'.format(True, not True))
print('not {}\t결과값: {}'.format(False, not False))
-- 출력 --

[not 연산]
not True 결과값: False
not False 결과값: True

 

# 파이썬

print('\"백신 접종 대상자는 20세미만 또는 65세 이상자에 한합니다.\"')
age = int(input('\n정보를 입력해주세요.\n나이 : '))
print('result : {}'.format(age<20 or age>=65))
-- 출력 --

"백신 접종 대상자는 20세미만 또는 65세 이상자에 한합니다."

정보를 입력해주세요.
나이 : 20
result : False
 
 

 

반응형