앎을 경계하기

파이썬 34

Python 함수, 변수 스코프, 글로벌 변수

함수 def sum(a, b): return a+b a = 3 b = 4 c = sum(a,b) print(c) 7 def say(): return 'Hi' say() 'Hi' def sum(a,b): print("%d"%(a+b)) sum(3,4) 7 print(sum(3,4)) # 반환값이 없어서 None이 나온다. 7 None 입력값이 몇 개일지 모르는 경우 def sum_many(*args): sum = 0 for i in args: sum = sum + i return sum result = sum_many(1,2,3) result 6 result = sum_many(1,2,3,4,5,6,7) result 28 함수의 결과값은 항상 하나 def sum_and_mul(a,b): return a+b..

Programming/Python 2019.12.26

Python 변수, 객체, 복사

# 파이썬에서 변수는 다음과 같이 사용할 수 있다. a = 1 b = 'python' c = [1,2,3] 변수 파이썬의 변수는 객체를 가리키는 것이다. 파이썬은 모든 것을 객체로 취급한다. type(3) # 3은 정수 상수가 아니라 정수형 객체이다. int a = 3 b = 3 a is b # a와 b가 동일한 객체를 가리키는가 True # 참조 개수 확인하기 import sys sys.getrefcount(3) 507 x = 3 sys.getrefcount(3) 503 y = 3 sys.getrefcount(3) 504 z = 3 sys.getrefcount(3) 505 #메모리에 생성된 변수 삭제하기 del(x) del(y) 복사 파이썬에서는 흔히 복사를 사용할 때 겪는 문제가 있다. a = [1,..

Programming/Python 2019.12.25

Python tuple, dictionary, set

Python 리스트, 튜플, 딕셔너리, 셋 list = [] tuple = () dict = {} set = set() Tuple은 리스트와 비슷하지만 값의 수정이 불가능하다는 차이점이 있다. t1 = () t1[0] = 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 1 t1 = () ----> 2 t1[0] = 1 TypeError: 'tuple' object does not support item assignment t1 = (1,2,3) t1[0] # list와 같이 인덱스로 접근 가능하다. 1 #괄호를 생략해도 튜플 사용할..

Programming/Python 2019.12.25

Python 리스트 관련 함수

리스트 관련 함수 append() - 추가 sort() - 정렬 reverse() - 뒤집기 index() - 위치 반환 insert() - 요소 삽입 remove() - 요소 삭제 pop() - 요소 꺼내기 count() - 요소 개수 세기 extend() - 리스트 확장 a = [1,2,3] a.append(4) a [1, 2, 3, 4] a.append([5,6]) a [1, 2, 3, 4, [5, 6]] a.append('sfsf') a [1, 2, 3, 4, [5, 6], 'sfsf'] a = [4,2,3,1] a.sort() a [1, 2, 3, 4] a.sort(reverse=True) a [4, 3, 2, 1] a = ['a','z','df'] a.sort() # 첫 알파벳 순으로 정렬 a..

Programming/Python 2019.12.25

Python 리스트 생성, 인덱싱, 슬라이싱, 수정, 삭제

리스트 표현방법 list = [요소1, 요소2, 요소3, ...] a = [] b = [1,2,3] c = ['life', 'is', 'too', 'short'] d = [1,2,'life','is'] e = list() a, b, c, d, e ([], [1, 2, 3], ['life', 'is', 'too', 'short'], [1, 2, 'life', 'is'], []) a == e # 빈 리스트는 [], list()로 생성할 수 있다. True 리스트 인덱싱, 슬라이싱 list는 문자열과 같이 인덱싱하고 슬라이싱할 수 있다. a = [1,2,3] a[0] 1 a[0]+a[2] 4 a[-1] 3 a = [1,2,3,['a','b','c']] a[0] 1 a[-1] ['a', 'b', 'c'] a[-..

Programming/Python 2019.12.25

Python 문자열 관련 함수

a = "hobby" a.count('b') 2 a = "Python is best choice" a.find('b') #있는 경우, 처음 나온 위치(인덱스) 반환 10 a.find('k') #없는 경우, -1 반환 -1 a = 'life is too short' a.index('t') #있는 경우, 처음 나온 위치(인덱스) 반환 8 a.index('k') #없는 경우, 에러 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 a.index('k') #없는 경우, 에러 ValueError: substring not foun..

Programming/Python 2019.12.25

<DAFIT> 09 OpenCV 연습하기(1) - 06 Edge Detection(1)

https://docs.opencv.org/3.4/d2/d2c/tutorial_sobel_derivatives.html OpenCV: Sobel Derivatives Prev Tutorial: Adding borders to your images Next Tutorial: Laplace Operator Goal In this tutorial you will learn how to: Use the OpenCV function Sobel() to calculate the derivatives from an image. Use the OpenCV function Scharr() to calculate a more accur docs.opencv.org 가장자리는 급격하게 값이 변하는 곳으로 표현할 수 있다. ..