{ "metadata": { "name": "", "signature": "sha256:4f962d4e16c48fdae6bd85f1bb692b638dbc9a5ebc7319bab2c289bfd88be313" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 2. \ud30c\uc774\uc36c \ubb38\uacfc \uae30\ubcf8 \uc790\ub8cc\ud615\n", "- \ucc38\uace0 \uc0ac\uc774\ud2b8\n", " 1. \ub0b4\uc7a5(Built-in) \ud568\uc218: https://docs.python.org/2/library/functions.html\n", " 1. \uc790\ub8cc\ud615: https://docs.python.org/2/library/types.html\n", "***\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 01 \ubcc0\uc218\uba85 \ubc0f \uc608\uc57d\uc5b4\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-1 \ubcc0\uc218\uba85 \ub9cc\ub4dc\ub294 \ubc95" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub300\uc18c\ubb38\uc790 \uad6c\ubcc4\ud568\n", "- \ub2e4\uc74c\uacfc \uac19\uc740 \uac83\uc740 \ubcc0\uc218\uba85\uc774 \ub420 \uc218 \uc5c6\uc74c\n", " - 1abc, @file, %x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-2 \uc608\uc57d\uc5b4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import keyword\n", "\n", "print keyword.kwlist\n", "print\n", "print len(keyword.kwlist)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']\n", "\n", "31\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-3 \ubcc0\uc218\uba85 \ub9cc\ub4e4 \ub54c \uc870\uc2ec\ud560 \uc810" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc57d\uc5b4, \ub0b4\uc7a5\ud568\uc218, \ubaa8\ub4c8 \uc774\ub984\uc744 \ubcc0\uc218\uba85\uc73c\ub85c \ub9cc\ub4dc\ub294 \uc77c\uc774 \uc5c6\ub3c4\ub85d \ud560\uac83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str(12345)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "'12345'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "str = 'abc'\n", "\n", "str(12345)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object is not callable", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'abc'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m12345\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object is not callable" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 02 \ud30c\uc774\uc36c \uae30\ucd08\ubb38\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-1 \uc8fc\uc11d\ubb38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#\uc774\uac83\uc740 \uc8fc\uc11d\uc785\ub2c8\ub2e4.\n", "import sys #\uc774\uac83\ub3c4 \uc8fc\uc11d\uc785\ub2c8\ub2e4." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-2 \uc5f0\uc18d\ub77c\uc778" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if (a == 1) and" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m if (a == 1) and\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "b = 3\n", "if (a == 1) and \\\n", "(b == 3):\n", " print 'connected lines'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "connected lines\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-3 \ud560\ub2f9\ubb38 (\uce58\ud658\ubb38, Assignment)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "b = a" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "1 + 3 = a" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "can't assign to operator (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1 + 3 = a\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "a = a + 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "4 == 5" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "False" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "4 == 4" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "True" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "c, d = 3, 4\n", "x = y = z = 0\n", "e = 3.5; f = 5.6\n", "\n", "print a, b, c, d, e, f" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 1 3 4 3.5 5.6\n" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "e, f = f, e\n", "print e, f" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5.6 3.5\n" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc544\ub798\uc5d0\uc11c b = c + d\ub294 \uc2dd(Expression)\uc774 \uc544\ub2c8\ub77c \ubb38(Statement)\uc774\uae30 \ub54c\ubb38\uc5d0 a\uc5d0 \ud560\ub2f9\ub420 \uc218 \uc5c6\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = (b = c + d)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m a = (b = c + d)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubcc0\uc218\uc758 \uc790\ub8cc\ud615 \ud655\uc778\uc744 \uc704\ud574\uc11c\ub294 type() \ud568\uc218 \uc0ac\uc6a9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "type(a)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "int" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 'dynamic'\n", "type(a)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "str" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-4 \ud655\uc7a5 \ud560\ub2f9\ubb38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "a += 4\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "5" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "a -= 3\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "2" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "a *= 2+3\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "10" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-5 \uc774\ub984\uacfc \uac1d\uccb4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- a = 1\uc774\ub77c\ub294 Statement\uc5d0\uc11c a\ub294 \uc774\ub984, 1\uc740 \uac1d\uccb4\uc774\uba70 a \ubcc0\uc218\ub294 1\uc774\ub77c\ub294 \uac1d\uccb4\ub97c \uac00\ub9ac\ud0a8\ub2e4.\n", "- a \ubcc0\uc218\ub294 1 \uac1d\uccb4\uc758 \ub808\ud37c\ub7f0\uc2a4\ub97c \uc9c0\ub2c8\uace0 \uc788\uc74c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-6 \uac1d\uccb4\uc640 \ud560\ub2f9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1,2,3]\n", "b = [10, a, 20]\n", "c = ['x', a, 'y']\n", "\n", "print a\n", "print b\n", "print c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3]\n", "[10, [1, 2, 3], 20]\n", "['a', [1, 2, 3], 'b']\n" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "- \uac1d\uccb4\uc758 \ubcc0\uc218\ub294 \ud574\ub2f9 \uac1d\uccb4\uc758 \ub808\ud37c\ub7f0\uc2a4\ub97c \uc9c0\ub2c8\uace0 \uc788\uc74c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/assignment.png)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a[1] = 1000\n", "\n", "print a\n", "print b\n", "print c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 1000, 3]\n", "[10, [1, 1000, 3], 20]\n", "['a', [1, 1000, 3], 'b']\n" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 03 \ubb38\uc790\uc5f4\ub85c \ub41c \ud30c\uc774\uc36c \ucf54\ub4dc \uc2e4\ud589\ud558\uae30\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-1 eval" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- eval() \ub0b4\uc7a5 \ud568\uc218\ub294 \ubb38\uc790\uc5f4\ub85c \ub418\uc5b4 \uc788\ub294 \ud30c\uc774\uc36c Expression\uc744 \uc2e4\ud589\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "a = eval('a + 4')\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "5" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- eval() \ub0b4\uc7a5 \ud568\uc218\ub85c \ud30c\uc774\uc36c \ubb38 (Statement)\uc744 \uc2e4\ud589\ud560 \uc218 \uc5c6\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "eval('a = a + 4')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m a = a + 4\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-2 exec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- exec Statement\ub294 \ubb38\uc790\uc5f4\ub85c \ub418\uc5b4 \uc788\ub294 \ud30c\uc774\uc36c Statement\uc744 \uc2e4\ud589\ud55c\ub2e4.\n", "- exec\ub294 \ub0b4\uc7a5\ud568\uc218\uac00 \uc544\ub2c8\ub2e4.\n", "- \uc0ac\uc6a9\ubc95\n", " 1. exec(statement)\n", " 2. exe statement\n", "- \uc5ec\ub7ec \uac1c\uc758 Statement\ub3c4 \ud55c\ubc88\uc5d0 \uc2e4\ud589\ud560 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 5\n", "exec('a = a + 4')\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "9" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 5\n", "exec 'a = a + 4'\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "9" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "s = '''\n", "a = 1\n", "if a > 0 :\n", " print 'Success'\n", "'''\n", "exec(s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Success\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "3-3 compile(*)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- compile(): \ud30c\uc774\uc120 \uc18c\uc2a4\ub97c \uc785\ub825\ubc1b\uc544 \uc18c\uc2a4 \ucf54\ub4dc \uac1d\uccb4\ub85c \uc800\uc7a5\ud558\ub294 \ub0b4\uc7a5\ud568\uc218\n", "- compile() \ub0b4\uc7a5\ud568\uc218 \ud615\uc2dd\n", " - compile(string, filename, kind)\n", " - kind\n", " - eval: \ud558\ub098\uc758 Expression \ucef4\ud30c\uc77c\n", " - single: \ud558\ub098\uc758 Statement \ucef4\ud30c\uc77c\n", " - exec: \uc5ec\ub7ec\uac1c\uc758 Statement \ucef4\ud30c\uc77c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "code = compile('a + 1', '', 'eval')\n", "print type(code)\n", "a = 1\n", "for k in range(10):\n", " a = eval(code)\n", " \n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "11" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "code = compile('a = a + 1', '', 'single')\n", "a = 1\n", "for k in range(10):\n", " exec(code)\n", "\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "11" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "s = '''\n", "a = 1\n", "for k in range(10):\n", " a = a + 1\n", "print a \n", "'''\n", "\n", "code = compile(s, '', 'exec')\n", "exec(code)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 04 \ucf58\uc194 \uc785\ucd9c\ub825\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4-1 \ucf58\uc194 \uc785\ub825" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ucf58\uc194 (Console): \uc708\ub3c4\uc6b0\uc988\uc5d0\uc11c\ub294 Command\ucc3d, \ub9ac\ub205\uc2a4/\ub9e5\uc5d0\uc11c\ub294 Terminal\ucc3d\n", "- raw_input(): \ubb38\uc790\uc5f4 \uc785\ub825 \ub0b4\uc7a5\ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = raw_input('name?')" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name?\uc774\uac15\uc131\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "print name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\uc774\uac15\uc131\n" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- int(): \ubb38\uc790\uc5f4\uc744 \uc815\uc218\ub85c \ubcc0\ud658\ud558\ub294 \ub0b4\uc7a5\ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "k = int(raw_input('int : '))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "int : 89\n" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "k" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "89" ] } ], "prompt_number": 60 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- input(): \uc815\uc218, \uc2e4\uc218, Expression \uc785\ub825 \ub0b4\uc7a5\ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i = input('int :')" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "int :45\n" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "i" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 62, "text": [ "45" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "k = input('expr:')" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "expr:30+50\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "k" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "80" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4-2 \ucf58\uc194 \ucd9c\ub825" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- print: \ud654\uba74\uc5d0 \uc790\ub8cc\ub97c \ucd9c\ub825\ud558\ub294 \ubcf4\ud3b8\uc801\uc778 statement\n", "- \uc5ec\ub7ec \uc790\ub8cc\ub97c \ud55c\uaebc\ubc88\uc5d0 \ucd9c\ub825\ud560 \ub54c\uc5d0\ub294 \ucf64\ub9c8(,)\ub97c \uc0ac\uc6a9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 4+5, 4-2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "9 2\n" ] } ], "prompt_number": 68 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc138\ubbf8\ucf5c\ub860(;)\uc740 \uc21c\ucc28\uc801\uc73c\ub85c \uc785\ub825\ub41c \uac01 statement\ub97c \ubd84\ub9ac\ud568 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 1; print 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n" ] } ], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uae30\ubcf8\uc801\uc73c\ub85c print\ub294 \ub9c8\uc9c0\ub9c9\uc5d0 \uc904\ubc14\uafc8\uc744 \ud558\uc9c0\ub9cc \ucf64\ub9c8(,)\uac00 \ub9c8\uc9c0\ub9c9\uc5d0 \uc788\uc73c\uba74 \uc904\ubc14\uafc8\uc744 \ud558\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 1,; print 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2\n" ] } ], "prompt_number": 70 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- +\uc5f0\uc0b0\uc790\ub294 \uc22b\uc790\uc640 \ubb38\uc790\uc5f4\uc5d0 \ub300\ud55c \uc5f0\uc0b0\uc744 \uc9c0\uc6d0\ud558\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 12 + 'spam'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;36m12\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'spam'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "print '12' + 'spam'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12spam\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- pprint: \uc5b4\ub290\uc815\ub3c4 \ud615\uc2dd\ud654\ub41c \ucd9c\ub825\uc744 \uc9c0\uc6d0\ud568" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pprint \n", "complicated = ['spam', (1,2,3), ('hanm', 'egg', ('ab', 'cd', ('abc', 'def')))]\n", "complicated = complicated * 3\n", "\n", "pprint.pprint(complicated)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))),\n", " 'spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))),\n", " 'spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def')))]\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "print complicated" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['spam', (1, 2, 3), ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))), 'spam', (1, 2, 3), ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))), 'spam', (1, 2, 3), ('hanm', 'egg', ('ab', 'cd', ('abc', 'def')))]\n" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "complicated" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "['spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))),\n", " 'spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def'))),\n", " 'spam',\n", " (1, 2, 3),\n", " ('hanm', 'egg', ('ab', 'cd', ('abc', 'def')))]" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 05 \uc790\ub8cc\ud615\uc758 \uc885\ub958\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-1 \ubb38\uc790\uc5f4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud30c\uc774\uc36c\uc758 \uc790\ub8cc\ud615: http://cs.kangwon.ac.kr/~leeck/Python_for_data_analysis/2_python_datatype.pdf" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'Hello World!'\n", "\n", "print \"Hello World!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n", "Hello World!\n" ] } ], "prompt_number": 81 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Indexing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"Hello world!\"\n", "print s[0]\n", "print s[1]\n", "print s[-1]\n", "print s[-2]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "H\n", "e\n", "!\n", "d\n" ] } ], "prompt_number": 82 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Slicing\n", " - \ud615\uc2dd: [start(included) : stop(excluded) : step]\n", " - \uae30\ubcf8\uac12: start - 0, stop - \uc790\ub8cc\ud615\uc758 \ud06c\uae30, step - 1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"Hello world!\"\n", "print s[1:3]\n", "print s[0:5]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "el\n", "Hello\n" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'Hello'\n", "print s[1:]\n", "print s[:3]\n", "print s[:]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ello\n", "Hel\n", "Hello\n" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'abcd'\n", "print s[::2]\n", "print s[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ac\n", "dcba\n" ] } ], "prompt_number": 86 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubb38\uc790\uc5f4 \uc5f0\uc0b0\n", " - +: \uc5f0\uacb0\n", " - *: \ubc18\ubcf5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'Hello' + '' + 'World'\n", "print 'Hello' * 3\n", "print '-' * 60" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "HelloWorld\n", "HelloHelloHello\n", "------------------------------------------------------------\n" ] } ], "prompt_number": 87 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubb38\uc790\uc5f4 \uc790\ub8cc\ud615\uc740 \ubcc0\uacbd\ub418\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'Hello World'\n", "s[0] = 'h'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Hello World'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'h'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "prompt_number": 88 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubb38\uc790\uc5f4\uc744 \ubcc0\uacbd\ud558\ub824\uba74 Slicing \ubc0f \uc5f0\uacb0 \uc5f0\uc0b0 (+)\uc744 \uc8fc\ub85c \uc774\uc6a9\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'Hello World'\n", "s = 'h' + s[1:]\n", "s" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'hello World'" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- len(): \ubb38\uc790\uc5f4\uc758 \uae38\uc774\ub97c \ubc18\ud658\ud558\ub294 \ub0b4\uc7a5\ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'Hello World'\n", "len(s)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "11" ] } ], "prompt_number": 90 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- in, not in: \ubb38\uc790\uc5f4\ub0b4\uc5d0 \uc77c\ubd80 \ubb38\uc790\uc5f4\uc774 \ud3ec\ud568\ub418\uc5b4 \uc788\ub294\uc9c0\ub97c \ud30c\uc545\ud558\ub294 \ud0a4\uc6cc\ub4dc" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'Hello World'\n", "print 'World' in s\n", "print 'World' not in s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "False\n" ] } ], "prompt_number": 92 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-2 \ub9ac\uc2a4\ud2b8\n", "- \ub9ac\uc2a4\ud2b8: \uc784\uc758\uc758 \uac1d\uccb4\ub97c \uc21c\ucc28\uc801\uc73c\ub85c \uc800\uc7a5\ud558\ub294 \uc9d1\ud569\uc801 \uc790\ub8cc\ud615" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubb38\uc790\uc5f4\uc774 \uc9c0\ub2cc \uc5f0\uc0b0\ub4e4 \uc911 \uba87\uac00\uc9c0\ub294 \ub9ac\uc2a4\ud2b8\ub3c4 \uc9c0\uc6d0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1,2,3]\n", "print type(L)\n", "print\n", "print len(L)\n", "print\n", "print L[1]\n", "print L[-1]\n", "print L[1:3]\n", "print\n", "print L + L\n", "print L * 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "3\n", "\n", "2\n", "3\n", "[2, 3]\n", "\n", "[1, 2, 3, 1, 2, 3]\n", "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- range(k): 0\ubd80\ud130 k-1\uae4c\uc9c0\uc758 \uc22b\uc790\uc758 \ub9ac\uc2a4\ud2b8\ub97c \ubc18\ud658\ud568 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = range(10)\n", "print L\n", "print L[::2]\n", "print L[::-1]\n", "print 4 in L" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[0, 2, 4, 6, 8]\n", "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n", "True\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub9ac\uc2a4\ud2b8\uc758 \ub2e4\uc591\ud55c \uba54\uc18c\ub4dc\ub4e4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1,2,3]\n", "\n", "L.append(4)\n", "print L\n", "print\n", "del L[0]\n", "print L\n", "print\n", "L.reverse()\n", "print L\n", "print\n", "L.append(5)\n", "print L\n", "L.sort()\n", "print L" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4]\n", "\n", "[2, 3, 4]\n", "\n", "[4, 3, 2]\n", "\n", "[4, 3, 2, 5]\n", "[2, 3, 4, 5]\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-3 \ud29c\ud50c\n", "- \ud29c\ud50c: \ub9ac\uc2a4\ud2b8\uc640 \uc720\uc0ac\ud558\uc9c0\ub9cc \ud29c\ud50c \ub0b4\uc758 \uac12\uc744 \ubcc0\uacbd\ud560 \uc218 \uc5c6\uc74c\n", " - \uc801\ud569\ud55c \uc0ac\uc6a9 \uc608\n", " - months = ('January','February','March','April','May','June','July','August','September','October','November',' December')\n", " - \uac01 \uac12\uc5d0 \ub300\ud574 \uc778\ub371\uc2a4\uac00 \ubd80\uc5ec\ub428\n", "- \ubb38\uc790\uc5f4\uacfc \ub9ac\uc2a4\ud2b8\uac00 \uc9c0\ub2cc \uc5f0\uc0b0\ub4e4 \uc911 \uba87\uac00\uc9c0\ub294 \ud29c\ud50c\ub3c4 \uc9c0\uc6d0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (1,2,3)\n", "\n", "print len(t)\n", "print\n", "print t[0]\n", "print t[-1]\n", "print t[0:2]\n", "print t[::2]\n", "print\n", "print t + t + t\n", "print t * 3\n", "print\n", "print 3 in t" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "\n", "1\n", "3\n", "(1, 2)\n", "(1, 3)\n", "\n", "(1, 2, 3, 1, 2, 3, 1, 2, 3)\n", "(1, 2, 3, 1, 2, 3, 1, 2, 3)\n", "\n", "True\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud29c\ud50c\uc740 \ub0b4\uc6a9 \ubcc0\uacbd \ubd88\uac00" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (1,2,3)\n", "\n", "t[0] = 100" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 105 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ubc18\uba74\uc5d0 \ub9ac\uc2a4\ud2b8\ub294 \ub0b4\uc6a9 \ubcc0\uacbd \uac00\ub2a5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1,2,3]\n", "L[0] = 100\n", "\n", "print L" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[100, 2, 3]\n" ] } ], "prompt_number": 107 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-4 \uc0ac\uc804" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc815\uc218\ud615 \uc778\ub371\uc2a4\uac00 \uc544\ub2cc \ud0a4\ub97c \uc774\uc6a9\ud558\uc5ec \uac12\uc744 \uc800\uc7a5\ud558\ub294 \uc790\ub8cc \uad6c\uc870\n", " - \uc800\uc7a5\ub41c \uac01 \uc790\ub8cc\uc5d0 \ub300\ud55c \uc21c\uc11c\ub294 \uc758\ubbf8 \uc5c6\uc74c\n", "- \ub9e4\ud551(Mapping) \ud568\uc218\uc640 \ube44\uc2b7\ud55c \uc5ed\ud560\uc744 \ud568\n", " - x\ub77c\ub294 \ud0a4\uac12\uc744 \ub123\uc73c\uba74 \uac12 y\ub97c \ubc18\ud658\ud568\n", "- \ub0b4\uc6a9\uc774 \uc5c6\ub294 \ube48(Empty) \uc0ac\uc804\uc758 \ucd08\uae30\ud654 \ubc29\ubc95\n", " - a = {}" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'one': 'hana', 'two': 'dul', 'three': 'set'}\n", "print d['one']\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hana\n" ] } ], "prompt_number": 109 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'one': 'hana', 'two': 'dul', 'three': 'set'}\n", "d['four'] = 'net' # \uc0c8 \ud56d\ubaa9\uc758 \uc0bd\uc785\n", "print d\n", "d['one'] = 1 # \uae30\uc874 \ud56d\ubaa9\uc758 \uac12 \ubcc0\uacbd\n", "print d\n", "print 'one' in d # \ud0a4\uc5d0 \ub300\ud55c \uba64\ubc84\uc27d \ud14c\uc2a4\ud2b8" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'four': 'net', 'three': 'set', 'two': 'dul', 'one': 'hana'}\n", "{'four': 'net', 'three': 'set', 'two': 'dul', 'one': 1}\n", "True\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'one': 1, 'two': 'dul', 'three': 'set', 'four': 'net'}\n", "print d.keys() # \ud0a4\ub9cc \ub9ac\uc2a4\ud2b8\ub85c \ucd94\ucd9c\ud568\n", "print d.values() # \uac12\ub9cc \ub9ac\uc2a4\ud2b8\ub85c \ucd94\ucd9c\ud568\n", "print d.items() # \ud0a4\uc640 \uac12\uc758 \ud29c\ud50c\uc744 \ub9ac\uc2a4\ud2b8\ub85c \ubc18\ud658\ud568" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['four', 'three', 'two', 'one']\n", "['net', 'set', 'dul', 1]\n", "[('four', 'net'), ('three', 'set'), ('two', 'dul'), ('one', 1)]\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-5 \ub0b4\uc7a5 \uc790\ub8cc\ud615\uc758 \ubd84\ub958" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| \uc790\ub8cc\ud615 | \uc800\uc7a5/\uc811\uadfc \ubc29\ubc95 | \ubcc0\uacbd \uac00\ub2a5\uc131 | \uc800\uc7a5 \ubaa8\ub378 |\n", "|:------:|:-----------------:|:-----------:|:--------------------:|\n", "| \uc218\uce58\ud615 | \uc9c1\uc811(Direct) | \ubcc0\uacbd\ubd88\uac00\ub2a5(Immutable) | \ub9ac\ud130\ub7f4 (Literal) |\n", "| \ubb38\uc790\uc5f4 | \uc2dc\ud000\uc2a4 (Sequence) | \ubcc0\uacbd\ubd88\uac00\ub2a5(Immutable) | \ub9ac\ud130\ub7f4 (Literal) |\n", "| \ub9ac\uc2a4\ud2b8 | \uc2dc\ud000\uc2a4 (Sequence) | \ubcc0\uacbd\uac00\ub2a5(Mutable) | \ucee8\ud14c\uc774\ub108 (Container) |\n", "| \ud29c\ud50c | \uc2dc\ud000\uc2a4 (Sequence) | \ubcc0\uacbd\ubd88\uac00\ub2a5(Immutable) | \ucee8\ud14c\uc774\ub108 (Container) |\n", "| \uc0ac\uc804 | \ub9e4\ud551 (Mapping) | \ubcc0\uacbd\uac00\ub2a5(Mutable) | \ucee8\ud14c\uc774\ub108 (Container) |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 06 \ubcc0\uacbd \uac00\ub2a5\uc131\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub9ac\uc2a4\ud2b8\ub294 \ubcc0\uacbd \uac00\ub2a5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = [1,2,3]\n", "s[1] = 10\n", "s" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 117, "text": [ "[1, 10, 3]" ] } ], "prompt_number": 117 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud29c\ud50c\uc740 \ubcc0\uacbd \ubd88\uac00\ub2a5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = (1,2,3)\n", "s[1] = 10" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 118 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc544\ub798 \uc608\uc5d0\uc11c\ub294 x\uac00 \uc9c0\ub2c8\uace0 \uc788\ub294 1\uc758 \uac12\uc774 \ubcc0\uacbd\ub418\ub294 \uac83\uc774 \uc544\ub2c8\ub77c \uc0c8\ub85c\uc6b4 \uac1d\uccb4 2\ub85c \ub808\ud37c\ub7f0\uc2a4\ub97c \ubcc0\uacbd\ud558\ub294 \uac83\uc784" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "x = 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image1](images/referenceChangeNumerical.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub9ac\uc2a4\ud2b8\ub3c4 \ubcc0\uc218\uc758 \ub808\ud37c\ub7f0\uc2a4\ub97c \ubcc0\uacbd\ud558\ub294 \uc591\uc2dd\uc73c\ub85c \ub3d9\uc77c\ud55c \ubcc0\uc218\uc5d0 \ud560\ub2f9\ub420 \uc218 \uc788\uc74c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l1 = [1,2,3]\n", "l1 = [4,5,6]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 120 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/referenceChangeList.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub9ac\uc2a4\ud2b8\ub294 \uac1d\uccb4 \ub0b4\ubd80\uc758 \uac12 \ubcc0\uacbd \uac00\ub2a5\ud568" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l1 = [4,5,6]\n", "l1[0] = 10" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/changeListElement.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 07 \uc790\ub8cc\ud615 \ud655\uc778\uacfc \uae30\ud0c0\uc758 \uc790\ub8cc\ud615\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7-1 \ub0b4\uc7a5 \uc790\ub8cc\ud615 \uc54c\uc544\ubcf4\uae30" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print type(3) #\uc815\uc218" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(3.3) #\uc2e4\uc218" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "print type('abc') #\ubb38\uc790\uc5f4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(None) #None \uac1d\uccb4, \uc544\ubb34 \uac12\ub3c4 \uc5c6\ub2e4(\ud639\uc740 \uc544\ub2c8\ub2e4)\ub97c \ub098\ud0c0\ub0b4\ub294 \uac1d\uccb4\n", "print\n", "a = None\n", "print a\n", "print type(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "None\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "print type([]) #\ub9ac\uc2a4\ud2b8" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(()) #\ud29c\ud50c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "print type({}) #\uc0ac\uc804(dict)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(type({})) #'type'\ud615" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc790\ub8cc\ud615\uc758 \ube44\uad50" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 0\n", "L = [1,2,3]\n", "print type(a) == type(0)\n", "print type(L) == type([])\n", "print type(L[0]) == type(0)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "True\n", "True\n" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7-2 \uae30\ud0c0 \uc790\ub8cc\ud615" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import types\n", "\n", "print dir(types)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__', '__package__']\n" ] } ], "prompt_number": 134 }, { "cell_type": "code", "collapsed": false, "input": [ "from types import *\n", "\n", "print type(123) == IntType\n", "print type('abc') == StringType" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "True\n" ] } ], "prompt_number": 136 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 08 \uba54\ubaa8\ub9ac \uad00\ub9ac\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8-1 \uc4f0\ub808\uae30 \uc218\uc9d1 (Garbage Collection)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = y = z = 100" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 137 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/referenceCount1.png)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "del x" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 138 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/referenceCount2.png)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y = 200\n", "z = 300" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/referenceCount3.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8-2 \ub808\ud37c\ub7f0\uc2a4 \uce74\uc6b4\ud2b8\ub97c \uc5bb\ub294 \ubc95" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- sys \ubaa8\ub4c8\uc758 getrefcount() \ud568\uc218: \ubcf8 \ud568\uc218\uac00 \uc791\uc5c5 \ucc98\ub9ac\ub97c \ud558\uae30 \uc704\ud574 \uc6d0\ub798 \ub808\ud37c\ub7f0\uc2a4 \uac12\ubcf4\ub2e4 1 \ud070 \uac12\uc744 \ubc18\ud658\ud568" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "aaa = 191919\n", "print sys.getrefcount(aaa)\n", "\n", "bbb = object()\n", "print sys.getrefcount(bbb)\n", "\n", "print sys.getrefcount('foobar')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "2\n", "3\n" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### 8-3 \uac1d\uccb4\uc758 \uc2e0\uc6d0 \uc2dd\ubcc4\ud558\uae30" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- id(): \uac1d\uccb4\uc758 \uc2dd\ubcc4\uc790\ub97c \ubc18\ud658\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 500\n", "b = a\n", "print id(a)\n", "print id(b)\n", "print\n", "x = 1\n", "y = 1\n", "print id(x)\n", "print id(y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4478569376\n", "4478569376\n", "\n", "4298182776\n", "4298182776\n" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- is \ud0a4\uc6cc\ub4dc: \ub450 \uac1d\uccb4\uc758 \uc2dd\ubcc4\uc790\uac00 \ub3d9\uc77c\ud55c\uc9c0 \ud14c\uc2a4\ud2b8\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = [1,2,3]\n", "d = [1,2,3]\n", "print c is d\n", "\n", "a = 500\n", "b = a\n", "print a is b\n", "\n", "x = 1\n", "y = 1\n", "print x is y\n", "\n", "e = f = [4,5,6]\n", "print e is f" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "False\n", "True\n", "True\n", "True\n" ] } ], "prompt_number": 60 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- == \uc5f0\uc0b0\uc790: \ub450 \uac1d\uccb4\uc758 \uac12\uc774 \ub3d9\uc77c\ud55c\uc9c0\ub97c \ud14c\uc2a4\ud2b8\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = [1,2,3]\n", "d = [1,2,3]\n", "c == d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 150, "text": [ "True" ] } ], "prompt_number": 150 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 09 \ud30c\uc774\uc36c \uc81c\uc5b4\ubb38\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9-1 if\ubb38\n", "- if\ubb38\uc758 \ud615\uc2dd\n", "\n", "> if \uc870\uac74\uc2dd1:\n", "\n", ">   statements\n", "\n", "> elif \uc870\uac74\uc2dd2:\n", "\n", ">   statements\n", "\n", "> elif \uc870\uac74\uc2dd3:\n", "\n", ">   statements\n", "\n", "> else:\n", "\n", ">   statements\n", "\n", "- \uc870\uac74\uc2dd\uc774\ub098 else \ub2e4\uc74c\uc5d0 \ucf5c\ub860(:) \ud45c\uae30 \ud544\uc694\n", "- \ub4e4\uc5ec\uc4f0\uae30(indentation)\ub97c \uc798 \uc9c0\ucf1c\uc57c \ud568" ] }, { "cell_type": "code", "collapsed": false, "input": [ "score = 90\n", "if score >= 90:\n", " print 'Congratulations!!! '" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Congratulations!!! \n" ] } ], "prompt_number": 153 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "if a > 5:\n", " print 'Big'\n", "else:\n", " print 'Small'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Big\n" ] } ], "prompt_number": 61 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- statement\uac00 1\uac1c\uc778 \uacbd\uc6b0 \uc870\uac74\uc2dd\uacfc \ud55c \uc904\uc5d0 \uc704\uce58 \uac00\ub2a5 (\ucd94\ucc9c\ud558\uc9c0\ub294 \uc54a\uc74c)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "if a > 5: print 'Big'\n", "else: print 'Small'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Big\n" ] } ], "prompt_number": 155 }, { "cell_type": "code", "collapsed": false, "input": [ "n = -2\n", "if n > 0:\n", " print 'Positive'\n", "elif n < 0:\n", " print 'Negative'\n", "else:\n", " print 'Zero'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Negative\n" ] } ], "prompt_number": 156 }, { "cell_type": "code", "collapsed": false, "input": [ "order = 'spagetti'\n", "\n", "if order == 'spam':\n", " price = 500\n", "elif order == 'ham':\n", " price = 700\n", "elif order == 'egg':\n", " price = 300\n", "elif order == 'spagetti':\n", " price = 900\n", " \n", "print price" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "900\n" ] } ], "prompt_number": 65 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc704 \ucf54\ub4dc\uc640 \ub3d9\uc77c\ud55c dictionary \uc790\ub8cc\uad6c\uc870\ub97c \uc0ac\uc6a9\ud55c \ucf54\ub4dc" ] }, { "cell_type": "code", "collapsed": false, "input": [ "order = 'spagetti'\n", "menu = {'spam':500, 'ham':700, 'egg':300, 'spagetti':900}\n", "price = menu[order]\n", "print price" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "900\n" ] } ], "prompt_number": 66 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9-2 \ub4e4\uc5ec\uc4f0\uae30\uc640 \uc81c\uc5b4\ubb38" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud30c\uc774\uc36c\uc740 \ub4e4\uc5ec\uc4f0\uae30\ub97c \uac15\uc81c\ud558\uc5ec \ucf54\ub4dc\uc758 \uac00\ub3c5\uc131\uc744 \ub192\uc778\ub2e4.\n", "- \uac00\uc7a5 \ubc14\uae65\ucabd\uc758 \ucf54\ub4dc\ub294 \ubc18\ub4dc\uc2dc 1\uc5f4\uc5d0 \uc2dc\uc791\ud55c\ub2e4.\n", "- \ube14\ub85d \ub0b4\ubd80\uc5d0 \uc788\ub294 statement\ub4e4\uc740 \ub3d9\uc77c\ud55c \uc5f4\uc5d0 \uc704\uce58\ud574\uc57c \ud55c\ub2e4.\n", "- \ube14\ub85d\uc758 \ub05d\uc740 \ub4e4\uc5ec\uc4f0\uae30\uac00 \ub05d\ub098\ub294 \ubd80\ubd84\uc73c\ub85c \uac04\uc8fc\ub41c\ub2e4.\n", " - python\uc5d0\ub294 {, }, begin, end \ub4f1\uc758 \ud0a4\uc6cc\ub4dc\uac00 \uc874\uc7ac\ud558\uc9c0 \uc54a\ub294\ub2e4.\n", "- \ub4e4\uc5ec\uc4f0\uae30\ub97c \ud560 \ub54c\uc5d0\ub294 \ud0ed\uacfc \uacf5\ubc31\uc744 \uc11e\uc5b4 \uc4f0\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1 # \uc131\uacf5\n", " a = 1 # \uc2e4\ud328" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "unexpected indent (, line 2)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m a = 1 # \uc2e4\ud328\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" ] } ], "prompt_number": 160 }, { "cell_type": "code", "collapsed": false, "input": [ "if a > 1:\n", " print 'big'\n", " print 'really?'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "unexpected indent (, line 3)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m print 'really?'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" ] } ], "prompt_number": 161 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9-3 for\ubb38\n", "\n", "> if <\ud0c0\uac9f> in <\ucee8\ud14c\uc774\ub108 \uac1d\uccb4>:\n", "\n", ">   statements\n", "\n", "> else:\n", "\n", ">   statements" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = ['cat', 'cow', 'tiger']\n", "for x in a:\n", " print len(x), x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3 cat\n", "3 cow\n", "5 tiger\n" ] } ], "prompt_number": 168 }, { "cell_type": "code", "collapsed": false, "input": [ "for x in [1,2,3]:\n", " print x," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3\n" ] } ], "prompt_number": 169 }, { "cell_type": "code", "collapsed": false, "input": [ "print range(10)\n", "\n", "for x in range(10):\n", " print x," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "0 1 2 3 4 5 6 7 8 9\n" ] } ], "prompt_number": 172 }, { "cell_type": "code", "collapsed": false, "input": [ "sum = 0\n", "for x in range(1, 11):\n", " sum = sum + x\n", " \n", "print sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] } ], "prompt_number": 174 }, { "cell_type": "code", "collapsed": false, "input": [ "prod = 1\n", "for x in range(1, 11):\n", " prod = prod * x\n", " \n", "prod" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 175, "text": [ "3628800" ] } ], "prompt_number": 175 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- enumerate() \ub0b4\uc7a5 \ud568\uc218: \ucee8\ud14c\uc774\ub108 \uac1d\uccb4\uac00 \uc9c0\ub2cc \uac01 \uc694\uc18c\uac12\ubfd0\ub9cc \uc544\ub2c8\ub77c \uc778\ub371\uc2a4 \uac12\ub3c4 \ud568\uaed8 \ubc18\ud658\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = ['cat', 'dog', 'bird', 'pig']\n", "for k, animal in enumerate(l):\n", " print k, animal" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 cat\n", "1 dog\n", "2 bird\n", "3 pig\n" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "t = ('cat', 'dog', 'bird', 'pig')\n", "for k, animal in enumerate(t):\n", " print k, animal" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 cat\n", "1 dog\n", "2 bird\n", "3 pig\n" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'c':'cat', 'd':'dog', 'b':'bird', 'p':'pig'}\n", "for k, key in enumerate(d):\n", " print k, key, d[key]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 p pig\n", "1 c cat\n", "2 b bird\n", "3 d dog\n" ] } ], "prompt_number": 71 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- break: \ub8e8\ud504\ub97c \ube60\uc838\ub098\uac04\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10):\n", " if x > 3: break\n", " print x\n", " \n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "done\n" ] } ], "prompt_number": 176 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- continue: \ub8e8\ud504 \ube14\ub85d \ub0b4\uc758 continue \uc774\ud6c4 \ubd80\ubd84\uc740 \uc218\ud589\ud558\uc9c0 \uc54a\uace0 \ub8e8\ud504\uc758 \uc2dc\uc791\ubd80\ubd84\uc73c\ub85c \uc774\ub3d9\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10):\n", " if x < 8: continue\n", " print x\n", " \n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "8\n", "9\n", "done\n" ] } ], "prompt_number": 177 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- else: \ub8e8\ud504\uac00 break\uc5d0 \uc758\ud55c \uc911\ub2e8 \uc5c6\uc774 \uc815\uc0c1\uc801\uc73c\ub85c \ubaa8\ub450 \uc218\ud589\ub418\uba74 else \ube14\ub85d\uc774 \uc218\ud589\ub41c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10):\n", " print x, # \ucf64\ub9c8(,) \ub54c\ubb38\uc5d0 \uc904\uc774 \ubc14\ub00c\uc9c0 \uc54a\ub294\ub2e4.\n", "else:\n", " print 'else block'\n", "\n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9 else block\n", "done\n" ] } ], "prompt_number": 73 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- break\uc5d0 \uc758\ud558\uc5ec \ub8e8\ud504\ub97c \ube60\uc838\ub098\uac00\uba74 else \ube14\ub85d\ub3c4 \uc218\ud589\ub418\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10):\n", " break\n", " print x,\n", "else:\n", " print 'else block'\n", " \n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "done\n" ] } ], "prompt_number": 179 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- for \ub8e8\ud504\uc758 \uc911\ucca9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(2, 4):\n", " for y in range(2, 10):\n", " print x, '*', y, '=', x*y\n", " print" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2 * 2 = 4\n", "2 * 3 = 6\n", "2 * 4 = 8\n", "2 * 5 = 10\n", "2 * 6 = 12\n", "2 * 7 = 14\n", "2 * 8 = 16\n", "2 * 9 = 18\n", "\n", "3 * 2 = 6\n", "3 * 3 = 9\n", "3 * 4 = 12\n", "3 * 5 = 15\n", "3 * 6 = 18\n", "3 * 7 = 21\n", "3 * 8 = 24\n", "3 * 9 = 27\n", "\n" ] } ], "prompt_number": 182 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9-4 while\ubb38" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- while \uc870\uac74\uc2dd\uc774 \ub9cc\uc871\ud558\ub294 \ub3d9\uc548 while \ube14\ub85d\ub0b4\uc758 statements \ub4e4\uc744 \ubc18\ubcf5 \uc218\ud589\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "count = 1\n", "while count < 11:\n", " print count\n", " count = count + 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "prompt_number": 183 }, { "cell_type": "code", "collapsed": false, "input": [ "sum = 0\n", "a = 0\n", "while a < 10:\n", " a = a + 1\n", " sum = sum + a\n", "print sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "while x < 10:\n", " print x, # \ucf64\ub9c8(,) \ub54c\ubb38\uc5d0 \uc904\uc774 \ubc14\ub00c\uc9c0 \uc54a\ub294\ub2e4.\n", " x = x + 1\n", "else:\n", " print 'else block'\n", "\n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9 else block\n", "done\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "while x < 10:\n", " break\n", " print x,\n", " x = x + 1\n", "else:\n", " print 'else block'\n", " \n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "done\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc544\ub798 \ucf54\ub4dc\uc5d0\uc11c continue \ub294 \ud544\uc694\ud55c \uad6c\ubb38\uc778\uac00?\n", "- \uc544\ub798 \ucf54\ub4dc\uc5d0\uc11c else: \ub294 while\uacfc if \uc911 \uc5b4\ub290 \ud0a4\uc6cc\ub4dc\uc640 \uc5f0\uacb0\ub418\ub294\uac00?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "while x < 10:\n", " if x < 8:\n", " x = x + 1\n", " continue\n", " else:\n", " print x,\n", " x = x + 1\n", " \n", "print 'done'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "8 9 done\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 10 \ud568\uc218\n", "***\n", "- \ud568\uc218\ub294 \ubc18\ubcf5\uc801\uc778 \ucf54\ub4dc\ub97c \uc5c6\uc5d0 \uc8fc\uc5b4 \ucf54\ub4dc\uc758 \uae38\uc774\ub97c \uc9e7\uac8c \ub9cc\ub4e4\uc5b4 \uc900\ub2e4.\n", "- \ucf54\ub4dc\uc758 \uc720\uc9c0\ubcf4\uc218\ub97c \uc27d\uac8c \ub9cc\ub4e4\uc5b4 \uc900\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(a, b):\n", " return a + b\n", "\n", "print add(3, 4)\n", "\n", "print add([1,2,3], [4,5,6])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "7\n", "[1, 2, 3, 4, 5, 6]\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc778\uc790\uc758 \uc774\ub984\uacfc \ud568\uaed8 \uc778\uc790 \uac12\uc744 \ub118\uaca8\uc904 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def minus(a, b):\n", " return a - b\n", "print minus(a=12, b=20)\n", "print minus(b=20, a=12)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-8\n", "-8\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc778\uc790\uc758 \ub514\ud3f4\ud2b8 \uac12\uc744 \uc9c0\uc815\ud560 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def incr(x, y=1):\n", " return x + y\n", "\n", "print incr(5)\n", "\n", "print incr(5, 10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n", "15\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }