목록2022/01/16 (15)
Developer_Neo
파일 열기 - open()함수 파일 객체 = open(파일명, 모드) 파일 열기가 성공하면 파일객체가 하나 생성된다. open() 함수의 반환값은 파일 객체임 그리고 이 파일 객체는 iterable하다 모드 읽기 모드 r 파일로부터 데이터를 읽어오기 위해 사용하는 모드 쓰기 모드 w 파일에 데이터를 저장하기 위해 사용하는 모드 없는 파일명 넣으면 새로 파일 생성 이미 존재하는 파일명 넣으면 기존의 파일 내용 모두지우고 새로운 데이터 저장 추가 모드 a 파일에 데이터를 추가하기 위해 사용하는 모드 이미 존재하는 파일 넣으면 기존내용그대로 두고 파일의 끝에 새로운 내용 추가 존재하지 않는 파일 넣으면 새로운 파일 생성후 내용 저장 ''' movie.txt Alice in Wonderland is a fant..
람다함수 - 이름 없는 함수 - 함수의 인수로 함수를 넣어야 하는 경우에 유용 형식 lambda 인수들 : 반환할 식 def add(x,y) return x+y #람다식 lambda x,y : x+y >>> f = lambda x,y: x+y >>> z = f(5,7) >>> print(z) 12 파이썬 내장 함수 자료형 변환 함수 - 각 자료형마다 같은 이름으로 함수가 존재 - int(), float(), complex(), bool(), str(), list(), tuple(), set(), dict() 수학관련함수 - abs(), divmod(x,y), pow(x,y), round(x), bin(x), oct(x), hex(x) iter(x) 함수 - x에 넣는 객체가 iterable 객체인지 판단..
함수 정의 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..