파이썬 강의 노트 - 조건문을 위한 불리언 표현법

복합 불리언 표현 COMPLEX BOOLEAN EXPRESSIONS

if 18.5 <= weight / height**2 < 25:
    print("BMI is considered 'normal'")

if is_raining and is_sunny:
    print("Is there a rainbow?")

if (not unsubscribed) and (location == "USA" or location == "CAN"):
    print("send email")

좋은 예와 나쁜 예

  1.  True  와  False  를 조건에 직접 입력하는 것은 의미가 없습니다.
    # 나쁜 예
    if True:
        print("이 코드는 모든 조건에서 실행됩니다.")
    이와 같은 예로, True 와 False 모두를 만족하도록 만든 조건문도 의미가 없습니다.
    # 또 다른 나쁜 예
    if is_cold or not is_cold:
        print("이 코드는 모든 조건에서 실행됩니다.")
  2. 논리 연산자를 사용할 때는 표현법에 주의해야 합니다.
     and or not  을 사용할 때는 영어처럼 표현하면 오류가 발생합니다.
    # 나쁜 예
    if weather == "snow" or "rain":
        print("Wear boots!")
    # 좋은 예
    if weather == "snow" or weather == "rain":
        print("Wear boots!")
  3. 불리언 값인  True  나  False  를 비교 대상으로 사용하지 않습니다
    # 나쁜 예
    if is_cold == True:
        print("The weather is cold!")
    # 좋은 예
    if is_cold:
        print("The weather is cold!")
    False 값인지를 확인하려면  not  연산자를 사용하면 됩니다.

False로 분류되는 값

  • false를 의미하는 값:  None  과  False 
  • 0을 의미하는 모든 형태:  0 0.0 0j Decimal(0) Fraction(0, 1) 
  • 비어있는 괄호 또는 상태:  '"" ( ) [ ] { } set( ) range(0) 
















Comments

Popular Posts