[Python] 모듈화

##모듈 1 : mod1.py def modfunc1(a,b):     return a+b ##모듈 2 : mod2.py def modfunc2(a,b):     return a-b ##모듈 3 : mod3.py def modfunc3(a,b):     return a*b ##메인 : main.py import mod1 import mod2 as alias_mod    #모듈 별칭 설정 from mod3 import modfunc3   #모듈의 특정 함수,클래스 등을 현재 네임스페이스에 등록 … Read more

카테고리 IT

[Python] 클래스

class mycls(object):        #() 안의 값은 상속받을 클래스     “mycls is tutorial class” #클래스 문서화 문자열     def __init__(self):        #클래스 초기화 함수         print “class initialize”     def func1(self,txt):    #class의 함수들의 첫번째 파라미터는 객체 자신을 가리키는 값 (통상적으로 self를 사용)       … Read more

카테고리 IT

[Python] 코루틴

def cor(txt):     print “coroutine start”     while True:         line = (yield)  #send로 전달받은 값을 line에 저장         if txt in line:             print line c=cor(“py”) #코루틴 초기화 c.next()    #코루틴 실행 c.send(“show me the money”) #코루틴에 값 전달 c.send(“snoopy”) c.send(“black … Read more

카테고리 IT

[Python] 예외처리

def trys(x,y):     try:         x / y         if y == 100:             raise ValueError     #강제로 예외 발생         elif y == 99:             raise NameError     #except에 명시적으로 정의되지 않은 … Read more

카테고리 IT

[Python] 생성기

def gene(n):     print “Generator start”     while n < 10:         yield n     #현재 n 값을 반환, 생성기 자체는 종료되지 않고 멈춰있음         n += 1     print “Generator end” for i in gene(1):    #순차적으로 생성기 호출 (next)     print i ... Read more

카테고리 IT

[Python] 파일 입출력

f=open(“C:\Windows\System32\drivers\etc\hosts”) #파일 객체 생성 f2=open(“out.log”,”w”) line=f.readline() #파일 read while line:     print line,    # ,는 줄바꿈 생략     #print(line,end=”) #파이썬3     print >>f2,line # f2에 line 내용 출력     #print(line,file=f2)  #파이썬3     line=f.readline() f.close() for line2 in open(“C:\Windows\System32\drivers\etc\hosts_tmp”):     print line2,

카테고리 IT

[Python] 함수

count=0 def func(a,b=1):    #두번째 인수에 기본값 설정     “func() is tutorial function” #함수 문서화 문자열     global count    #전역 변수를 함수 내에서 사용     count += 1     print “call count : %d” % count     print a,”+”,b     return a+b print func(4,5) print func(9) print func.__doc__

카테고리 IT

[Python] 집합 (데이터 순서 없음, 중복값 없음)

set1 = set([1,2,3,4]) set2 = set(“hello”)     #중복값은 제거됨 (helo) set1.add(5)     #값 1개 추가 set1.update([6,7,8])    #값 여러개 추가 set2.remove(‘e’)    #값 제거 set1 | set2     #합집합 set1 & set2     #교집합 set1 – set2     #차집합 set1 ^ set2     #대칭차집합 (교집합 반대)

카테고리 IT

[Python] 사전

dic1 = {“item1” : “123”, “item2” : 456, “item3” : “data”} dic2 = {}   #빈 사전 생성 dic3 = dict()   #빈 사전 생성 dic1[“item4”] = “data2” #키 추가 del dic1[“item3”]       #키 삭제 if “item2” in dic1:     #사전에 해당 키가 있는지 확인     i=dic1[“item2”] else:     i=0 j=dic1.get(“item2”,0) … Read more

카테고리 IT
바로가기