Python

[python] if 문

nang. 2020. 9. 3. 22:19
반응형
SMALL

11. if 문

if 조건문:
    수행할 문장 # 같은 들여쓰기로 / 따로 괄호 필요 없음
else:
    수행할 문장

11.1 x in s / x not in s

in not in
x in 리스트 x not in 리스트
x in 튜플 x not in 튜플
x in 문자열 x not in 문자열

 

1 in [1,2,3]
>> True

1 not in [1,2,3]
>> False

'a' in ('a', 'b', 'c')
>> True

'j' not in 'python'
>> True

 

if X IN

data = ['a', 'b', 'c']

if 'a' in data:
    print("ok")
else:
    print("not ok")

>> ok

 

11.2 조건문에서 아무 일도 하지 않도록 하기

pass

if 'a' in data:
    pass

 

11.3 else if

elif

if 'a' in data:
    pass
elif 'b' in data:
    print("b ok")
elif 'c' in data:
    print("c ok")
else:
    print("not ok")

 

11.4 축약형

if score >= 60:
    message = "success"
else:
    message = "failure"

>>

message = "success" if score >= 60 else "failure"
반응형
LIST

'Python' 카테고리의 다른 글

[python] for 문  (0) 2020.09.03
[python] while 문  (0) 2020.09.03
[python] 배열 (array)  (0) 2020.09.03
[python] print  (0) 2020.09.03
[python] 변수 선언  (0) 2020.09.03