if
if
If <์กฐ๊ฑด๋ฌธ>:
<์ํํ ๋ฌธ์ฅ1>
<์ํํ ๋ฌธ์ฅ2>
...
elif <์กฐ๊ฑด๋ฌธ>:
<์ํํ ๋ฌธ์ฅ1>
<์ํํ ๋ฌธ์ฅ2>
...
elif <์กฐ๊ฑด๋ฌธ>:
<์ํํ ๋ฌธ์ฅ1>
<์ํํ ๋ฌธ์ฅ2>
...
...
else:
<์ํํ ๋ฌธ์ฅ1>
<์ํํ ๋ฌธ์ฅ2>
...
์กฐ๊ฑด๋ฌธ ๋ค์์ ์ฝ๋ก (:)์ ์์ง ๋ง์!
if ์กฐ๊ฑด๋ฌธ์ ๊ธฐ๋ณธ์ฌ์ฉ
number = input("์ ์ ์
๋ ฅ> ")
number = int(number)
#0 ์ผ๋
if number == 0:
print("0์
๋๋ค.")
#์์ ์กฐ๊ฑด
if number > 0:
print("์์์
๋๋ค.")
#์์ ์กฐ๊ฑด
if number < 0:
print("์์์
๋๋ค.")
if~else ์ elif
# if ์กฐ๊ฑด๋ฌธ์ else ๊ตฌ๋ฌธ์ ์ถ๊ฐํด์ ์ง์์ ํ์ ๊ตฌ๋ถ
number = input("์ ์ ์
๋ ฅ> ")
number = int(number)
if number % 2 == 0:
# ์กฐ๊ฑด์ด ์ฐธ์ผ ๋, ์ฆ ์ง์ ์กฐ๊ฑด
print("์ง์์
๋๋ค")
else:
# ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋, ์ฆ ํ์ ์กฐ๊ฑด
print("ํ์์
๋๋ค")
pocket = ['paper', 'cellphone']
card = True
if 'money' in pocket:
print("๋์ผ๋ก ํ์๋ฅผ ํ๊ณ ๊ฐ๋ผ")
elif card:
print("์นด๋๋ก ํ์๋ฅผ ํ๊ณ ๊ฐ๋ผ")
else:
print("๊ฑธ์ด๊ฐ๋ผ")
# ์นด๋๋ก ํ์๋ฅผ ํ๊ณ ๊ฐ๋ผ
if ์กฐ๊ฑด๋ฌธ์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ
score = float(input("ํ์ ์
๋ ฅ> "))
#์กฐ๊ฑด์ผ๋ก ๊ตฌํํ๊ธฐ 1
if score == 4.5:
print("์ ")
elif 4.2 <= score < 4.5:
print("๊ต์๋์ ์ฌ๋")
elif 3.5 <= score < 4.2:
print("ํ ์ฒด์ ์ ์ํธ์")
elif 2.8 <= score < 3.5:
print("์ผ๋ฐ์ธ")
elif 2.3 <= score < 2.8:
print("์ผํ์ ๊ฟ๊พธ๋ ์์๋ฏผ")
elif 1.75 <= score < 2.3:
print("์ค๋ฝ๋ฌธํ์ ์ ๊ตฌ์")
elif 1.0 <= score < 1.75:
print("๋ถ๊ฐ์ด์ฒ๋ฏผ")
elif 0.5 <= score < 1.0:
print("์๋ฒ๋ ")
elif 0 < score < 0.5:
print("ํ๋ํฌํค")
elif score == 0:
print("์๋๋ฅผ ์์๊ฐ๋ ํ๋ช
์ ์จ์")
#์กฐ๊ฑด์ผ๋ก ๊ตฌํํ๊ธฐ 2
if score == 4.5:
print("์ ")
elif 4.2 <= score:
print("๊ต์๋์ ์ฌ๋")
elif 3.5 <= score:
print("ํ ์ฒด์ ์ ์ํธ์")
elif 2.8 <= score:
print("์ผ๋ฐ์ธ")
elif 2.3 <= score:
print("์ผํ์ ๊ฟ๊พธ๋ ์์๋ฏผ")
elif 1.75 <= score:
print("์ค๋ฝ๋ฌธํ์ ์ ๊ตฌ์")
elif 1.0 <= score:
print("๋ถ๊ฐ์ด์ฒ๋ฏผ")
elif 0.5 <= score:
print("์๋ฒ๋ ")
elif 0 < score:
print("ํ๋ํฌํค")
else:
print("์๋๋ฅผ ์์๊ฐ๋ ํ๋ช
์ ์จ์")
False๋ก ๋ณํ๋๋ ๊ฐ
None / '0' / ๋น ์ปจํ ์ด๋๋ False๋ก ๋ฐํ๋๋ค.
```python
if ์กฐ๊ฑด๋ฌธ์ 0 ๋ฃ๊ธฐ
if 0:
print("0์ True๋ก ๋ณํ๋ฉ๋๋ค")
else:
print("0์ False๋ก ๋ณํ๋ฉ๋๋ค")
0์ False๋ก ๋ณํ๋ฉ๋๋ค
if ์กฐ๊ฑด๋ฌธ์ ๋น ๋ฌธ์์ด ๋ฃ๊ธฐ
if "": print("๋น ๋ฌธ์์ด์ True๋ก ๋ณํ๋ฉ๋๋ค") else: print("๋น ๋ฌธ์์ด์ False๋ก ๋ณํ๋ฉ๋๋ค")
๋น ๋ฌธ์์ด์ False๋ก ๋ณํ๋ฉ๋๋ค
### Pass ํค์๋
- ๊ณจ๊ฒฉ์ ์ก์๋๊ณ ๋์ค์ ์ฒ๋ฆฌํ๊ธฐ ์ํด์ ๋ง๋ค์ด์ง
```python
number = input("์ ์ ์
๋ ฅ> ")
number = int(number)
# ๋น ๊ณต๊ฐ์ ์ฌ์ฉํ ๊ฒฝ์ฐ indentationError๊ฐ ๋ฐ์ํ๋ค
# if number > 0:
# ์์์ผ ๋: ์์ง ๋ฏธ๊ตฌํ ์ํ์
๋๋ค.
# else:
# ์์์ผ ๋: ์์ง ๋ฏธ๊ตฌํ ์ํ์
๋๋ค.
# ์ด๋ ์ฌ์ฉํ๊ธฐ ์ํด์ pass๋ฅผ ์ฌ์ฉํ์ฌ ๊ณจ๊ฒฉ์ ์ก์ ๋์ ์ ์๋ค
if number > 0:
pass
else:
pass
# raise NotImplementedError : ์์ง ๊ตฌํํ์ง ์์ ๋ถ๋ถ์ด๋ผ๊ณ ์ค๋ฅ๋ฅผ ๊ฐ์ ๋ก ๋ฐ์ํ ์ ์๋ค
if number > 0:
raise NotImplementedError
else:
raise NotImplementedError
๋ ์ง/์๊ฐ ํ์ฉํ๊ธฐ (if, format)
import datetime
now = datetime.datetime.now()
# ๋ ์ง/์๊ฐ ์ถ๋ ฅํ๊ธฐ : if
print(now.year , "๋
")
print(now.month , "์")
print(now.day , "์ผ")
print(now.hour , "์")
print(now.minute , "๋ถ")
print(now.second , "์ด")
# ๋ ์ง/์๊ฐ์ ํ์ค๋ก ์ถ๋ ฅํ๊ธฐ : format
print("{}๋
{}์ {}์ผ {}์ {}๋ถ {}์ด".format(
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second
))
# ์ค์ ๊ณผ ์คํ๋ฅผ ๊ตฌ๋ถํ๊ธฐ
if now.hour < 12:
print("ํ์ฌ ์๊ฐ์ {}์๋ก ์ค์ ์
๋๋ค.".format(now.hour))
if now.hour > 12:
print("ํ์ฌ ์๊ฐ์ {}์๋ก ์คํ์
๋๋ค.".format(now.hour))
# ๊ณ์ ์ ๊ตฌ๋ถํ๊ธฐ
if 3 <= now.month <= 5:
print("์ด๋ฒ ์๊ฐ์ {}์๋ก ๋ด์
๋๋ค.".format(now.month))
if 6 <= now.month <= 8:
print("์ด๋ฒ ์๊ฐ์ {}์๋ก ์ฌ๋ฆ์
๋๋ค.".format(now.month))
if 9 <= now.month <= 11:
print("์ด๋ฒ ์๊ฐ์ {}์๋ก ๊ฐ์์
๋๋ค.".format(now.month))
if now.month == 12 or 1 <= now.month <= 2:
print("์ด๋ฒ ์๊ฐ์ {}์๋ก ๊ฒจ์ธ์
๋๋ค.".format(now.month))
elif๋ฅผ ์ด์ฉํ์ฌ ๊ณ์ ๊ตฌํ๊ธฐ
import datetime
# ํ์ฌ ๋ ์ง/์๊ฐ์ ๊ตฌํ๊ณ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ์์ ๋ณ์์ ์ ์ฅํฉ๋๋ค.
now = datetime.datetime.now()
month = now.month
# ์กฐ๊ฑด๋ฌธ์ผ๋ก ๊ณ์ ์ ํ์ธํฉ๋๋ค.
if 3 <= month <= 5:
print("ํ์ฌ๋ ๋ด์
๋๋ค.")
elif 6 <= month <= 8:
print("ํ์ฌ๋ ์ฌ๋ฆ์
๋๋ค.")
elif 9 <= month <= 11:
print("ํ์ฌ๋ ๊ฐ์์
๋๋ค.")
else:
print("ํ์ฌ๋ ๊ฒจ์ธ์
๋๋ค.")
๋์๋ฆฌ๋ก ์ง์์ ํ์ ๊ตฌ๋ถํ๊ธฐ
number = input("์ ์ ์
๋ ฅ> ")
last_character = number[-1] #๋ง์ง๋ง ์๋ฆฌ ์ซ์๋ฅผ ์ถ์ถ
last_number = int(last_character) # ์ซ์๋ก ๋ณํํ๊ธฐ
# ์ง์ ํ์ธ
if last_number == 0 \
or last_number == 2 \
or last_number == 4 \
or last_number == 6 \
or last_number == 8:
print("์ง์ ์
๋๋ค")
# ํ์ ํ์ธ
if last_number == 1 \
or last_number == 3 \
or last_number == 5 \
or last_number == 7 \
or last_number == 9:
print("ํ์ ์
๋๋ค")
# in ๋ฌธ์์ด ์ฐ์ฐ์๋ฅผ ํ์ฉํด์ ์ง์์ ํ์ ๊ตฌ๋ถ
if last_character in "02468":
print("์ง์ ์
๋๋ค")
if last_character in "13579":
print("ํ์ ์
๋๋ค")
# ๋๋จธ์ง ์ฐ์ฐ์๋ฅผ ํ์ฉํด์ ์ง์์ ํ์ ๊ตฌ๋ถ
number = int(number)
if number % 2 == 0:
print("์ง์ ์
๋๋ค")
if number % 2 == 1:
print("ํ์ ์
๋๋ค")
Last updated
Was this helpful?