{ "metadata": { "name": "", "signature": "sha256:0d14ea254c5e581553c40bdff9f2b31b4c6f87e5c409acdbecafd34ea54f5d5a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 8. \uac1d\uccb4\uc758 \ubcf5\uc0ac \ubc0f \ud615 \ubcc0\ud658\n", "***\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 01 \uac1d\uccb4\uc758 \ubcf5\uc0ac\n", "***\n", "- \ud30c\uc774\uc36c\uc758 \ubcf5\uc0ac \ubc29\ubc95\n", " - \ub808\ud37c\ub7f0\uc2a4 \ubcf5\uc0ac\n", " - \uc2e4\uc81c \uac1d\uccb4\uc758 \ubcf5\uc0ac" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1\n", "b = a" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1,2,3]\n", "b = [4,5,a]\n", "x = [a,b,100]\n", "y = x\n", "\n", "print x \n", "\n", "x.append(200)\n", "\n", "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[1, 2, 3], [4, 5, [1, 2, 3]], 100]\n", "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 200]\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/object_copy.png)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1,2,3,4,5]\n", "M = L[1:4] # \uc2ac\ub77c\uc774\uc2f1 - \uc2e4\uc81c \uac1d\uccb4 \ubcf5\uc0ac\uc758 \ud55c \ubc29\ubc95\n", "\n", "L[1] = 200\n", "\n", "print L \n", "print M # L\uc758 \ubcc0\uacbd\uc774 M\uc5d0 \uc601\ud5a5\uc744 \ubbf8\uce58\uc9c0 \uc54a\ub294\ub2e4." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 200, 3, 4, 5]\n", "[2, 3, 4]\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-1 copy \ubaa8\ub4c8\uc744 \uc774\uc6a9\ud55c \uac1d\uccb4 \ubcf5\uc0ac\n", "- copy\uc758 \uc885\ub958\n", " - \uc595\uc740 \ubcf5\uc0ac (Shwallow Copy): \ub300\uc0c1 \uac1d\uccb4\ub97c \ubcc4\ub3c4\ub85c \uc0dd\uc131\ud558\ub418 \uac1d\uccb4\uac00 \uc9c0\ub2cc \ub0b4\uc6a9\uc740 \ub808\ud37c\ub7f0\uc2a4\ub97c \ubcf5\uc0ac\ud55c\ub2e4.\n", " - \uae4a\uc740 \ubcf5\uc0ac (Deep Copy): \ub300\uc0c1 \uac1d\uccb4\ub97c \ubcc4\ub3c4\ub85c \uc0dd\uc131\ud558\uace0 \uac1d\uccb4\uac00 \uc9c0\ub2cc \uc7ac\uadc0\uc801\uc73c\ub85c \uacc4\uc18d\ud558\uc5ec \uc0dd\uc131\ud558\uc5ec \ubcf5\uc0ac\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import copy\n", "\n", "a = [1, 2, 3]\n", "b = [4, 5, a]\n", "x = [a, b, 100, 'w']\n", "y = copy.copy(x)\n", "print x\n", "print y\n", "print\n", "\n", "x[1][1] = 1000\n", "x[2] = 200\n", "x[3] = 'y'\n", "print x\n", "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']\n", "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']\n", "\n", "[[1, 2, 3], [4, 1000, [1, 2, 3]], 200, 'y']\n", "[[1, 2, 3], [4, 1000, [1, 2, 3]], 100, 'w']\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/object_shallow_copy.png)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import copy\n", "\n", "a = [1, 2, 3]\n", "b = [4, 5, a]\n", "x = [a, b, 100, 'w']\n", "y = copy.deepcopy(x)\n", "print x\n", "print y\n", "print\n", "\n", "x[1][1] = 1000\n", "x[2] = 200\n", "x[3] = 'y'\n", "print x\n", "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']\n", "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']\n", "\n", "[[1, 2, 3], [4, 1000, [1, 2, 3]], 200, 'y']\n", "[[1, 2, 3], [4, 5, [1, 2, 3]], 100, 'w']\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](images/object_deep_copy.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uae4a\uc740 \ubcf5\uc0ac\ub97c \ud560 \ub54c\uc5d0\ub294 \uac1d\uccb4\uac00 \uc9c0\ub2cc \ub0b4\uc6a9 \ub0b4\uc5d0 \uc7ac\uadc0\uc801\uc778 \ub0b4\uc6a9\uc774 \uc788\uc9c0 \uc54a\uc740\uac00 \uc8fc\uc758\ud574\uc57c \ud55c\ub2e4.\n", " - copy \ubaa8\ub4c8\uc758 deepcopy() \ud568\uc218\ub294 \uc774\uc640 \uac19\uc740 \uc7ac\uadc0\uc801\uc778 \ub0b4\uc6a9\uc5d0 \ub300\ud55c \ucc98\ub9ac\ub97c \uc774\ubbf8 \ub0b4\ubd80\uc801\uc73c\ub85c \ud558\uba74\uc11c \uad6c\ud604\ub418\uc5b4 \uc788\ub2e4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 02 \ud615 \ubcc0\ud658\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-1 \uc218\uce58 \ud615 \ubcc0\ud658" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1) \uc815\uc218\ud615 \ubcc0\ud658\n", "- \uc77c\ubc18\uc801\uc73c\ub85c int() \ub0b4\uc7a5\ud568\uc218\ub97c \uc0ac\uc6a9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = '1234'\n", "i = 10\n", "f = 4.56\n", "l = 12345678L\n", "\n", "print int(f), int(l), int(s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 12345678 1234\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub0b4\uc7a5 \ud568\uc218 int(): \uc18c\uc218 \ubd80\ubd84\uc744 \uc5c6\uc5d0\uace0 \uc815\uc218 \ubd80\ubd84\ub9cc \ucde8\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print int(1.1)\n", "print int(1.9)\n", "print int(-1.1)\n", "print int(-1.9)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "1\n", "-1\n", "-1\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub0b4\uc7a5 \ud568\uc218 round(): \ubc18\uc62c\ub9bc\ud558\uc5ec \uc815\uc218\ud615\uc758 \uc2e4\uc218\ub97c \ucde8\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print round(1.1)\n", "print round(1.49)\n", "print round(1.5)\n", "print round(1.9)\n", "print\n", "\n", "print round(-1.1)\n", "print round(-1.49)\n", "print round(-1.5)\n", "print round(-1.9)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.0\n", "1.0\n", "2.0\n", "2.0\n", "\n", "-1.0\n", "-1.0\n", "-2.0\n", "-2.0\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- math \ubaa8\ub4c8\uc758 \ud568\uc218 floor(): \uc8fc\uc5b4\uc9c4 \uc2e4\uc218\ubcf4\ub2e4 \uc791\uac70\ub098 \uac19\uc740 \uc218 \uc911\uc5d0\uc11c \uac00\uc7a5 \ud070 \uc815\uc218\ub97c \ucde8\ud55c\ub2e4.\n", " - [\ucc38\uace0]: http://ko.wikipedia.org/wiki/%EB%B0%94%EB%8B%A5_%ED%95%A8%EC%88%98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "print math.floor(1.0)\n", "print math.floor(1.1)\n", "print math.floor(1.9)\n", "print math.floor(-1.0)\n", "print math.floor(-1.1)\n", "print math.floor(-1.9)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.0\n", "1.0\n", "1.0\n", "-1.0\n", "-2.0\n", "-2.0\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- math \ubaa8\ub4c8\uc758 \ud568\uc218 ceil(): \uc8fc\uc5b4\uc9c4 \uc2e4\uc218\ubcf4\ub2e4 \ud06c\uac70\ub098 \uac19\uc740 \uc218 \uc911\uc5d0\uc11c \uac00\uc7a5 \uc791\uc740 \uc815\uc218\ub97c \ucde8\ud55c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print math.ceil(1.0)\n", "print math.ceil(1.1)\n", "print math.ceil(1.9)\n", "print math.ceil(-1.0)\n", "print math.ceil(-1.1)\n", "print math.ceil(-1.9)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.0\n", "2.0\n", "2.0\n", "-1.0\n", "-1.0\n", "-1.0\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub0b4\uc7a5 \ud568\uc218 float(): \uc2e4\uc218\ud615\uc73c\ub85c \uc804\ud658\n", "- \ub0b4\uc7a5 \ud568\uc218 long(): \ub871\ud615\uc73c\ub85c \uc804\ud658" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = '1234'\n", "i = 10\n", "f = 4.56\n", "l = 12345678L\n", "\n", "print float(i), float(l), float(s)\n", "print long(i), long(f), long(s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10.0 12345678.0 1234.0\n", "10 4 1234\n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-2 \uc2dc\ud000\uc2a4 \uc790\ub8cc \ud615 \ubcc0\ud658\n", "\n", "- \ub0b4\uc7a5 \ud568\uc218 list(): \ub9ac\uc2a4\ud2b8\ub85c \uc804\ud658\n", "- \ub0b4\uc7a5 \ud568\uc218 tuple(): \ud29c\ud50c\ub85c \uc804\ud658 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (1,2,3,4)\n", "l = [5,6,7,8]\n", "s = 'abcd'\n", "\n", "print list(t), list(s)\n", "print tuple(l), tuple(s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4] ['a', 'b', 'c', 'd']\n", "(5, 6, 7, 8) ('a', 'b', 'c', 'd')\n" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-3 \ubb38\uc790\uc5f4\ub85c\uc758 \ud615 \ubcc0\ud658\n", "\n", "- str(): \ube44\ud615\uc2dd\uc801\uc778 \ud3ec\ub9f7\uc758 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658\n", "- repr(): \ud615\uc2dd\uc801\uc778 \ud3ec\ub9f7\uc758 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658 (eval()\uc758 \uc815\ubc18\ub300 \uae30\ub2a5)\n", "- \\`obj`: repr(obj)\uacfc \ub3d9\uc77c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = str([1, 2, 3])\n", "b = repr([1, 2, 3])\n", "print type(a), type(b)\n", "print a\n", "print b\n", "print" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " \n", "[1, 2, 3]\n", "[1, 2, 3]\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print str([1,2,3]), str([4,5,6]), str('abc')\n", "print repr([1,2,3]), repr([4,5,6]), repr('abc')\n", "print\n", "print str(123), str(123.34), str(123456L)\n", "print repr(123), repr(123.34), repr(123456L)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3] [4, 5, 6] abc\n", "[1, 2, 3] [4, 5, 6] 'abc'\n", "\n", "123 123.34 123456\n", "123 123.34 123456L\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "s1 = repr([5,6,7,8])\n", "print type(s1)\n", "print s1\n", "print list(s1)\n", "print\n", "s2 = eval(s1) # \ubb38\uc790\uc5f4 '[5,6,7,8]'\ub85c\ubd80\ud130 \uc5ed\uc73c\ub85c \ub9ac\uc2a4\ud2b8 \uc0dd\uc131\n", "print type(s2)\n", "print s2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "[5, 6, 7, 8]\n", "['[', '5', ',', ' ', '6', ',', ' ', '7', ',', ' ', '8', ']']\n", "\n", "\n", "[5, 6, 7, 8]\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- eval(expr): \ud45c\ud604\uc2dd expr\uc744 Evaluation\ud558\ub294 \ub0b4\uc7a5 \ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "print eval('x+1')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "a = {1:\"one\", 2:\"two\"}\n", "b = repr(a) # a\ub97c \ud615\uc2dd\uc801\uc778 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658\n", "print type(b)\n", "print b\n", "print\n", "\n", "c = eval(b) # b\ub97c \uc2e4\ud589\ud558\uc5ec(\uc989, \uc0ac\uc804\uc774\ub9cc\ub4e4\uc5b4\uc9c4\ub2e4) c\uc5d0 \uce58\ud658\n", "print type(c)\n", "print c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "{1: 'one', 2: 'two'}\n", "\n", "\n", "{1: 'one', 2: 'two'}\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "L = ['\ud30c\ub780\ud558\ub298', 'blue sky', 1, 1234L, 1/3.0]\n", "for s in L:\n", " print 's:', s\n", " print 'str(s):', str(s)\n", " print 'repr(s):', repr(s)\n", " print '\\'s\\':', `s`\n", " print" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "s: \ud30c\ub780\ud558\ub298\n", "str(s): \ud30c\ub780\ud558\ub298\n", "repr(s): '\\xed\\x8c\\x8c\\xeb\\x9e\\x80\\xed\\x95\\x98\\xeb\\x8a\\x98'\n", "'s': '\\xed\\x8c\\x8c\\xeb\\x9e\\x80\\xed\\x95\\x98\\xeb\\x8a\\x98'\n", "\n", "s: blue sky\n", "str(s): blue sky\n", "repr(s): 'blue sky'\n", "'s': 'blue sky'\n", "\n", "s: 1\n", "str(s): 1\n", "repr(s): 1\n", "'s': 1\n", "\n", "s: 1234\n", "str(s): 1234\n", "repr(s): 1234L\n", "'s': 1234L\n", "\n", "s: 0.333333333333\n", "str(s): 0.333333333333\n", "repr(s): 0.3333333333333333\n", "'s': 0.3333333333333333\n", "\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-4 \ubb38\uc790\uc5f4 \uc694\uc18c\ub97c \uac16\ub294 \ub9ac\uc2a4\ud2b8\ub098 \ud29c\ud50c\uc744 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658: join \ud568\uc218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import string\n", "\n", "s = 'Python is the first language'\n", "L = s.split()\n", "\n", "print L\n", "print ' '.join(L)\n", "print '-'.join(L)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Python', 'is', 'the', 'first', 'language']\n", "Python is the first language\n", "Python-is-the-first-language\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-5 \ub9ac\uc2a4\ud2b8, \ud29c\ud50c\uacfc \uc0ac\uc804\uc758 \ubcc0\ud658" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc0ac\uc804\uc5d0\uc11c \ub9ac\uc2a4\ud2b8\ub85c \ubcc0\ud658" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {1:'one', 2:'two', 3:'three'}\n", "print d.keys()\n", "print d.values()\n", "print d.items()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3]\n", "['one', 'two', 'three']\n", "[(1, 'one'), (2, 'two'), (3, 'three')]\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ub9ac\uc2a4\ud2b8\uc5d0\uc11c \uc0ac\uc804\uc73c\ub85c \ubcc0\ud658" ] }, { "cell_type": "code", "collapsed": false, "input": [ "keys = ['a', 'b', 'c', 'd']\n", "values = [1, 2, 3, 4]\n", "L = zip(keys, values)\n", "print L\n", "print dict(L)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[('a', 1), ('b', 2), ('c', 3), ('d', 4)]\n", "{'a': 1, 'c': 3, 'b': 2, 'd': 4}\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-6 \ubb38\uc790 \ubcc0\ud658" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print chr(97) # ASCII\ucf54\ub4dc -> \ubb38\uc790\n", "print ord('a') # \ubb38\uc790 -> ASCII\ucf54\ub4dc" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "97\n" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-7 \uc784\uc758\uc758 \uc9c4\uc218\ub97c 10\uc9c4\uc218\ub85c \ubcc0\ud658" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print int('64', 16) # 16\uc9c4\uc218 '64'\ub97c 10\uc9c4\uc218\ub85c\n", "print int('144', 8) # 8\uc9c4\uc218 '144'\ub97c 10\uc9c4\uc218\ub85c\n", "print int('101111', 2) # 2\uc9c4\uc218 '101111'\uc744 10\uc9c4\uc218\ub85c\n", "print int('14', 5) # 5\uc9c4\uc218 '14'\ub97c 10\uc9c4\uc218\ub85c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100\n", "100\n", "47\n", "9\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-8 10\uc9c4\uc218\ub97c \uc784\uc758\uc758 \uc9c4\uc218\ub85c \ubcc0\ud658" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 10\uc9c4\uc218\uc5d0\uc11c 16\uc9c4\uc218/8\uc9c4\uc218\ub85c \ubcc0\ud658 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print hex(100) #10\uc9c4\uc218 100\uc744 16\uc9c4\uc218 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658\n", "print oct(100) #10\uc9c4\uc218 100\uc744 8\uc9c4\uc218 \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0x64\n", "0144\n" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 10\uc9c4\uc218\uc5d0\uc11c 2\uc9c4\uc218\ub85c\n", " - \uae30\uc874\uc5d0 \uc874\uc7ac\ud558\ub294 \ub0b4\uc7a5\ud568\uc218\ub294 \uc5c6\uc74c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"%o\" % 23\n", "#file : int2bin1.py\n", "\n", "octtab = {'0':'000', '1':'001', '2':'010', '3':'011', '4':'100', '5':'101', '6':'110', '7':'111', }\n", "\n", "def bin1(d):\n", " \"integer to binary (string)\"\n", " s = \"%o\" % d # 10\uc9c4\uc218 d\ub97c 8\uc9c4\ubc95 \uc218\uc758 \ubb38\uc790\uc5f4\ub85c \ud3ec\ub9f7\ud305\n", " b = ''\n", " for el in s:\n", " b += octtab[el]\n", " return b\n", "\n", "print bin1(23) # 10\uc9c4\uc218 23\uc744 2\uc9c4\uc218\ub85c " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "27\n", "010111\n" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 10\uc9c4\uc218\uc5d0\uc11c 2\uc9c4\uc218\ub85c\n", " - \ube44\ud2b8 \uc5f0\uc0b0\uc790 \uc0ac\uc6a9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#file : int2bin2.py\n", "\n", "def bin2(n):\n", " result = []\n", " while 1:\n", " result[:0] = [str(n&1)]\n", " n >>= 1\n", " if not n:\n", " break\n", " results = ''.join(result)\n", " return results\n", "\n", "print bin2(23) # 10\uc9c4\uc218 23\uc744 2\uc9c4\uc218\ub85c " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10111\n" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }