[Python] if-else

정현석·2020년 9월 21일
0

python

목록 보기
3/3

if- else

def plus(a=0,b=0):
if type (a,b) is int or type (a,b) is float: //조건문
return a + b
else : //조건이 아닐 경우
return "error"
print(plus(a=5,b=5.5)

age check

>def age_check(age) :
print(f"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)  //출력

//if 문이 true 해당 블록 실행, 만약 false일 경우 다음 elif 블록 실행, 어느것도 tuue가 아닐 경우 else 실행

moudule import

import math // 이 경우 math에 있는 함수를 모두 불러옴 (효율이 떨어짐)
print(math.ceil(1.2))

특정 함수만 불러올 경우

from math import ceil, fsum

print (ceil(1.5))
print (fsum([1,2,3,4,5,6,7]))
//함수의 이름 변경
from math import fsum as (변경이름)

ex) from calculator import plus
print (plus( 1, 2))

calculator.py 생성 후
def plus (a,b):
return a + b

//다른 파일에서 import , plus 기능 사용

profile
기록하는 벨로그

0개의 댓글