[as] as 키워드를 이용하여 모듈 이름을 단축 시킬 수 있다. # 파일명 : calculator.py def add(n1,n2): n1+n2 def sub(n1,n2): n1-n2 import calculator as cal print( cal.add(1,2) ) 다음과 같이 as를 써서 calculator을 cal이라고 짧게 호칭할 수 있다. [from] from 키워드를 이용해서 모듈의 특정 기능만 이용할 수 있다. from calculator import add as a print( a(1,2)) 마치 실행파일 내에 add 라는 함수가 있는 것 처럼 add를 저렇게 바로 쓸 수 있다. 물론 저기선 as를 연습하느라고 a라고 줄여썼다. '*' 는 전체를 의미한다. 모듈 내의 모든 기능을 다 가져올..
[모듈] 모듈이란 이미 만들어진 기능으로 사용자가 쉽게 사용할 수 있다. ex) math, random, 날짜/시간 모듈 etc.. 내부 모듈 : 파이썬 설치시 기본적으로 사용할 수 있는 모듈 외부 모듈 : 별도 설치 후 사용할 수 있는 모듈 ex) pandas, numpy etc.. 사용자 모듈 : 사용자가 직접 만든 모듈 [실습1] random 모듈을 이용해서 1부터 10까지 정수 중 난수 1개 발생시켜보자. import random rNum = random.randint(1,10) print(f'rNum:{rNum}') random.randint(a,b) 는 a부터 b까지의 정수 중 난수를 1개 발생시킨다. [실습2] random 모듈을 이용해서 0부터 100 사이의 난수 10개 발생시켜보자. im..
[lambda] 함수 선언을 보다 간단하게 할 수 있다. def calculator_1(a,b): # 기존의 함수 선언 방식 return a+b print(calculator_1(1,1)) calculator2 = lambda a, b : a+b # 람다를 통한 간편한 방식 print(calculator2(1,1)) ''' 2 2 ''' lambda는 다음과 같이 함수 명 없이, 변수에 함수를 할당하여 사용할 수 있다. [실습1] 삼각형, 사각형, 원의 넓이를 반환하는 lambda 함수를 만들어보자. triangleArea = lambda width, height : round(width*height/2,2) squareArea = lambda width,height : round(width*height..
[중첩함수] 함수 안에 또다른 함수가 있는 형태이다. 내부 함수를 밖에서 호출할 수 없다. def out_function(): print('out function is called') def in_function(): print('in function is called') in_function() in_function() ''' Traceback (most recent call last): File "c:\Users\Jupiter\Desktop\mygit\LearnInZeroBase\파이썬 중급\3_중첩함수.py", line 9, in in_function() NameError: name 'in_function' is not defined. Did you mean: 'out_function'? ''' 이..