Python에서 if not 조건 사용
    
    
            Najwa Riyaz
    2023년10월10일
    
    Python
    Python Condition
    
 
if문은 조건이 발생하지 않았는지 평가하기 위해not논리 연산자와 결합됩니다. 이 기사에서는 Python에서if not조건을 사용하는 방법을 설명합니다.
다음은이 조건을 보여주는 코드 블록입니다.
if not a_condition:
    block_of_code_to_execute_if_condition_is_false
위의 경우a_condition의 결과가False이면block_of_code_to_execute_if_condition_is_false코드가 성공적으로 실행됩니다.
Python의 참 및 거짓 값
시작하기 전에 다음과 같은 경우에 동등한 값이 Python에서False임을 이해합시다.
- 0,- 0L,- 0.0과 같은 숫자 0 값
- 다음과 같은 빈 시퀀스 :
- 빈 목록 []
- 빈 사전 {}
- 빈 문자열 ''
- 빈 튜플
- 빈 세트
- None개체
 
Python에서if not조건의 예
다음은 Python에서만약이 어떻게 활용되는지 이해하는 데 도움이되는 몇 가지 예입니다.
부울값의 사용
if not False:
    print("not of False is True.")
if not True:
    print("not of True is False.")
출력:
not of False is True.
숫자값 사용
예를 들어0,0L,0.0과 같은 값은False값과 연관됩니다.
if not 0:
    print("not of 0 is True.")
if not 1:
    print("not of 1 is False.")
출력:
not of 0 is True.
값의목록사용
if not []:
    print("An empty list is false. Not of false =true")
if not [1, 2, 3]:
    print("A non-empty list is true. Not of true =false")
출력:
An empty list is false. Not of false =true
사전값 사용
if not {}:
    print("An empty dictionary dict is false. Not of false =true")
if not {"vehicle": "Car", "wheels": "4", "year": 1998}:
    print("A non-empty dictionary dict is true. Not of true =false")
출력:
An empty dictionary dict is false. Not of false =true
값의문자열사용
if not "":
    print("An empty string is false. Not of false =true")
if not "a string here":
    print("A non-empty string is true. Not of true =false")
출력:
An empty string is false. Not of false =true
없음값 사용 :
if not None:
    print("None is false. Not of false =true")
출력:
None is false. Not of false =true
값집합의 사용 :
dictvar = {}
print("The empty dict is of type", type(dictvar))
setvar = set(dictvar)
print("The empty set is of type", type(setvar))
if not setvar:
    print("An empty set is false. Not of false =true")
출력:
   The empty dict is of type <class 'dict'>
   The empty set is of type <class 'set'>
   An empty dictionary dict is false. Not of false =true
값의튜플사용
빈 튜플이False값과 연결됨.
if not ():
    print("1-An empty tuple is false. Not of false =true")
if not tuple():
    print("2-An empty tuple is false. Not of false =true")
출력:
1-An empty tuple is false. Not of false =true
2-An empty tuple is false. Not of false =true
        튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다