고급생물정보학(python) - kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf ·...

109
고급 생물 정보학(Python) 컴퓨터과학과 인텔리전트소프트웨어 연구실 배장성

Upload: others

Post on 13-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

고급 생물 정보학(Python)

컴퓨터과학과인텔리전트소프트웨어 연구실

배장성

Page 2: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 컴퓨터: 하드웨어 + 소프트웨어 (프로그램)

• 하드웨어: CPU, RAM(주기억장치), Hard DISK, …

• 소프트웨어

• OS(운영체제)

• Excel, power point, kakao talk, overwatch, etc,

• Your program also software

프로그램 개념

Page 3: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 프로그래밍 언어

• 동작시키는 프로그램을 작성하기 위한 인공적인 언어

• 프로그래밍 언어의 분류

• 사용하기 편리한 정도에 따라 분류

프로그래밍 언어

Page 4: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 컴퓨터가 이해할 수 있는 언어

• 0과 1로 구성되어 있는 언어 (이진수)

• 0101011100011 0101011111111

• 컴퓨터는 모든 것을 0과 1로 표현하고 0과 1에 의하여 내부 회로가 동작

기계어

Page 5: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 컴파일 기법: C, C++ 등

프로그램 처리 기법

Page 6: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 인터프리터(해석) 기법: python, perl 등

• 고급 언어로 작성된 프로그램을 바로 실행• 해석하는 프로그램: 인터프리터

• 인터프리터: 기계어로 번역된 코드를 시뮬레이션

프로그램 처리 기법

Page 7: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 컴파일 기법과, 인터프리터 기법의 장 단점

• 컴파일러

• 매번 번역할 필요는 없다

• 인터프리터에 비해 실행 속도가 빠름

• 인터프리터

• 실행 속도가 느림

• 줄 단위로 번역 수행 (코드 수정이 용이)

프로그램 처리 기법

Page 8: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

WHY Python?

Page 9: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

Python Install

• www.python.org

• Downloads > windows

Page 10: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

Python 설치

Page 11: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

Python 설치

Page 12: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 1990년 암스테르담의 귀도 반 로섬(Guido Van Rossum)에 의해 만들어진 인터프리터 언어

• 사전적인 듯은 고대 신화 속의 파르나수스 산의동굴에 살던 큰 뱀으로, 아폴로가 겔파이에서 파이썬을 퇴치했다는 이야기에서 비롯

• 주로 사용되는 곳

•구글에서만들어진소프트웨어의 50% 이상이파이썬으로 만들어졌다고 함

• 유명한 파일 동기화 서비스인 Dropbox 또한파이썬으로 만들어짐

파이썬(python) 이란?

Page 13: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 문법이 쉬워 빠르게 학습할 수 있다

• 파이썬을 공부한지단하루만에자신이원하는프로그램을작성할 수 있었다고 함 (프로그래밍 경험이 있었음)

• 간결하다

• 강력하다

• 대부분의 모든 일을 할 수 있음: But:시스템 프로그래밍, 하드웨어 제어, 복잡하고 많은 반복연산에는 어울리지 않음

파이썬 특징

Page 14: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 무료이다 (오픈소스)

• 개발 속도가 빠르다

• C, C++, Java 등으로한달이상걸릴것을 1주일안에개발가능

• python을 이용하여 빠르게 프로토타입(prototype) 시스템을 구축 후, 시스템이 검증되면 이를 C, C++ 등으로 구현

• 결과적으로 전체 개발 시간을 단축 시킴

파이썬 특징

Page 15: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 대단히 빠른 속도를 요구하거나 하드웨어를 직접 건드려야 하는 프로그램을 제외한 모든 일

• 할 수 있는 것들

• 시스템 유틸리티: 운영체제 시스템 명령어 지원

• 웹 프로그래밍: 많은 라이브러리가 있음(Django)

• C/C++과의 결합

• 수치연산 프로그래밍:Numpy

• 데이터베이스 프로그래밍

• …

파이썬으로 할 수 있는 것

Page 16: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 자료형 이란 프로그래밍을 할 때 쓰이는 숫자, 문자열 등 자료 형태로 저장되는 그 모든 것(값을 저장하는 방식을 결정)

• python 기본 자료형

• Number, String, List, Tuple, Dictionary

python 자료형

Page 17: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Number

항목 Example

정수 123, -345, 0

실수 123.45, -1234.5, 3.4e10

복소수 1 + 2j, -3j

8진수 034, 025

16진수 0x2A, 0xFF

