Making decisions
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
선택하기
if은 조건이 참일 때만 코드를 실행합니다.- 조건 뒤에는 콜론
:이 따라옵니다. if에 속하는 줄들은 들여쓰기됩니다(4개 공백).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
비교하기
- 조건은 보통 True 또는 False인 비교식입니다.
==는 같음,!=는 다름,<><=>=를 의미합니다.- 참고:
==은 비교하여 동등성을 검사합니다; 단일=은 값을 저장합니다.
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
elif("else if")은 첫 번째 조건이 거짓일 때 다른 조건을檢查합니다.else는 모든 조건이 거짓일 때 실행됩니다.- Python은 이들을 순서대로 확인하여 첫 번째로 참인 것에서 멈춥니다.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
나머지
a % b은a을b으로 나눈 후 남아있는 값입니다.year % 4 == 0은 해가 4로 정확히 나누어지는 것을 의미합니다 — 남은 것이 없습니다.8 % 3은2입니다. 왜냐하면 8은 두 개의 3과 함께 2가 남기 때문입니다.
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
조건 결합하기
and은 양쪽 모두 참일 때만 참입니다.or은 적어도 한 쪽이 참일 때 참입니다.not은 참을 거짓으로, 거짓을 참으로 바꿉니다.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
캐미지아 가위코드에서
- Python의
if/elif/else은 시험의IF … ELSE … ENDIF과CASE에 해당합니다.
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
흔한 실수
==과 비교하십시오; 단일=은 할당합니다.elif을 쓰고else if대신 사용하며, 각if/elif/else줄 끝에:을 넣으십시오.if아래에 있는 들여쓰기 블록은 조건이 참일 때 실행되는 부분입니다.
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
이제 직접 해보기
- 실행할 때 코드 아래 상자에 입력을 입력하고 Enter를 누르십시오.
- 체크는 입력을 자동으로 입력합니다. 읽어보고, what to print할지 결정하십시오.
- 출력 텍스트를 완전히 일치시키십시오.
if / elif / else
Conditions are checked top-down; only the first true branch runs.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Dividing by 4 is not the whole rule. A year that also divides by 100 is not a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or Common.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.