본문 바로가기

뇌새김질12

[Python] 복권번호 추출 import random data = list(range(1, 46)) print(data) for i in range(6): num = random.choice(data) data.remove(num) print(num) 2021. 7. 27.
[Python] Thread 컴퓨터에서 동작하고 있는 프로그램을 프로세스(Process)라고 한다. 보통 1개의 프로세스는 한 가지 일만 하지만 스레드(Thread)를 사용하면 한 프로세스 안에서 2가지 또는 그 이상의 일을 동시에 수행할 수 있다. 아래는 스레드를 수행하는 간단한 예이다. 수행 결과는 아래와 같다. join을 수행하지 않으면, "Start"와 "End"가 먼저 출력되고 그 이후에 스레드의 결과가 출력되며, 프로그램이 정상 종료되지 않는다. 2021. 7. 27.
[Python] 예외(Exception) 만들기 예외 클래스를 만들기 위해서는 Exception클래스를 상속받아야 한다. NewExcept 클래스 사용 make_except('except') 예외처리 오류메시지 출력 아무 결과도 출력되지 않음. 오류 메시지를 출력했을 때 오류 메시지가 보이게 하려면 오류 클래스에 다음과 같은 __str__ 메서드를 구현해야 한다. __str__ 메서드는 print(e)처럼 오류 메시지를 print문으로 출력할 경우에 호출되는 메서드이다. 에러메시지 정상 출력됨. 2021. 7. 26.
[Python] 리스트(List) vs 튜플(Tuple) and Dictionary 차이점. List Tuple [ ] ( ) 값을 생성, 삭제, 수정이 가능 값 수정 불가 [ ], [1], [1, 2, 3] ( ), (1, ), (1, 2, 3)... 1, 2, 3 (괄호생략가능) 이외 index, slice, 합치기 등은 모두 동일하게 동작한다. Dictionary key와 value의 쌍으로 이루어지며, value값은 고정 또는 변경할수 있으나, key값은 변하지 않으며, 중복될 경우 오류가 발생한다. key값으로 리스트는 쓸 수 없지만, 튜플은 사용가능하다. dic = { 'key':'value', 1:'val', 3:[1, 2, 3] } dic['key'] => 'value' dic[1] => 'val' 추가 : dic['add'] = 'new' => { 'key':'value.. 2021. 7. 21.
[Python] 파일을 모듈로 사용 시 주의할 점(if __name__ == '__main__') 모듈이란 함수나 변수 또는 클래스를 모아놓은 파일로 다른 프로그램에서 불러와 사용할수 있다. #cal.py def add(a, b): reutrn a + b def mul(a, b): return a * b # 데이터 검증용 함수 print(add(2, 4)) print(mul(2, 4)) 1. 직접 파일 실행 >>>python cal.py 6 8 2. 모듈로 사용 #math.py import cal >>>python math.py 6 8 print(add(2,4)), print(mul(2,4))함수가 자동 실행된다. 이처럼 테스트용 또는 검증용 함수실행을 방지하기 위해 if __name__ == '__main__': 을 사용한다. 1. 직접파일 실행시 if __name__ == '__main__':는 .. 2021. 7. 21.
[Python] bytes to String변환 bytes = b'its bytes' print(bytes) ==> b'its bytes' result = bytes.decode('utf-8') print(result) ==> its bytes result = str(bytes, 'utf-8') print(result) ==> its bytes 2021. 7. 19.
[Python] Method 종류 (instance, class, static) Python의 메서드는 Instance method, class method, static method가 있다. 1. 인스턴스 메서드(Instance Method) 인스턴스 변수 및 함수에 접근가능하도록 함수 첫번째 파라미터로 항상 "self"를 갖는다. 2. 클래서 메서드(Class Method) 메서드 앞에 @classmethod 라는 decorator로 표시하며, 첫번째 파라미터로 "cls"를 갖는다. 클래스 정의에서 메서드 밖에 존재하는 변수인 클래스 변수에 접근 가능 3. 정적 메서드(Static Method) self파라미터를 갖지않으며, 인스턴스 변수에 접근하지 못한다. 일반적으로 객체와 독립적이지만, 로직상 클래스에 필요한 경우 사용된다. class Video(): count = 0 # 클.. 2021. 7. 16.
[Python] String 포맷 & prefix (f, b, u, r) 1. f prefix : format _name = jack _msg = f'Hi {_name}' print(_msg) => Hi Jack 2. b prefix : bytes _str = 'this is string' _bytestr = b'this is byte string' print(_str) => this is string print(_bytestr) => b'this is byte string' 3. u prefix : unicode python 3 defaut encoding is unicode. don't need to use 'u' 4. r prefix : raw ignore escape character print('It is raw prefix.\nIs it right?') => It .. 2021. 7. 15.
[Python] VS Code로 빌드하기 1. 가상환경 (Virtual Environment) python -m venv .venv .venv\Scripts\activate =>아래 오류 발생시 아래 설정 수행 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process 그 후 다시 수행 .venv\Scripts\activate 2. Package 설치 python -m pip install package python -m pip uninstall package 3. 가상환경 종료 deactivate 2021. 6. 30.
[Flutter] 구글 광고(AdMob) 추가 오류 * 광고 배너 추가시 광고가 뜨지 않는 이유 1. 앱 ID 또는 광고 단위 ID 가 잘못된 경우 2. 프로젝트 설정 AndroidManifest.xml 파일에서의 설정이 잘못된 경우 3. AdMob계정 (AdMob.google.com) 정보 승인이 완료되지 않았을 경우 2021. 6. 25.