Page 18: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Number - cont’d

Page 19: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• Complex number

python 자료형: Number - cont’d

Page 20: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 숫자 연산

python 자료형: Number - cont’d

소수점 형태의 결과를 얻으려면 피연산자중 한가지라도 실수형으로 써 주어야 한다.

위와 같은 방법을 ‘형변환’ 이라고 한다.

Page 21: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

• 여러 개의 문자 > 문자열/문장

• Single quotation(‘) 혹은double quotation(“)을 사용하여 데이터를 저장

• 여러 문장의 문자열 (“““ ~ ”””) 및 (‘‘‘~’’’) 사용

python 자료형: String

Page 22: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 여러 문자열 표현 방법

Page 23: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 문자열에 single quotation을 포함시킬 경우

• \(역슬래시)를 이용해 quotation mark 삽입 가능

• double quotation을 이용하여 single quotation 포함가능

a = “I don’t agree with you”

Page 24: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 문자열이 여러 줄일 경우Life is too shortYou need python

• 줄 바꿈 문자인 \n 삽입

Page 25: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 연속된 3개의 double quotation 사용

Page 26: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 특수 문자 (Escape code)

Page 27: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 문자열 연산

• 문자열 합치기

• 문자열 곱하기

Page 28: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 문자열 연산

• 문자열 곱하기

Page 29: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 문자열 연산

• 인덱싱(indexing), 슬라이싱(slicing)

• python index는 0부터 시작

• a = “PYTHON”

Page 30: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 인덱싱

Page 31: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 인덱싱

Page 32: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 슬라이싱

• slice: 한 조각, 자르다, 썰다

• 문자열을 잘라내는 것

Page 33: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 슬라이싱

• a[0:4]

• 수식으로 변환

• 0 <= a < 4

• a[0], a[1], a[2], a[3]

Page 34: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• 슬라이싱

Page 35: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

Page 36: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String formatting

• 문자열 내에 어떤 값을 삽입하는 방법

• 현재 온도는 14도 입니다.

• 현재 시간은 1시 10분 입니다.

• 시간이 지나고 온도가 바뀐다면 위의 문자열도 같이 수정해야 함 >> 문자열 내 특정한 값을 변화 시킬 필요성 >> string formatting

Page 37: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String formatting

• 변수 대입도 가능: 변수(추 후 설명, 어떠한 값)

Page 38: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String formatting

Page 39: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

Page 40: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String function

Page 41: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String function

Page 42: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String function

Page 43: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String function

Page 44: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: String - cont’d

• String function

Page 45: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List

• 리스트 (C언어의 배열과 유사함)

• 리스트를 만들 때는 대괄호를 이용

• a = []

• b = [1, 2, 3]

• c = [“my”, “life”, “for”, “aiur”]

• d = [1, 2, “my”, “life”]

• e = [1, 2, [“my”, “life”]]

• 리스트는 어떠한 자료형도 포함할 수 있다

Page 46: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• 리스트 인덱싱

life 에 대해 인덱싱 해보세요

Page 47: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• 리스트 슬라이싱

Page 48: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• 리스트 연산

Page 49: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• a[0], a[0:1] 의 차이점 // 인덱싱과 슬라이싱의 차이점

Page 50: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• List function

Page 51: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• List function

Page 52: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: List - cont’d

• List function

Page 53: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Tuple

• 몇 가지 점을 제외하면 리스트와 동일함

• 다른점:

• 리스트와 달리 소괄호를 이용하여 정의

• 리스트는 값의 수정, 삭제가 가능했지만,튜플은 값을 변화시킬 수 없음

• t1 = ()

• t2 = (1,)

• t3 = (1, 2, 3)

• t4 = (‘a’, ‘b’, (1,2,3))

Page 54: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Tuple - cont’d

• 프로그램이 진행되는 동안 여러 데이터를 상수(변하지 않는 값) 처럼 사용하기 위해 사용함

• days = (“SUN”, “MUN”, “TUE”, … ,”SAT”)

• 리스트와 마찬가지로 인덱싱, 슬라이싱 가능

• 튜플의 요소를 삭제하려 할 때 에러 발생

Page 55: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary

• 이름 = 박신혜, 생일 = 1990년 2월 18일

• 위와 같은 대응 관계를 나타내는 자료형을Dictionary 라고 부름

• 사전과 같이 Key: Value 형태로 이루어진 한 쌍의자료형

• 인덱스를 통해 각 요소의 값을 구하는 리스트나튜플과 달리 Key 검색을 통해 값(value)을 구함

