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?