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?