Skip to content

Instantly share code, notes, and snippets.

@nck2
Last active August 22, 2018 14:10
Show Gist options
  • Save nck2/410c8cc2e483d6cdf953c5bd9cb4fc70 to your computer and use it in GitHub Desktop.
Save nck2/410c8cc2e483d6cdf953c5bd9cb4fc70 to your computer and use it in GitHub Desktop.
파이썬으로 수학문제풀기(수학과 스터디)1강
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python으로 수학문제 해결하기_1강"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# python 추천 강좌 및 자료\n",
"- inflearn(https://www.inflearn.com/) - 강의 싸이트 (유,무료)\n",
"- AskDjango(https://nomade.kr) - 강의 싸이트 (유료)\n",
"- 프로그래머스 (https://programmers.co.kr/learn/) -강의 및 연습싸이트 \n",
"- jump to python (https://wikidocs.net/book/1) - 파일 자료"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# python으로 할 수 있는 것들"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 네이티브 앱 개발을 제외한 모든 영역이 가능\n",
"- 웹서비스 \n",
"- 수치연산 : Matplotlib,numpy,pandas\n",
"- Gui프로그래밍 : PyQt,Kivy,wxPython\n",
"- 각종 자동화 : MS 오피스 자동화 , 포토샵 자동화 등\n",
"- 기타"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 예시1- 각종수치연산 및 수학적 활용"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"http://nbviewer.jupyter.org/gist/nck2/8059f771316cb863717fbafbb45ad5ab\" target=\"_blank\"> 수학 문제 시뮬레이션</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"http://nbviewer.jupyter.org/gist/nck2/9512a9f8a0c2d32f6cda2d5dd36f8140\" target=\"_blank\"> 그래프와 리만합</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"http://nbviewer.jupyter.org/gist/nck2/8b943d0aa055eac115c575d581a0389e\" target=\"_blank\">선형대수학</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 예시2- 스크래핑(크롤링)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"학교급식 스크래핑\n",
"- 이를 이용하여 카카오톡에서 메뉴를 알려주는 서비스를 제공하고 있음. (플러스 친구 mathsshs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"학교 공지사항 목록 가지고 오기\n",
"- http://nbviewer.jupyter.org/gist/nck2/af75ae0138af097ba7ac694ba5407e77"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 예시3- 웹서비스 (mathsshs.net)\n",
"- 웹서비스의 백엔드언어로 python의 django를 활용하여 구성하였음. (django는 python으로 만들어진 풀스택 웹프레임워크임)\n",
"- 요즘 뜨고 있는(?) 매크로 프로그램"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 예시4 -기타\n",
"- 자료를 엑셀파일로 만들기, 엑셀파일을 조작하기, 엑셀파일을 읽어서 원하는 자료 만들기\n",
"- 학교홈피의 월간 자료(한글자료)를 엑셀로 복사하여 파이썬을 이용하여 원하는 자료를 가공하여 간식메뉴를 제공하고 있음.\n",
"- 각종 GUI 프로그램"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pycon 소개"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- https://www.pycon.kr/2018/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2020년, 만약 모든 과목 선생님들이 파이썬을 배운다면?\n",
"https://www.slideshare.net/SongSukree/2018-pyconkr-dive-into-2020-110359617"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 우리학교 교과서로 word cloud적용하면?\n",
"http://nbviewer.jupyter.org/gist/nck2/1bd6482b841c57b8153b088b9013aaa1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# python의 철학 (The Zen of Python)\n",
"- Beautiful is better than ugly. 아름다움이 추한 것보다 낫다.\n",
"- Explicit is better than implicit. 명확함이 함축된 것보다 낫다.\n",
"- Simple is better than complex. 단순함이 복잡한 것보다 낫다.\n",
"- Complex is better than complicated. 복잡함이 난해한 것보다 낫다. ...(생략)\n",
"- 실행명령 import this\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Zen of Python, by Tim Peters\n",
"\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n"
]
}
],
"source": [
"import this"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 입,출력"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 #대화형모드에서만 답을 내놓는다"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2\n"
]
}
],
"source": [
"print(1,2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n"
]
}
],
"source": [
"print(1)\n",
"print(2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"print(\"hello world\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"print('hello world')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name=\"홍길동\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"홍길동\n"
]
}
],
"source": [
"print(name)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"너의 이름은?강호동\n"
]
},
{
"data": {
"text/plain": [
"'강호동'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input(\"너의 이름은?\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"너의 이름은?유재석\n"
]
}
],
"source": [
"name = input(\"너의 이름은?\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"유재석\n"
]
}
],
"source": [
"print(name) # 변수 덮어써짐"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_int = 1\n",
"my_str = \"Python\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"count = 1"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"count = count + 1"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 변수(Variables)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Numeric Type(숫자)\n",
"- 정수형 : int , 실수형 : float\n",
"- 사칙연산(+,-,*,/) , 몫(//),나머지(%), 지수 승(**)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"print(1+5)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2*6"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.6666666666666666"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2/3"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10//3"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10%3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n"
]
}
],
"source": [
"print(2**3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Boolean Type"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"True, False"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ab = True"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cd = False"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ab, cd"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1<2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2>=3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 비교연산자 \n",
" - <, <=, >, >=, ==, !=\n",
"- 논리연산자\n",
" - or, and, not\n",
"- 기타\n",
" - 숫자0은 거짓, 그 외에는 참\n",
" - 빈문자열은 거짓, 그 외에는 참\n",
" - 빈 list/tuple/set/dict는 거짓, 그외에는 참"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(False, False)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2==3 , 1!=1"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, True)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(0),bool(1),bool(-5)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, True)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(''), bool(' '),bool('a')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(False, False, False, False, True)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool([]),bool(()),bool({}),bool(set()), bool([' '])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. String Type(문자열)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 문자열을 ''로 감싸거나, \"\"로 감싸면 된다."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name= \"python\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name_str= \"1\""
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name_int, name_flo=1,1.3"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(str, int, float)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(name_str),type(name_int), type(name_flo)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name_str2=\"\"\"\n",
"The now glows white on the mountain tonight\n",
"Now a footprint to be seen A kingdom of isolation\n",
"and it looks like I'm the queen\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'a' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-20-c5a4f3535135>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
]
}
],
"source": [
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"The now glows white on the mountain tonight\n",
"Now a footprint to be seen A kingdom of isolation\n",
"and it looks like I'm the queen\n",
"\n"
]
}
],
"source": [
"print(name_str2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 블록문\n",
"- python은 블록(indentation)문으로 코드를 구분함.\n",
"- space 4칸 혹은 탭을 일관성있게 사용하는 것을 권장.(둘을 혼용하여 사용하지 말것)\n",
"- 보통의 편집기에서는 자동으로 indentation을 실행시켜 줌"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (<ipython-input-21-8c08af4d1647>, line 2)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-21-8c08af4d1647>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m print(i)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
]
}
],
"source": [
"for i in range(10):\n",
"print(i)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"for i in range(10):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 주석(Comments)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 일주일만 지나면 내 코드가 아니다!! (주석으로 코드 설명이 필요함)\n",
"- '#' 한줄 주석 처리, python은 여러줄 주석처리 문법이 기본적으로 없다.!!"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 주석처리"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"'문자열도 주석처럼 쓰인다.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"''' 이렇게 여러 줄 문자를 주석처럼 쓰기도 한다. \n",
"하지만 주석이 아니기 때문에 특정한 상황에서는 오류를 일으킬 수도 있다.\n",
"이렇게 여러 줄 문자열을 주석으로 사용하는 경우는 함수에서 함수의 설명을 적을 때만 사용한다.\n",
"'''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 문자열 지정자 format"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"이름: 홍길동, 나이: 24\n"
]
}
],
"source": [
"print('이름: {}, 나이: {}'.format('홍길동','24')) #위치인자"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'이름:김구라, 나이:22'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'이름:{1}, 나이:{0}'.format('22','김구라') #0부터 시작"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'별명:강호동, 나이:40'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'별명:{name}, 나이:{age}'.format(name=\"강호동\",age=\"40\") # 키워드 인자"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 문자열 format 2"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"나는 3개의 사과가 있다.\n"
]
}
],
"source": [
"print( \"나는 %d개의 사과가 있다.\" % 3)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"나는 2개의 사과와 3개의 바나나가 있다. 나는 바나나를 좋아한다.\n"
]
}
],
"source": [
"print(\"나는 %d개의 사과와 %d개의 바나나가 있다. 나는 %s를 좋아한다.\" %(2,3,\"바나나\"))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.2346\n"
]
}
],
"source": [
"print(\"%0.4f\" % 3.234556)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 자료의 기본구조\n",
"- list,dict,tuple,set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### List\n",
"- 문법 : [],list()\n",
"- 순서를 고려하여 저장\n",
"- 여러줄로 나눠쓸 때는 마칠때 쉼표를 쓰는게 관례\n",
"- 색인 지원 : 0부터 1씩 증가 , 음수는 뒤에서부터 -1"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num=[1,2,3,4,5]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num2=[\n",
" 1,\n",
" 2,\n",
" 3\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(num[0])"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(num2[-2])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(3 in num)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len(num))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"for i in num3:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-35-559f4304845e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m#범위밖의 인덱스 참조할때 생기는 오류(IndexError)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"num[6] #범위밖의 인덱스 참조할때 생기는 오류(IndexError)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dict (사전)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- key와 value로 구성됨\n",
"- key의 중복을 허용하지 않음.\n",
"- :으로 key와 value를 구분\n",
"- {}로 생성"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dict_value={'blue':10,'yellow':10,'red':8}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-in 연산자로 key가 있는지 체크 가능"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print('blue' in dict_value)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print('black' in dict_value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 멤버함수\n",
"- .keys() : key 목록\n",
"- .values() : value 목록\n",
"- .items() : (key, value)목록"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['blue', 'yellow', 'red'])\n"
]
}
],
"source": [
"print(dict_value.keys())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_values([10, 10, 8])\n"
]
}
],
"source": [
"print(dict_value.values())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_items([('blue', 10), ('yellow', 10), ('red', 8)])\n"
]
}
],
"source": [
"print(dict_value.items())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### for 순회시 사용 예제"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"blue\n",
"yellow\n",
"red\n"
]
}
],
"source": [
"for key in dict_value:\n",
" print(key)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"10\n",
"8\n"
]
}
],
"source": [
"for value in dict_value.values():\n",
" print(value)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key= blue value= 10\n",
"key= yellow value= 10\n",
"key= red value= 8\n"
]
}
],
"source": [
"for key,value in dict_value.items():\n",
" print('key=',key,'value=',value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 생성 : (), tuple()\n",
"- list와 유사. 변경 불가능함"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num_tu=(1,3,5,7)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num_tu[0]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-57-1f1154c38c33>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum_tu\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"num_tu[0]=2"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-58-9661e4356dcd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum_tu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"num_tu.append(6)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x,y=1,2"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x,y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 생성 {},set()\n",
"- 중복을 제거한다.\n",
"- 순서를 고려하지 않는다.(유지하지 않는다.)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"set1={1,2,3,3,4}"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4}"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set1"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'set' object does not support indexing",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-67-1ba320498e76>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mset1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: 'set' object does not support indexing"
]
}
],
"source": [
"set1[0]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"list_num={1,2,3,3,4}"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"set_2= set(list_num)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4}"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set_2 #중복을 제거하는 용도로 많이 쓰임."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 변수형 변환"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 숫자를 문자로 : str\n",
"- 문자를 숫자로 : int, float\n",
"- 튜플로 : tuple()\n",
"- 리스트로 : list()\n",
"- set으로 : set()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(1) 숫자를 문자로"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(str(1))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int('23')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(int('34'))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"23.0"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float('23')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(float('23'))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 5]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list((1,3,5))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 3, 4}"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set((1,3,3,4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment