본문 바로가기

Python

(13)
Python [import] 1) import 방법 import math print(math.ceil(1.2)) print(math.fsum([1, 2, 3])) 위 처럼 import 작성 후 import 함수를 사용하면 된다. 하지만, 위 처럼 외부 api 전체를 불러올 필요가 없다. (효율측면) 2) 특정 함수만 import from math import ceil, fsum print(ceil(1.2)) print(fsum([1, 2, 3])) 이렇게 하면 1, 2는 같은 결과 값을 갖는다. 3) import 한 함수의 명칭을 변경 할 수 있다. from math import ceil, fsum as sum print(ceil(1.2)) print(sum([1, 2, 3])) - 위의 fsum 함수를 sum 으로 명칭 변경하여 ..
Python [for in] days = ("Mon", "Tue", "Wed", "Thu", "Fri") for day in days: if day is "wed": break else: print(day) 결과 값: Mon Tue for 변수명 in 리스트명: 리스트 안의 데이터를 차례로 day 에 넣는다. python for 문은 for in 한 구문 밖에 지원을 하지않는다. 위 형식처럼 리스트 내의 key 를 가지고 반복 수행 할 구문을 작성하는 형식
Python [논리 연산자 and, or, not] def age_check(age): print(f"you are {age}") if age 20 and age < 25: print("you are still young") elif not age < 50: print("you have to check your condition") else: print("enjoy your drink") age_check(23) https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
Python if - elif - else def plus(a, b): if type(b) is int or type(b) is float: return a + b elif type(b) is str: print("dont put here str") else: return None print(plus(12, "10")) 결과 값 : dont put here str None 소괄호 로 묶지 않고 위 처럼 if - elif - else 조건문 : 를 사용하면 된다.
Python [format] 스트링 안에 변수 값 넣고 출력 def say_hello(name, age): return "hellp {} you are {} year".format(name, age)
Python [function] arg 위치 지정 호출 def minus(a, b): return a - b result = minus(b=30, a=1) -29
Python [function] return , void 차이 def p_plus(a, b): print(a + b) def r_plus(a, b): return a + b p_result = p_plus(2, 3) r_result = r_plus(2, 3) print(p_result, r_result) 결과 값 : 5 - p_plus 의 print 로 출력 된 값 None 5 none - p_plus 의 return 값이 없어서 none 5 - r_result 의 return 값
Python [function] argugment use def say_hi(who): print("hi!!", who) say_hi("kim") => hi!! kim def plus(a, b): print(a + b) def minus(a, b): print(a - b) plus(2, 1) => 3 minus(2, 1) => 1 def plus(a, b=3): 5