📘Python
Welcome to Python : if문
4:Bee
2023. 7. 9. 15:28
728x90
- if문 : https://hi-code.tistory.com/116
- while문 : https://hi-code.tistory.com/117
- for문 : https://hi-code.tistory.com/118
| Title | Code | Console |
| if문 | ||
| if문 기본 구조 |
money = True
if money:
print("택시를 타고 가라")
else:
print("걸어가라")
|
택시를 타고 가라 |
| 들여쓰기 방법 indent_error.py(123p) |
money = True
if money:
print("택시를")
print("타고")
print("가라")
|
File "...Quiz.py", line 6 print("가라") IndentationError: unexpected indent |
| 들여쓰기 방법 indent_error.py(124p) |
money = True if money:
print("택시를")
print("타고")
print("가라")
|
|
| and, or ,not |
x or y |=> x,y 둘 중 하나 True = True |
- |
|
x and y |=> x(True),y(True) 둘 다 True = True x and y |=> x(True),y(False) 둘 중 하나 True = False
|
||
|
not x |=> x가 False니까 = True |
||
| in, not in |
print(1 in [1, 2, 3])
print(1 not in [1, 2, 3])
|
True False |
|
print('a' in ('a', 'b', 'c'))
print('j' not in 'python')
|
True True |
|
|
pocket = ['paper', 'cellphone', 'money']
if 'money' in pocket:
print("택시를 타고 가라")
else:
print("걸어가라")
|
택시를 타고 가라` | |
| pass |
pocket = ['paper', 'cellphone', 'money']
if 'money' in pocket:
pass
else:
print("카드를 꺼내라")
|
by 'pass' so nothing print |
| elif |
#Before (only use to ' if ')
pocket = ['paper', 'cellphone', 'money']
card = True
if 'money' in pocket:
print("택시를 타고 가라")
else:
if card:
print("택시를 타고 가라")
else:
print("걸어가라")
|
택시를 타고 가라 |
|
# After pocket = ['paper', 'cellphone', 'money']
card = True
if 'money' in pocket:
print("택시를 타고 가라")
elif card:
print("택시를 타고 가라")
else:
print("걸어가라")
|
택시를 타고 가라 | |
| 조건부 표현식 (conditional expression) |
score = 100
if score >= 60:
message = "success"
print(message)
else:
message = "failure"
print(message)
|
success |
|
score = 100
message = "success" if score >= 60 else "failure"
print(message)
|
success | |
728x90