Page 56: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• {key1: value1, key2: value2, …}

• key와 value 쌍 들이 중괄호로 둘러싸여 있음

• key에는 변하지 않는 값을 사용하고,value에는 변하는 값과 변하지 않는 값 모두 사용할 수 있음

• dic={‘name’: ‘ python’, ‘category’: ‘ program language’, ‘level’:4}

Page 57: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 쌍 추가

• {1: 'a'}라는 딕셔너리에 a[2] = 'b'와 같이 입력하면 딕셔너리 a에 Key와 Value가 각각 2와'b'인 2 : 'b'라는 딕셔너리 쌍이 추가된다

Page 58: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 a에 'name': 'pey'라는 쌍이 추가되었다

• Key는 3, Value는 [1, 2, 3]을 가지는 한 쌍이 또 추가되었다

Page 59: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 요소 삭제

• 위의 예제는 딕셔너리 요소를 지우는 방법을 보여준다. del 함수를 사용해서 del a[key]처럼 입력하면 지정한 key에 해당하는 {key : value} 쌍이삭제된다

Page 60: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• Key를 이용하여 value 얻기

• 어떤 Key의 Value를 얻기 위해서는 "딕셔너리 변수[Key]"를 사용한다

Page 61: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 주의 사항

• key는 고유한 값이므로 중복되는 값을 사용할 경우 중복되는 것들이 무시된다

• key에는 리스트 및 변하는 값을 사용할 수 없다

Page 62: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 함수

함수 정의

a.keys() 딕셔너리 의 key들을 모아놓은 리스트를 반환

a.values() 딕셔너리 의 value들을 모아놓은 리스트를 반환

a.items() 딕셔너리 의 Key, value 쌍의 튜플을 모아놓은 리스트를 반환

a.clear() 딕셔너리의 모든 Key,value 쌍을 삭제

a.get(x) 딕셔너리의 Key가x인 것의 value를 반환

a.has_key(x) 딕셔너리에 x라는 key가 있는지 조사하여, 참, 거짓을 반환

Page 63: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 함수 예제

Page 64: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 함수 예제

Page 65: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Dictionary- cont’d

• 딕셔너리 함수 예제

Page 66: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 자료형: Boolean

• 참과 거짓 (True, False)

자료형 True / False

“Python” True

“” False

[1,2,3] True

[] False

() False

{} False

1 True

0 False

None False

Page 67: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수(Variable)

• 오른쪽 예제와 같이 a, b, c를‘파이썬 변수’라 한다

• 변수를 만들 때는 오른쪽과같이 ‘=‘(assignment)기호를 사용한다

• 객체(object)를 가리키는 것으로, 객체는 우리가지금껏 보아왔던 자료형을 포함한 파이썬에서사용되는 모든 것

Page 68: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• 변수 a와 b가 서로 3이란 정수형 객체의 메모리위치를 가리키고 있다. 즉, a == b 명령문을 실행한다면, 아래의 예제와 같이 True(참)를 반환(return)하게 된다

Page 69: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• 변수를 만드는 여러 방법

Page 70: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• immutable

Page 71: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• immutable

Page 72: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• immutable

Page 73: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• mutable

Page 74: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• mutable

Page 75: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• mutable > immutable

Page 76: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 변수

• mutable > immutable

Page 77: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문

• 제어문을 사용의 예

• 조건에 따른 분기

• 100점이면 A+, 100미만 90이상이면 A, 그 이하면 B

• y=[x], 0<= x <1 이면 y=0, 1<= x <2 면 y=1, …

• 반복적인 작업

• 반복되는 연산

• 수천, 수만 줄의 파일(정보) 읽기

Page 78: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문

Page 79: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 반복문

Page 80: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if

• if문 기본 구조

Page 81: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• if문 기본 구조 – 에러 예시

Page 82: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 조건문은 True(참) / False(거짓)을 판단

Page 83: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 조건문: 비교 연산자

• 조건 판단에 가장 많이 쓰임, 논리적으로 이해하기 쉽다, 이전 페이지의 자료형 보다 쓰이는 경우가 훨씬 많다

Page 84: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python .py 파일 생성 (in linux)

• .py 파일 생성

• .py는 파이썬 확장자

• 편집기를 이용하여 .py 파일 생성

• 원하는 파일 이름.py

• e.g. example1.py

• 소스코드 입력 후 저장

• 현재 작성한 코드를 실행 하기 위해python 명령어를 이용e.g. python example.py

