Python

[Python]if...else, elif..or/and

sagesse2021 2021. 11. 19. 16:56
반응형

// b의 타입이 number일때 a+b를 return하고, b가 number가 아니라면 None을 return한다

// b의 타입이 int이거나 float이라면 a+b를 return하고, 위의 조건이 아니라면 None을 return

def plus(a,b):
  if type(b) is int or type(b) is float:
      return a + b
  else:
      return None


print(plus(12, 1.2))

13.2

def age_check(age):
  print(f"you are {age}") //string안에 변수를 출력하는 문법
  if age < 18:
    print("you cant drink")
  elif age == 18:
    print("you are new to this!")
  elif age >20 and age <25:
    print("you are still kind of young")
  else:
    print("enjoy your drink")
    
age_check(23)

you are 23

you are still kind of young

  • elif = else if
  • and을 사용할때 조건이 둘다 true여야 함, 하나라도 false면 실행되지x
  • if문과 elif문이 모두 false일때 else문이 실행됨
def age_check(age):
  print(f"you are {age}") # =("you are" + age)
  if age < 18:
    print("you cant drink")
  elif age == 18 or age == 19:
    print("you are new to this!")
  elif age >20 and age <25:
    print("you are still kind of young")
  else:
    print("enjoy your drink")
    
age_check(19)

you are 19

you are new to this!

  • or은 둘중 하나만 true면 실행됨
반응형

'Python' 카테고리의 다른 글

[Python]모듈(Module)  (0) 2021.11.20
[Python]for in 반복문  (0) 2021.11.19
[Python]Code Challenge  (0) 2021.11.19
[Python]Keyworded Arguments  (0) 2021.11.19
[Python]return  (0) 2021.11.18