ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ํŒŒ์ด์ฌ - ๋ถˆ ์ž๋ฃŒํ˜•๊ณผ if ์กฐ๊ฑด๋ฌธ

Post Series
  • ๋ถˆ ์—ฐ์‚ฐํ•˜๊ธฐ : ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
  • if ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ
  • ๋‚ ์งœ/์‹œ๊ฐ„ ํ™œ์šฉํ•˜๊ธฐ
  • ์ปดํ“จํ„ฐ์˜ ์กฐ๊ฑด - if ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๊ตฌ๋ถ„ํ•ด๋ณด๊ธฐ

๋ถˆ ์—ฐ์‚ฐํ•˜๊ธฐ : ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

python์—๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ not, and, or ๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

not ์—ฐ์‚ฐ์ž

not ์—ฐ์‚ฐ์ž๋Š” ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ํ•˜๋‚˜์ธ ๋‹จํ•ญ ์—ฐ์‚ฐ์ž ์ž…๋‹ˆ๋‹ค.

>>> print(not True)
False
>>> print(not False)
True
>>>

not ์—ฐ์‚ฐ์ž ์กฐํ•ฉํ•˜๊ธฐ

>>> x = 10
>>> under_20 = x < 20
>>> print(under_20)
True
>>> print(not under_20)
False
>>>

and ์—ฐ์‚ฐ์ž์™€ or ์—ฐ์‚ฐ์ž

>>> print (True and True)
True
>>> print (True and False)
False
>>> print (False and False)
False
>>> print (True or True)
True
>>> print (True or False)
True
>>> print (False or False)
False

if ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ

if ์กฐ๊ฑด๋ฌธ์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

if ์กฐ๊ฑด :  -> if ์กฐ๊ฑด๋ฌธ ๋’ค์—๋Š” ๋ฐ˜๋“œ์‹œ ํด๋ก (:)์„ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    ๋ฌธ์žฅ
    ๋ฌธ์žฅ

์กฐ๊ฑด๋ฌธ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ

number = input("์ •์ˆ˜ ์ž…๋ ฅ>")
number = int(number)

if number > 0:
    print("์–‘์ˆ˜์ž…๋‹ˆ๋‹ค.")

if number < 0:
    print("์Œ์ˆ˜์ž…๋‹ˆ๋‹ค.")

if number == 0 :
    print("0์ž…๋‹ˆ๋‹ค.")

โ–ถ ์‹คํ–‰๊ฒฐ๊ณผ

์ •์ˆ˜ ์ž…๋ ฅ>273
์–‘์ˆ˜์ž…๋‹ˆ๋‹ค.
์ •์ˆ˜ ์ž…๋ ฅ>0
0์ž…๋‹ˆ๋‹ค.
์ •์ˆ˜ ์ž…๋ ฅ>-52
์Œ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‚ ์งœ/์‹œ๊ฐ„ ํ™œ์šฉํ•˜๊ธฐ

๋‚ ์งœ/์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ

import datetime # ๋‚ ์งœ/์‹œ๊ฐ„๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

now = datetime.datetime.now() # ํ˜„์žฌ ๋‚ ์งœ/์‹œ๊ฐ„์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

print(now.year, "๋…„") # 2021 ๋…„
print(now.month, "์›”") # 7 ์›”
print(now.day, "์ผ") # 19 ์ผ
print(now.hour, "์‹œ") # 13 ์‹œ
print(now.minute, "๋ถ„") # 41 ๋ถ„
print(now.second, "์ดˆ") # 39 ์ดˆ

๋‚ ์งœ/์‹œ๊ฐ„ ํ•œ ์ค„๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

import datetime # ๋‚ ์งœ/์‹œ๊ฐ„๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

now = datetime.datetime.now()

print("{}๋…„ {}์›” {}์ผ {}์‹œ {}๋ถ„ {}์ดˆ".format(
  now.year,
  now.month,
  now.day,
  now.hour,
  now.minute,
  now.second
  ))

โ–ถ ์‹คํ–‰๊ฒฐ๊ณผ

2021๋…„ 7์›” 19์ผ 13์‹œ 44๋ถ„ 52์ดˆ

์˜ค์ „๊ณผ ์˜คํ›„๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

import datetime

now = datetime.datetime.now()

if now.hour < 12 :
  print("ํ˜„์žฌ ์‹œ๊ฐ์€ {}์‹œ๋กœ ์˜ค์ „์ž…๋‹ˆ๋‹ค!".format(now.hour))
if now.hour >= 12 :
  print("ํ˜„์žฌ ์‹œ๊ฐ์€ {}์‹œ๋กœ ์˜คํ›„์ž…๋‹ˆ๋‹ค!".format(now.hour))

โ–ถ ์‹คํ–‰๊ฒฐ๊ณผ

ํ˜„์žฌ ์‹œ๊ฐ์€ 13์‹œ๋กœ ์˜คํ›„์ž…๋‹ˆ๋‹ค!

๊ณ„์ ˆ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

import datetime

now = datetime.datetime.now()

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))

โ–ถ ์‹คํ–‰๊ฒฐ๊ณผ

์ด๋ฒˆ ๋‹ฌ์€ 7์›”๋กœ ์—ฌ๋ฆ„์ž…๋‹ˆ๋‹ค!'

์ปดํ“จํ„ฐ์˜ ์กฐ๊ฑด - if ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๊ตฌ๋ถ„ํ•ด๋ณด๊ธฐ

๋์ž๋ฆฌ๋กœ ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๊ตฌ๋ถ„

number = input("์ •์ˆ˜ ์ž…๋ ฅ>")
last_character = number[-1]
last_character = int(last_character)

if last_character == 0 \
    or last_character == 2 \
    or last_character == 4 \
    or last_character == 6 \
    or last_character == 8 :
    print("์ง์ˆ˜์ž…๋‹ˆ๋‹ค")

if last_character == 1 \
    or last_character == 3 \
    or last_character == 5 \
    or last_character == 7 \
    or last_character == 9 :
    print("์ง์ˆ˜์ž…๋‹ˆ๋‹ค")

ํŒŒ์ด์ฌ์—์„œ ์ค„์ด ๋„ˆ๋ฌด ๊ธธ์–ด์งˆ ๋•Œ๋Š” \ ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ค„๋ฐ”๊ฟˆํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

in ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๊ตฌ๋ถ„

number = input("์ •์ˆ˜ ์ž…๋ ฅ>")
last_character = number[-1]

if last_character in "02468":
    print("์ง์ˆ˜์ž…๋‹ˆ๋‹ค.")
if last_character in "13579":
    print("ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.")

๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด์„œ ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๊ตฌ๋ถ„

number = input("์ •์ˆ˜ ์ž…๋ ฅ>")
number = int(number)

if number % 2 == 0 :
    print("์ง์ˆ˜์ž…๋‹ˆ๋‹ค.")
if number % 2 == 1 :
    print("ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.")

Reference