시작하기무료로 시작하기

Truthy, True, Falsey, and False

비교 연산은 진릿값을 확인하지만, 값이 truthy하다고 해서 곧바로 True인 것은 아니에요. 그 반대도 마찬가지로, falsey하다고 해서 False와 동일하지는 않아요. 따라서 어떤 것이 True/False인지와 truthy/falsey인지 구분해 꼼꼼히 확인해야 합니다. Python에서는 두 값이 완전히 동일한지 확인할 때 is 연산자를 사용해요. 이번에는 이전 연습 문제와 같은 키들(species, flipper_length, body_mass, sex)에 더해 불리언 값을 가진 tracked 키가 있는 펭귄 상세 정보 사전을 사용하겠습니다.

단일 펭귄의 모든 정보를 담은 사전 penguin_305_details를 미리 로드해 두었어요.

이 연습은 강의의 일부입니다

Python의 데이터 타입

강의 보기

연습 안내

  • penguin_305_detailssex 키가 truthy한지 확인하세요.
    • truthy하면, sexTrue인지 확인해 그 결과를 sex_is_true에 저장하세요.
    • sex 키의 값과 sex_is_true를 출력하세요.
  • penguin_305_detailstracked 키가 truthy한지 확인하세요.
    • truthy하면, trackedTrue인지 확인해 그 결과를 tracked_is_true에 저장하세요.
    • tracked 키의 값과 tracked_is_true를 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Check the truthiness of penguin_305_details sex key
if ____["____"]:
	# If true, check if sex is True and store it as sex_is_true
    sex_is_true = penguin_305_details["sex"] ____ ____
    # Print the sex key's value and sex_is_true
    print(f"{____['____']}: {____}")

# Check the truthiness of penguin_305_details tracked key
if ____["____"]:
	# If true, check if tracked is True and store it as tracked_is_true
    tracked_is_true = penguin_305_details["tracked"] ____ ____
    # Print the tracked key and tracked_is_true
    print(f"{____['____']}: {____}")
코드 편집 및 실행