Page 85: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python .py 파일 생성 (in windows)

• Python (shell) 에서file > new file (ctrl + N)메모장과 비슷한 창이 하나 생김소스코드 입력 후 저장(저장 위치는 본인이알아서..).py는 파이썬 확장자 임현재 작성한 코드 실행 = F5키를 누름

Page 86: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 조건문: 비교 연산자

• 3000원 이상 돈이 있다면 택시를 타고 그렇지 않으면 걸어가라

Page 87: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 조건문: 논리 연산자, (and, or, not)

Page 88: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 조건문: 논리 연산자, (and, or, not)

• 돈이 3000원 이상 있거나, 풀러 줄 시계가 있다면택시를 타고 그렇지 않으면 걸어가라

Page 89: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• x in s , x not in s

Page 90: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• x in s , x not in s

Page 91: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• x in s , x not in s

Page 92: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• 만약 주머니에 돈이 있으면 택시를 타고 없으면걸어가라

Page 93: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if-elif – cont’d

• 만약 주머니에 돈이 있으면 택시를 타고 없으면걸어가라

Page 94: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if-elif – cont’d

• 지갑에 돈이 있으면 택시를 타고, 지갑엔 돈이 없지만 시계가 있으면 택시를 타고, 돈도 시계도 없으면 걸어가라

Page 95: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: if – cont’d

• if문: pass

• 참 거짓에 따른 행동을 정의 할 때 아무런 일도하지 않게끔 설정을 하고 싶을 때 사용

• 지갑에 돈이 있으면 가만히 있고, 지갑에 돈이 업으면 시계를 끌러라

Page 96: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: while

• while (반복문) 기본 구조

Page 97: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: while – cont’d

• while문 빠져 나가기: break

• 조건에 맞지 않는 경우 while문을 빠져 나간다

Page 98: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: while – cont’d

• while문 빠져 나가기: continue

• 조건에 맞지 않는 경우 while문의 맨 처음(조건문)으로 돌아가게 하고 싶을 경우 사용

Page 99: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: while – cont’d

• while문 무한 루프

Page 100: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for

• 리스트의 첫 번째 요소부터 마지막 요소까지 차례로 변수에 대입해서 <수행할 문장1>, <수행할문장2>,…를 수행

Page 101: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• 총 5명의 학생이 시험을 보았는데 시험점수가 60점이 넘으면 합격이고 그렇지 않으면 불합격이다,각 학생마다 번호를 부여하고, 합격인지 불합격인지 보여준다

Page 102: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• 앞의 예제에서, 합격한 사람에게 축하 메시지를보내고 나머지 사람에게는 아무런 메시지도 전하지 마시오

Page 103: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• range 함수

• 숫자 리스트를 자동으로 만들어주는 함수

• 시작과 끝을 정해줄 수 있다

• range(숫자), 숫자 만큼의 리스트 생성, 0부터 1씩증가하며 순차적으로 리스트를 채움

• range(시작, 끝)

Page 104: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• range 함수 예제

• i에 range 함수로 생성한 리스트[1, 2, 3, …, 10] 값이 한번씩 차례대로 대입 됨리스트의 끝에 도달하면 for문을 나감

Page 105: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• for문과 range 함수를 이용하여 구구단 출력하기

Page 106: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

• for문과 딕셔너리

Page 107: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

python 제어문: for – cont’d

Page 108: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

실습

• 2000보다 작은 자연수 중에서 3 또는 5의 배수를모두 더한 값을 구하시오

• 1부터 100까지 “합의 제곱” 과 “제곱의 합” 의 차를 구하시오

Page 109: 고급생물정보학(Python) - Kangwoncs.kangwon.ac.kr/~bjs/bioinformatics_2017/01.pdf · 2017-04-26 · python .py 파일생성(in windows) • Python (shell) 에서 file > new

과제

• 세 자연수 a, b, c가 피타고라스 정리a^2 + b^2 = c^2를 만족하고,a < b < c 이다

이 때 a + b + c =1000인 a, b, c를 구해서a * b + c를 구하시오

• 소수점 뒤에 양의 정수를 차례대로 붙여 나가면 아래와 같은 무리수를 만들 수 있습니다.

0.123456789101112131415161718192021...

이 무리수의 소수점 아래 12번째 자리에는 1이 옵니다 (위에서 붉게표시된 숫자). 소수점 아래 n번째 숫자를 dn이라고 했을 때, 아래 식의 값은 얼마입니까?

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000