{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A.\t파이썬 연산자 우선순위 표" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(None and 100)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "print(None or 100)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(not(''))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# B.\t내장 파이썬 함수" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## abs(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 3+4j\n", "abs(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## all(iterable)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all([1, 2, 4])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all([1, 2, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## any(iterable)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "any([0, 2, 0])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "any([0, 0, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## bin(n)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b111'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## bool(obj)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Blah():\n", " pass\n", "\n", "b = Blah()\n", "b.egg = 0\n", "bool(b)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(b.egg)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## bytes(source, encoding)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'Hi there!'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bs = bytes('Hi there!', encoding='utf-8')\n", "bs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## chr(n)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(97)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## compile(cmd_str, filename, mode_str, flags=0, dont_inherit=False, optimize=–1)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pi/2 = 1.570796\n" ] } ], "source": [ "command_str = '''\n", "pi = 3.141592\n", "print('pi/2 = ', pi/2)\n", "'''\n", "cmd = compile(command_str, 'my_mod', 'exec')\n", "exec(cmd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## complex(real=0, imag=0)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0j" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5.5+0j)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex(5.5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3-2.1j)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex(3, -2.1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10-3.5j)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex(10-3.5j)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-4+0j)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex(1, 5j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## complex(complex_str)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10.2+5j)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex('10.2+5j')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10.2+5j)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex('(10.2+5j)')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2+0j)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex('2')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3j" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex('3j')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1+1j)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex('1+j')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## delattr(obj, name_str)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "St Bernard\n", "True\n", "False\n" ] }, { "ename": "AttributeError", "evalue": "breed", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mdelattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dog\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma_str\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dog\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma_str\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mdelattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dog\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma_str\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: breed" ] } ], "source": [ "class Dog:\n", " bread = ''\n", " \n", "my_dog = Dog()\n", "my_dog.breed = 'St Bernard'\n", "print(my_dog.breed)\n", "\n", "a_str = 'breed'\n", "print(hasattr(my_dog, a_str))\n", "delattr(my_dog, a_str)\n", "print(hasattr(my_dog, a_str))\n", "delattr(my_dog, a_str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## dir([obj])" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['In',\n", " 'Out',\n", " '_',\n", " '__',\n", " '___',\n", " '__builtin__',\n", " '__builtins__',\n", " '__doc__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " '_dh',\n", " '_i',\n", " '_i1',\n", " '_ih',\n", " '_ii',\n", " '_iii',\n", " '_oh',\n", " 'exit',\n", " 'get_ipython',\n", " 'quit']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['i', 'j']\n" ] } ], "source": [ "def demo():\n", " i = 1\n", " j = 2\n", " print(dir())\n", "\n", "demo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']\n" ] } ], "source": [ "print(dir(int))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## divmod(a, b)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20 3\n" ] } ], "source": [ "quot, rem = divmod(203, 10) \t# 결과는 20, 나머지는 3\n", "print(quot, rem)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(21.0, 0.09999999999999964)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(10.6, 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## enumerate(iterable, start=0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. John\n", "2. Paul\n", "3. George\n", "4. Ringo\n" ] } ], "source": [ "beat_list = ['John', 'Paul', 'George', 'Ringo']\n", "for n, item in enumerate(beat_list, 1):\n", " print('%s. %s' % (n, item))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## eval(expr_str [, globals [, locals]] )" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "355" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 100\n", "eval('3 * a + 55')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "355" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('3 * 100 + 55', {}) \t\t# 문제 없음" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "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\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'3 * a + 55'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 에러; 'a'가 정의되지 않음\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "source": [ "eval('3 * a + 55', {}) \t\t# 에러; 'a'가 정의되지 않음" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "466.3076581549986" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import *\n", "a = 25\n", "dict_1 = {'tan': tan, 'radians': radians, 'a': a }\n", "eval('1000 * tan(radians(a))', dict_1)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "725" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('a * a + 100', {}, locals())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## filter(function, iterable)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-1, -3, -2.5]\n" ] } ], "source": [ "def is_neg(x):\n", " return x < 0\n", "\n", "my_list = [10, -1, 20, -3, -2.5, 30]\n", "print(list(filter(is_neg, my_list)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## float([x])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0 -230.0\n" ] } ], "source": [ "n = 1\n", "yyy = float(n) \t\t\t# yyy에 1.0 대입.\n", "amt = float('-23E01') \t\t# amt에 -23E01 대입.\n", "print(yyy, amt)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "amt = float() \t\t\t# amt에 0.0 대입.\n", "amt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## format(obj, [format_spec])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1,000,000'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(1000000, ',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## frozenset([iterable])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "frozenset({1, 2, 3, 4})" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "frozenset((1, 2, 2, 3, 3, 4, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## getattr(obj, name_str [,default])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Bulldog'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Dog:\n", " breed = ''\n", " \n", "d = Dog()\n", "d.breed = 'Bulldog'\n", "attr_name = 'breed'\n", "getattr(d, attr_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## hasattr(obj, name_str)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Dog:\n", " breed = ''\n", " \n", "my_dog = Dog()\n", "my_dog.breed = 'Husky'\n", "nm = 'breed'\n", "hasattr(my_dog, nm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## hex(n)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x17'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(23)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## input([prompt_str])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your name, please: insuk\n", "Enter your age, please: 40\n" ] } ], "source": [ "my_name = input('Enter your name, please: ')\n", "my_age = int(input('Enter your age, please: '))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## int(x, base=10)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4096" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('1000', 16)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, -3)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.99), int(-3.99)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## issubclass(class1, class2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Floopy(int):\n", " pass\n", "\n", "f = Floopy()\n", "\n", "issubclass(Floopy, int)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(int, Floopy)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(int, int)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "issubclass() arg 1 must be a class", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0missubclass\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# TypeError: f 는 클래스가 아니다\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: issubclass() arg 1 must be a class" ] } ], "source": [ "issubclass(f, int) \t# TypeError: f 는 클래스가 아니다" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## iter(obj)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'int' object is not iterable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgen\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0miter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# TypeError 발생\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable" ] } ], "source": [ "gen = iter(5) \t\t# TypeError 발생" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen = iter([5])\n", "gen" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen = iter([1, 2, 3])\n", "next(gen)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(gen)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(gen)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "next(gen)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## len(sequence)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('Hello')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list([iterable])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['c', 'a', 't']\n" ] } ], "source": [ "print(list('cat'))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list = list() \t\t# new_list를 빈 리스트로 초기화.\n", "new_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## locals()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'a': 2, 'b': 3, 'c': 1}\n" ] } ], "source": [ "def foobar():\n", " a = 2\n", " b = 3\n", " c = 1\n", " print(locals())\n", "\n", "foobar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## map(function, iterable1 [, iterable2…])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 20000]\n" ] } ], "source": [ "def multy(a, b, c):\n", " return a * b * c\n", "\n", "m = map(multy, [1, 20], [1, 20], [1, 50])\n", "print(list(m))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## max(arg1 [, arg2]…)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.25" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(1, 3.0, -100, 5.25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## max(iterable)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(5, 2)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from fractions import Fraction\n", "\n", "a_list = [1, Fraction('5/2'), 2.1]\n", "max(a_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## min(arg1 [, arg2]…)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-100" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(1, 3.0, -100, 5.25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## min(iterable)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from fractions import Fraction\n", "\n", "a_list = [1, Fraction('5/2'), 2.1]\n", "min(a_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## oct(n)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0o11'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oct(9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## open(file_name_str, mode='rt')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "f = open('mydata.dat', 'wb')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ord(char_str)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(ord('a'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## pow(x, y [, z])" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(2, 4)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(2, 4, 10)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13780.61233982238" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(1.1, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## print(objects, sep='', end='\\n', file=sys.stdout)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eenie; meenie; Moe\n" ] } ], "source": [ "s1 = 'eenie'\n", "s2 = 'meenie'\n", "s3 = 'Moe'\n", "print(s1, s2, s3, sep='; ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## range(n)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## range(start, stop [, step])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(1, 7, 2))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 4, 3, 2]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ " list(range(5, 1, -1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## repr(obj)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Hi, I'm Brian!\"\n" ] } ], "source": [ "my_str = \"Hi, I'm Brian!\"\n", "print(repr(my_str))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Hi, I'm Brian!\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_str" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## reversed(iterable)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 2, 1]\n" ] } ], "source": [ "print(list(reversed([1, 2, 3])))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(reversed('Wow, Bob, wow!'))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'!wow ,boB ,woW'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Wow, Bob, wow!'[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## round(x [,ndigits])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12.6" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.555, 1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12.55" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.555, 2)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.555, 0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.555)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.555, -1)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-56" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(-55.55)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## set([iterable])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 5, 11}" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = [11, 11, 3, 5, 3, 3, 3]\n", "my_set = set(my_list)\n", "my_set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## setattr(obj, name_str, value)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "class Dog:\n", " breed = ''\n", "\n", "d = Dog()\n", "d.breed = 'Dane'" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Dane'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "attr_str = 'breed'\n", "setattr(d, attr_str, 'Dane') \t\t# d.breed = 'Dane' 설정\n", "d.breed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## sorted(iterable [, key] [, reverse])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 5, 7, 10]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted([5, 0, 10, 7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## str(obj='')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 10100140\n", "s = str(n)\n", "s.count('0')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## str(obj=b'' [, encoding='utf-8'])" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello!\n" ] } ], "source": [ "bs = b'Hello!' \t\t\t# 정확하게 6 바이트를 보장한다\n", "s = str(bs, encoding='utf-8')\n", "print(s) \t\t\t# 일반 문자열 출력, 문자당 ? 바이트." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## sum(iterable [, start])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_avg(a_list):\n", " return sum(a_list)/len(a_list)\n", "\n", "get_avg([1, 2, 3, 4, 10])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def gen_count(n):\n", " i = 1\n", " while i <= n:\n", " yield i\n", " i += 1\n", "\n", "sum(gen_count(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## type(obj)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 5\n", "type(i) is int" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i) == int" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## zip(*iterables)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 22, 33]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3]\n", "b = [10, 20, 30]\n", "c = [i[0] + i[1] for i in zip(a, b)]\n", "c" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 10), (2, 20), (3, 30)]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_list = list(zip(a, b))\n", "a_list" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 22, 33]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3]\n", "b = [10, 20, 30]\n", "c = []\n", "\n", "for i in range(min(len(a), len(b))):\n", " c.append(a[i] + b[i])\n", " \n", "c" ] }, { "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.6.8" } }, "nbformat": 4, "nbformat_minor": 4 }