Conditional Statement

김남연·2024년 1월 3일

Python

목록 보기
5/12

if statement

if <<condition>>:
   <<block1>>    

else:
   <<block2>>
  • condition
    결과값으로 True 또는 False 값을 가진다.
    True일 경우 block1의 내용을 수행함
  • else
    condition의 결과값이 False일 경우에 block2의 내용을 수행함. 필요할 때 사용

elif statement

if <<condition1>>:
   <<block1>>

elif <<condition2>>:
   <block2>>
  • if구문에서 condition1이 False일 경우에 수행하는 조건문
  • condition1이 False이고 condition2가 True일 때 block2를 수행함
  • 두 개의 독립적인 if조건문과는 구분해야한다.
  • elif는 여러개 사용 가능

Nested if statements

  • if구문 안의 if구문
if 0<=ph<=14:
    if ph<7.0:
        print(ph, "is acidic.")
    elif ph>7.0:
        print(ph, "is basic.")
    else:
        print(ph, "is neutral.")
else:
    print("ph value has to be a number between 0 and 14)

0개의 댓글