Programming Language/Python28

서식 및 포맷 1. 서식 및 포맷 1) 기본포맷형식 정수형 %d 실수형 %f 문자 %c 문자열 %s 2) 이스케이프 \n Enter \t Tab \b Backspace \\ \ 출력 \" " 출력 \' ' 출력 \ \하나만 쓸 경우 다음줄까지 이어서 인식 [소스] 예제1) - 서식사용은 c언어와 동일, 뒤에 %기호를 사용print '%d' %10 print '%3d' %5 #3칸 띄우고 5입력 print '%03d' %5 #3칸 확보 후 0으로 패딩, 그리고 5입력 print '%f' %3.14 #기본 소수 여섯째자리까지 출력 print '%.2f' %3.14 #소수 둘째자리까지 출력 print '%c' %'c' #문자 하나 출력 print '%s' %'string' #문자열 출력 [실행결과] 10 5 005 3.14.. 2018. 8. 10.
이름 1. 이름 1) 예약되어 있는 변수 - 이 전에 globals() 함수를 출력하면 {... '__name__': '__main__', '__doc__': None ... } 의 내용이 들어 있음을 알 수 있다. - 두 언더스코어 __ 로 시작하고 끝나는 이름은 파이썬 내에서 사용하기 위해 예약되어 있는 변수이다. - 위에 적은 '__name__' 은 시스템 변수인 'function.__name__ ' 에 존재하며, '__doc__' 는 함수의 docstring은 'function.__doc__ '에 존재한다.def test() : print test.__name__ print test.__doc__ test() [실행결과] test None 2018. 8. 10.
전역변수와 지역변수 1. 네임스페이스 1) 전역변수와 지역변수 차이animal = 'cat' def change_local() : animal = 'dog' print animal, id(animal) change_local() print animal, id(animal) + 파이썬 함수 id()는 변수가 같거나 다름을 증명하기 위해 사용. [실행결과] dog 91508648 cat 73809792 2) 함수 내에서 전역변수 설정animal = 'cat' def change_local() : global animal animal = 'dog' print animal, id(animal) change_local() print animal, id(animal) [실행결과] dog 102059944 dog 102059944 + .. 2018. 8. 10.
에러처리 1. 에러처리 [try - except] 1) 잠재적인 에러를 방지하기 위해 사용short_list = [ data for data in range(10,1,-2)] value = 10 try : #에러가 발생하지 않으면? print short_list[value] except : #에러가 발생하면? print "Need a position between 0 and", len(short_list)-1, 'but value is', value [실행결과] >>>Need a position between 0 and 4 but value is 10 2018. 8. 9.
위치인자 1-1. 위치인자def menu(wine, entree, dessert) : return {'wine' : wine, 'entree' : entree, 'dessert' : dessert} test = menu("white", "beef", "vegetable") print test.keys() print test.values() print test.items() [실행결과] ['dessert', 'entree', 'wine'] ['vegetable', 'beef', 'white'] [('dessert', 'vegetable'), ('entree', 'beef'), ('wine', 'white')] 1-2. 키워드인자def menu(wine, entree, dessert) : return {'wine' .. 2018. 7. 29.
함수 1-1. 기본매개변수 지정def addition(arg, result=[]) : result.append(arg) return result print addition('1') print addition('2') [실행결과] ['1'] ['1', '2'] 1-2. 기본매개변수 지정def addition(arg) : result = [] result.append(arg) return result print addition('1') print addition('2') [실행결과] ['1'] ['2'] 1-3. 기본매개변수 지정def addition(arg, result = None) : if result is None : result = [] result.append(arg) return result prin.. 2018. 7. 29.
컴프리헨션 1. 딕셔너리 컴프리헨션 (1) { 'word의 단어 중 1개' : 그 단어의 개수 } for 문으로 순회word = "standar" word_cnt = {data:word.count(data) for data in word} print word_cnt {'a': 2, 'd': 1, 'n': 1, 's': 1, 'r': 1, 't': 1} 2. 셋 컴프리헨션 (1) { 'word의 단어 중 1개' : 그 단어의 개수 } for 문으로 순회 test_set = {number for number in range(1,8,1) if number % 3 == 1} print test_set set([1, 4, 7]) 2018. 7. 26.
for문 + break 1. for문에 들어가지 않을 시 else : 실행 values= [1] for a in values : print 'data' break else : print 'non data' data 2. 여러 시퀀스 순회 : zipeng = ('monday', 'tuesday', 'wedesday') fre = ('lundi', 'mardi', 'mercredi') test_list = list(zip(eng,fre)) print test_list test_dic = dict(zip(eng,fre)) print test_dic [('monday', 'lundi'), ('tuesday', 'mardi'), ('wedesday', 'mercredi')] {'tuesday': 'mardi', 'wedesday': '.. 2018. 7. 25.
if, elif, else 문 info = "name"if info == "name" : print "d4tai1"elif info == "weather" : print "cloud"else: print "false" 2018. 7. 25.
형변환 score = 99.8 print float(score) 99.8 ---------------------------------------------- print int(-100) -100 ---------------------------------------------- info = 'result = ' data = -1000 print info + str(data) result = -1000 2018. 7. 23.
문자열 길이 : len() len() 함수는 문자열 길이를 알려주는 함수 ex) weather = 'sunny' print len(weather) 5 2018. 7. 23.
문자열 slice 슬라이스 : [start:end:step] (1) start = 시작 offset (2) end = 끝 offset (3) step = 옵션 [:] = 처음부터 끝까지 전체 sequence를 출력 [start:] = 시작 offset부터 끝까지 sequence를 출력 [:end] = 처음부터 [end-1] offset까지 sequence를 출력 [start:end] = 시작 offset부터 [end-1] offset까지 sequence를 출력 [start:end:step] = step만큼 문자를 건너뛰면서, 시작 offset부터 [end-1] offset까지 sequence를 출력 ex) >>>data='abcdefghijkl' >>>data[::2] 'acegik' 2018. 7. 23.