(Control Structures 03) else 문
SoloLearn Python 번역
- An
elsestatement follows anifstatement, and contains code that is called when the if statement evaluates toFalse.else문은if문 뒤에 온다.if문이False로 평가될 때 호출되는 코드를 포함한다.
- As with
ifstatements, the code inside the block should be indented.if문과 마찬가지로, 블록 내부의 코드는 들여쓰기 해야 한다.
x = 4
if x == 5:
print("Yes")
else:
print("No")
# No
- You can chain
ifandelsestatements to determine which option in a series of possibilities is true.if와else문을 연결해서, 어떤 옵션이 참(true)인지 결정할 수 있다.
num = 7
if num == 5:
print("Number is 5")
else:
if num == 11:
print("Number is 11")
else:
if num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")
# Number is 7
elif Statements
elif 문
- The
elif(short for else if) statement is a shortcut to use when chainingifandelsestatements.elif(else if의 단축)문은if와else문을 열결할 때 사용하는 단축이다.
- A series of
if elifstatements can have a finalelseblock, which is called if none of theiforelifexpressions is True.- 일련의
if elif문은 최종else블록을 가질 수 있다. if또는elif표현식 중 어느 것도 참(True)이 아닌 경우 호출된다.
- 일련의
num == 7
if num == 5:
print("Number is 5")
elif num == 11:
print("Number is 11")
elif num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")
# Number is 7
In other programming languages, equivalents to the
elifstatement have varying names, includingelse if,elseiforelsif.다른 프로그래밍 언어에서
elif문과 동일한 것으로else if,elseif,elsif가 있다.
QUIZ
- What is the result of this code?
- 이 코드의 결과는 무엇인가?
if 1 + 1 == 2:
if 2 * 2 == 8:
print("if")
else:
print("else")
else
- A shorter option for an “else if” statement is:
- “else if” 문의 단축은 무엇인가?
elif