{ "cells": [ { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "colors = list()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "colors.append('red')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red']" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colors\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "colors.append('blue')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'blue']" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colors" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "colors.append('yellow')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'blue', 'yellow']" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colors" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "colors.append('red')" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'blue', 'yellow', 'red']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colors\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "colors.remove('red')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'yellow', 'red']" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colors" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "a = [1,5,7,3,9]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 5, 7, 3, 9]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sort()\n", "a" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 7, 5, 3, 1]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sort(reverse=True)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> sort() 와 sorted() 차이점은 반환값이 있냐 없냐, " ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "f = ['babaab', 'apple', 'orange']\n", "f1 = f.sort()\n", "print(f1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'babaab', 'orange']\n" ] } ], "source": [ "f = ['babaab', 'apple', 'orange']\n", "f1 = sorted(f)\n", "print(f1)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "56" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [12,4,6,34,56]\n", "max(a)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(a)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number : 4\n", "Enter a number : 2\n", "Enter a number : 5\n", "Enter a number : 6\n", "Enter a number : done\n", "[4.0, 2.0, 5.0, 6.0]\n", "Maximum: 4.25\n", "Maximum: 6.0\n", "Minimum: 2.0\n" ] } ], "source": [ "# number = int(input(\"Enter a number : \"))\n", "l = list()\n", "while 1:\n", " a = input('Enter a number : ')\n", " if( a == 'done'):\n", " break\n", " else:\n", " l.append(float(a))\n", " \n", "\n", "# nl = list(map(float,l))\n", "print(l)\n", "print('Maximum: {}'.format(sum(l)/len(l)))\n", "print('Maximum: {}'.format(max(l)))\n", "print('Minimum: {}'.format(min(l)))\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Tuple(튜플)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Tuple Packing 관련함수는 index(), count() 두개이다" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 집합(set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> 순서 없이 항목을 저장, 항목이 중복 될 수 없음\n", ">> add, remove , discard, update, clear" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set()" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set()\n", "s" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "s1 = set([1,2,3])\n", "s2 = {3,4,5}" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4, 5}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - 중괄호로{} 묶여있으며 키와 벨류의 쌍으로 이뤄짐\n", " - key 값은 동일한거 없음 중복 없음\n", " - 항목들 사이에 순서가 없음" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "10\n" ] } ], "source": [ "d = {'yoon':1, 'park':2, 'kim': 10}\n", "\n", "for x in d.values():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "10\n" ] } ], "source": [ "d = {'yoon':1, 'park':2, 'kim': 10}\n", "\n", "for key in d:\n", " print(d[key])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "10\n" ] } ], "source": [ "for key, value in d.items():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a sentence : Python is fun? really? yes!!\n", "{'P': 1, 'y': 3, 't': 1, 'h': 1, 'o': 1, 'n': 2, ' ': 4, 'i': 1, 's': 2, 'f': 1, 'u': 1, '?': 2, 'r': 1, 'e': 2, 'a': 1, 'l': 2, '!': 2}\n" ] } ], "source": [ "word = input('Enter a sentence : ')\n", "\n", "word_list = list(word)\n", "word_list\n", "d = {}\n", "count = 0\n", "\n", "for x in word_list:\n", " if x in d:\n", " d[x] += 1\n", " else:\n", " d[x] = 1\n", "print(d)\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'h': 2, 'e': 2, 'l': 4, 'o': 2}\n" ] } ], "source": [ "sen = 'hello' * 2\n", "d = dict()\n", "\n", "for key in sen:\n", " if key in d:\n", " d[key] += 1\n", " else:\n", " d[key] = 1\n", " \n", "print(d)\n", " " ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'t': 1, 'f': 1, ' ': 2, '!': 1, 'i': 1, 's': 1, 'y': 1, 'P': 1, 'h': 1, 'u': 1, 'o': 1, 'n': 2}\n" ] } ], "source": [ "sen = \"Python is fun!\"\n", "sen_set = set(sen)\n", "d = {}\n", "\n", "for x in sen_set:\n", " d[x] = sen.count(x)\n", "# print(sen.count(x))\n", "print(d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "f = open(\"new.txt\",\"w\")\n", "\n", "for i in range(6,11):\n", " data = \"%d line\\n\" % i\n", " f.write(data)\n", "\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 line\n", "7 line\n", "8 line\n" ] } ], "source": [ "fr = open(\"new.txt\", \"r\")\n", "\n", "line = fr.readline()\n", "print(line, end='')\n", "\n", "line = fr.readline()\n", "print(line, end='')\n", "\n", "line = fr.readline()\n", "print(line, end='')\n", "\n", "fr.close()" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'this': 1, 'is': 1, 'test': 1, 'first': 1, 'line': 3, 'second': 1, 'third': 1}\n" ] } ], "source": [ "fr = open(\"test.txt\", \"r\")\n", "\n", "d = {}\n", "for line in fr:\n", " lineList = line.split()\n", " for word in lineList:\n", " if word in d:\n", " d[word] += 1\n", " else:\n", " d[word] = 1\n", "\n", "\n", "f.close()\n", "\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['third', 'line']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lineList" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 11\n", "6 11\n" ] } ], "source": [ "a = [5,5,6,7,8,3]\n", "b = \"I am a boy.\"\n", "\n", "def my_len(l):\n", " count = 0\n", " for x in l:\n", " count += 1\n", " return count\n", "\n", "print(len(a), len(b))\n", "print(my_len(a), my_len(b))\n" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 -1 2 0.5\n" ] } ], "source": [ "a = 1\n", "b = 2\n", "\n", "def add(x, y):\n", " return x + y\n", "\n", "def sub(x, y):\n", " return x - y\n", "\n", "def mul(x, y):\n", " return x * y\n", "\n", "def div(x, y):\n", " return x / y\n", "\n", "\n", "c = add(a, b)\n", "d = sub(a, b)\n", "e = mul(a, b)\n", "f = div(a, b)\n", "\n", "\n", "print(c, d, e, f)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial number : 4\n", "24\n" ] } ], "source": [ "num = int(input('Factorial number : '))\n", "\n", "def factorial(n):\n", " if n == 0:\n", " return 1\n", " return n * factorial(n-1)\n", "\n", "res = factorial(num)\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## assignment 01" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "top-level A.py\n", "A.py 직접 실행\n" ] } ], "source": [ "def func():\n", " print(\"function A.py\")\n", "\n", "print(\"top-level A.py\")\n", "\n", "if __name__ == \"__main__\":\n", " print(\"A.py 직접 실행\")\n", "else:\n", " print(\"A.py가 임포트되어 사용됨\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }