목록2022/01 (70)
Developer_Neo
함수 정의 def 함수명(매개변수): print("함수의 정의에 해당") print("필요한 것들을 적으면 된다.") return 반환값 함수를 정의하기 전에 함수호출에 관한것을 적으면 컴퓨터가 에러를 낸다. print('before hello') hello() # ->에러 발생 print('after hello') def hello(): print("Hi") print("Nice to meet you") main문 #main 또는 if__name__=='__main__': 으로 나타낸다. mutable인 리스트, 집합, 사전이 인수로 넘어가는 경우, main과 함수가 객체를 공유함 함수의 반환값에서의 * 사용 *를 붙이면 나머지 모든 값을 의미하고 리스트 형으로 처리 ‘=’ 왼쪽에 * 표현이 두 개 올..
주의점 ‘키’는 유일해야 함 (중복되면 안된다.) immutable 자료형(정수, 실수, 복소수, 부울, 튜플, 문자열)만 ‘키’로 사용 수 있음 리스트 집합 사전 불가 값인 value는 모든 자료형이 될 수 있다. 사전은 집합과 함께 +, * 지원안됨 =’으로는 복사본이 생기지 않음(리스트, 튜플, 문자열, 집합) 사전으로 변환 크기가 2인 튜플 또는 리스트로 구성된 리스트는 사전으로 변환 가능 튜플과 리스트는 '키' 와 '값'으로 사용할 데이터로 구성되어야함. >>> T = [('name', 'Alice'), ('age', 10), ('grade', 3)] >>> D1 = dict(T) >>> print(D1) {'name': 'Alice', 'age': 10, 'grade': 3} >>> area_..
얕은복사 mutable 자료형(리스트, 집합, 사전)의 copy() 메소드 copy모듈의 copy() 함수 깊은복사 copy모듈의 deepcopy() 함수 - 데이터 자체를 통째로 복사한다 - 복사된 두 객체는 완전히 독립적인 메모리를 차지한다. 얕은복사 -> copy함수를 안쓰고 = 기호를 썼을 때 , [:]슬라이싱 이용 >>> L = [3, 7, 20, 5] >>> M = L # L, M은 객체를 공유 >>> id(L), id(M) (1306075477120, 1306075477120) >>> M[2] = 100 >>> print(L, M) [3, 7, 100, 5] [3, 7, 100, 5] >>> M=L[:] >>> id(L), id(M) (1306075477120, 1306075577728) #..
while while True/False를 판단할 수 있는 조건문장 : ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ else: ~~~~~~~~~~~~~~ 무한 루프는 while True로 작성하고 break가 꼭 있어야 한다 이때에 continue들어가도 된다. break문을 거치면 else문도 실행하지 않고 넘어간다. i = 1 while i
중복된 원소가 없음 집합 내의 원소 간에는 순서 개념이 없음 -> print로 출력시에는 오름차순으로 출력이 된다. 하지만 실제로는 순서가 없다. 집합에는 immutable 객체들만 저장되어야 함 (리스트, 집합, 사전은 넣을 수 없음) set(iterable객체) 함수 사용가능 집합은 인덱스 개념이 없어서 메소드를 이용해서 원소를 추가 삭제 해야한다. 연산자 in, not in del 함수 len(), max(), min(), sum(), sorted() reversed()는 사용불가 메소드 add(x) 집합에 데이터 x를 추가 하지만 x가 이미 있다면 추가 X 반환값 None clear() 집합을 공집합으로 만듬 반환값 None copy() 집합을 복사해 새로운 집합 반환 discard(x) 데이터 ..
튜플 안에는 9가지 자료형을 모두 저장가능 tuple(iterable) 함수 사용 튜플은 immutable 튜플에 mutable 객체인 리스트, 집합, 사전도 저장 가능 연산자 + * in, not in del >>> T = (7, 8, 2, 5, 4) >>> S = (6,9) >>> T + S (7, 8, 2, 5, 4, 6, 9) >>> S * 5 (6, 9, 6, 9, 6, 9, 6, 9, 6, 9) >>> 4 in T True >>> 10 not in S True 함수 len(), max(), min(), sum(), sorted(), reversed() sum(?)은 튜플의 데이터가 숫자만 있을 때 가능. 메소드 >>> dir(tuple) ['__add__', '__class__', '__cla..
리스트 만들기 리스트 객체 = list( iterable 자료형 ) iterable 자료형 컨테이너 자료형 - 문자열, 튜플, 집합, 사전 내장함수 - range(), reversed(), enumerate(), filter(), map(), zip() 리스트는 mutable객체 연산자 + * in, not in del ''' + ''' >>> L = [1, 3, 5, 7]; M = [2, 4, 6, 8] >>> L + M [1, 3, 5, 7, 2, 4, 6, 8] >>> L, M ([1, 3, 5, 7], [2, 4, 6, 8]) >>> K = L + M >>> K [1, 3, 5, 7, 2, 4, 6, 8] ''' * ''' >>> M = [7, 9] >>> M *= 3 >>> M [7, 9, 7,..
문자열 연산자 + * in, not in del + 문자열은 immutable자료형으로 수정이 불가하다 ex) s[2]='b' - (X) ''' + ''' >>> first_name = 'Hyunho' >>> last_name = 'Jo' >>> full_name = first_name + ' ' + last_name >>> full_name 'Hyunho Jo' ''' * '''' >>> a = 'Hello' >>> a * 3 'HelloHelloHello' >>> greeting 'Hello' >>> a = 'Hello' >>> a *= 3 >>> a # a=a*3 'HelloHelloHello' ''' in, not in ''' >>> words = 'abcdef' >>> 'b' in words T..