{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. 정밀하게 텍스트 포매팅하기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.1\t백분율 기호 연산자(%)를 사용한 포매팅" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25 plus 75 equals 100 .\n" ] } ], "source": [ "a, b, c = 25, 75, 100\n", "print(a, ' plus ', b, ' equals ', c, '.')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25 plus 75 equals 100.\n" ] } ], "source": [ "print(a, ' plus ', b, ' equals ', c, '.', sep='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 문자열 포맷팅 연산자 \\% 사용법\n", " - 값이 1개일 때\n", " > **포맷_문자열 % 값**\n", " \n", " - 값이 여러 개일 때\n", " > **포맷_문자열 % (여러 값들)**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25 plus 75 equals 100.\n" ] } ], "source": [ "print('%d plus %d equals %d.' % (a, b, c))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a number: 100.'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Here is a number: %d.' % 100" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sum is 100.\n" ] } ], "source": [ "n = 25 + 75\n", "fmt_str = 'The sum is %d.'\n", "print(fmt_str % n)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n is 100\n", "n is 100 and m is 200\n" ] } ], "source": [ "n, m = 100, 200\n", "\n", "print('n is %d' % n)\n", "print('n is %d and m is %d' % (n, m))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Answers are 10, 20, and 30.\n" ] } ], "source": [ "tup = 10, 20, 30\n", "print('Answers are %d, %d, and %d.' % tup)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.2\t백분율 기호 (%) 포맷 지시자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 기억해야 할 주요 지시자\n", " - \\%d\n", " - \\%s\n", " - \\%f\n", " - \\%%" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The result is f9.\n" ] } ], "source": [ "h1 = int('e9', 16)\n", "h2 = int('10', 16)\n", "print('The result is %x.' % (h1 + h2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 연산자 우선순위에서 \\%가 사칙연산자보다 높다." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", "\u001B[0;32m/var/folders/fb/55f4mk_d67x6jtxmmtsn_hjh0000gn/T/ipykernel_48057/1543535552.py\u001B[0m in \u001B[0;36m\u001B[0;34m\u001B[0m\n\u001B[0;32m----> 1\u001B[0;31m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m'The result is %x.'\u001B[0m \u001B[0;34m%\u001B[0m \u001B[0mh1\u001B[0m \u001B[0;34m+\u001B[0m \u001B[0mh2\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;31m# 에러!\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", "\u001B[0;31mTypeError\u001B[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "print('The result is %x.' % h1 + h2) \t\t# 에러!" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The result is 0xf9.\n" ] } ], "source": [ "print('The result is 0x%x.' % (h1 + h2))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "We is Moe, Curly, & Larry.\n" ] } ], "source": [ "s = 'We is %s, %s, & %s.' % ('Moe', 'Curly', 'Larry')\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 포멧팅 양식\n", " > **\\%[-][너비][.정밀도]c**\n", " \n", " - 위 양식에서 c는 위 표 5-1에서 열거한 지시자 중 하나\n", " - \\%뒤 마이너스(-) 기호는 왼쪽 정렬을 의미" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is a number: 255 .'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'This is a number: %-6d.' % 255" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is John.\n" ] } ], "source": [ "print('My name is %10s.' % 'John')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Amount is 25.\n", "Amount is 00025.\n", "Amount is 00025.\n" ] } ], "source": [ "print('Amount is %10d.' % 25)\n", "print('Amount is %.5d.' % 25)\n", "print('Amount is %10.5d.' % 25)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result: 3.14000\n", "result: 333.14000\n" ] } ], "source": [ "print('result:%12.5f' % 3.14)\n", "print('result:%12.5f' % 333.14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 정밀도에 따라 반올림 또는 버림이 자동으로 수행됨" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1416\n" ] } ], "source": [ "print('%.4f' % 3.141592)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14\n" ] } ], "source": [ "print('%.2f' % 3.141592)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \\%s: 객체를 가장 쉽게 표현할 수 있는 문자열\n", "- \\%r: 객체의 표준(canonical) 문자열" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 숫자에 대한 %s, %r 포멧팅은 동일" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The number is 10.'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'The number is %s.' % 10" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The number is 10.'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'The number is %r.' % 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 문자열에 대한 %s, %r 포멧팅은 상이" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is 'Sam'.\n" ] } ], "source": [ "print('My name is %r.' % 'Sam')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Sam.\n" ] } ], "source": [ "print('My name is %s.' % 'Sam')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "대한민국\n" ] } ], "source": [ "print(str('대한민국'))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'대한민국'\n" ] } ], "source": [ "print(repr('대한민국'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.3\t백분율 기호 (%) 변수-너비 출력 필드 - [생략]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a number: 6'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Here is a number: %*d' % (3, 6)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a number: VI'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Here is a number: %*s' % (3, 'VI')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Item 1: Bob, Item 2: Suzanne'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Item 1: %*s, Item 2: %*s' % (8, 'Bob', 8, 'Suzanne')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Item 1: Bob, Item 2: Suzanne'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 8\n", "'Item 1: %*s, Item 2: %*s' % (n, 'Bob', n, 'Suzanne')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Item 1: 'Bob', Item 2: 'Suzanne'\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 9\n", "'Item 1: %*r, Item 2: %*r' % (n, 'Bob', n, 'Suzanne')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.142'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%*.*f' % (8, 3, 3.141592)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.4\t전역 'format' 함수" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1,000,000,000,000'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "big_n = 10 ** 12 \n", "format(big_n, ',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 전역 format 함수 사용 방법\n", " > **format(데이터, 사양/양식/포멧 지시자/포멧 명세)**\n", "
\n", "- 사양/양식/포멧 지시자/포멧 명세\n", " > **[너비][,][.정밀도][타입_문자]**\n", " \n", " - 너비: 최소 길이를 나타냄\n", " - 쉼표 기호(,): 천 단위 위치 구분자\n", " - 정밀도: 출력할 부동소수점 숫자의 자리수\n", " - 타입 문자:\n", " - d: 정수\n", " - s: 문자열\n", " - f: 부동 소수\n", " \n", "- 사실은 다음이 더 완벽한 명세 \n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 쉼표(,)\n", " - 숫자 데이터에만 사용\n", " - 문자 데이터에 사용하면 예외 발생" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "150,000,000\n" ] } ], "source": [ "n = 150000000\n", "print(format(n, ','))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Cannot specify ',' with 's'.", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mValueError\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[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mformat\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m\"대한민국\"\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0;34m','\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", "\u001B[0;31mValueError\u001B[0m: Cannot specify ',' with 's'." ] } ], "source": [ "print(format(\"대한민국\", ','))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 너비\n", " - 기본 자리 맞춤(justification)\n", " - 숫자 데이터: 오른쪽 자리 맞춤\n", " - 문자 데이터: 왼쪽 자리 맞춤\n", " - 너비에 지정된 수치보다 데이터가 더 길면 너비는 자동으로 증가됨 " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Bob '" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format('Bob', '10')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Suzie '" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format('Suzie', '7')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 150'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(150, '8')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 99'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(99, '5')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'대한민국'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(\"대한민국\", '2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 너비.정밀도\n", " - 정밀도\n", " - 데이터가 일반 숫자인 경우\n", " - 전체 숫자 개수\n", " - 타입_문자 f와 함께 사용\n", " - 소수점 오른쪽에 출력할 숫자의 고정된 자리수 의미\n", " - 데이터가 문자열인 경우\n", " - 전체 문자열의 개수" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Bob '" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format('Bobby K.', '6.3')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.14'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(3.141592, '6.3')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.142'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(3.141592, '6.3f')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.142'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(3.141592, '9.3f')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 100.700'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(100.7, '9.3f')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.5\tformat 메서드 소개" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25 plus 75 equals 100.\n" ] } ], "source": [ "print('{} plus {} equals {}.'.format(25, 75, 100))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pythagoras said, I want 2 slices of 3.141592.\n" ] } ], "source": [ "fss = '{} said, I want {} slices of {}.'\n", "\n", "name = 'Pythagoras'\n", "pi = 3.141592\n", "print(fss.format(name, 2, pi))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set = {1, 2}\n" ] } ], "source": [ "print('Set = {{{}, {}}}'.format(1, 2))" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set = { 15, 35, 25 }\n" ] } ], "source": [ "fss = 'Set = {{ {}, {}, {} }}'\n", "print(fss.format(15, 35, 25))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set = { 15, 35, 25 }\n" ] } ], "source": [ "print('Set = {{ {}, {}, {} }}'.format(15, 35, 25))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.6\t위치로 순서 정하기 (이름 혹은 색인)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10; 20; 30!\n" ] } ], "source": [ "print('{}; {}; {}!'.format(10, 20, 30))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10; 20; 30!\n" ] } ], "source": [ "print('{0}; {1}; {2}!'.format(10, 20, 30))" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The items are 30, 20, 10.\n" ] } ], "source": [ "print('The items are {2}, {1}, {0}.'.format(10, 20, 30))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The items are 40, 20, 10.\n" ] } ], "source": [ "fss = 'The items are {3}, {1}, {0}.'\n", "print(fss.format(10, 20, 30, 40))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a equals 10, b equals 50, c equals 100.\n" ] } ], "source": [ "fss = 'a equals {a}, b equals {b}, c equals {c}.'\n", "print(fss.format(a=10, c=100, b=50))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100, 100, 200, 200\n" ] } ], "source": [ "print('{0}, {0}, {1}, {1}'.format(100, 200))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "한국어를 원하면 KRW를 입력하세요:KRW\n", "Fred가 Joe에서 Sam을 만나는 게 언제지?\n" ] } ], "source": [ "current_lang = input('한국어를 원하면 KRW를 입력하세요:')\n", "\n", "if current_lang == 'KRW':\n", " fss = '{0}가 {2}에서 {1}을 만나는 게 언제지?'\n", "else:\n", " fss = \"When will {0} meet {1} at {2}'s?\"\n", "print(fss.format('Fred', 'Sam', 'Joe'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 아래 내용은 생략" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'200, 300'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_list = [100, 200, 300]\n", "'{0[1]:}, {0[2]:}'.format(a_list)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'200, 300'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{a[1]:}, {a[2]:}'.format(a=a_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 문자열 내부의 포멧 문자열 양식\n", " > **{ [위치] [!r|s|a] [:사양] }**\n", " \n", " - !s: str()에 대응 - 일반적 문자열\n", " - !r: repr()에 대응 - 표준 문자열\n", " - !a: 아스키코드(ASCII)로만 이루어진 문자열\n", " - 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.7\t'repr' vs 문자열 변환" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "10\n" ] } ], "source": [ "print(10) \t\t# 10 출력\n", "print(str(10)) \t# 동일한 결과!" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print(repr(10)) \t\t# 이 출력 결과도 10이다." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here is a \n", " newline! \n" ] } ], "source": [ "test_str = 'Here is a \\n newline! '\n", "print(test_str)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Here is a \\n newline! '\n" ] } ], "source": [ "print(repr(test_str))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here is a \n", " newline! \n" ] } ], "source": [ "print('{}'.format(test_str))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Here is a \\n newline! '\n" ] } ], "source": [ "print('{!r}'.format(test_str))" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'ChaCha' loves 'Joanie'\n" ] } ], "source": [ "print('{1!r} loves {0!r}'.format('Joanie', 'ChaCha'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.8\t'format' 함수와 메서드의 'spec' 필드" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.1\t 출력-필드 너비 " ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "** 777**999**\n" ] } ], "source": [ "n1, n2 = 777, 999\n", "print('**{:10}**{:2}**'.format(n1, n2))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 111\n", " 10\n", " 2001\n", " 2\n", " 55\n", " 144\n", " 2525\n", " 1984\n" ] } ], "source": [ "n_lst = [111, 10, 2001, 2, 55, 144, 2525, 1984]\n", "\n", "for n in n_lst:\n", " print('{:5}'.format(n))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'ChaCha' loves 'Joanie' !!\n" ] } ], "source": [ "fss = '{1!r:10} loves {0!r:10}!!'\n", "print(fss.format('Joanie', 'ChaCha'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.2 텍스트 조정: '채우기'와 '자리 맞춤' 문자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----Hey Bill G, pick me!\n" ] } ], "source": [ "print('{:->24}'.format('Hey Bill G, pick me!'))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Tom\n", "@@@Lady\n", "***Bill\n" ] } ], "source": [ "print('{:>7}'.format('Tom')) \t\t# ' Tom' 출력\n", "print('{:@>7}'.format('Lady')) \t\t# '@@@Lady' 출력\n", "print('{:*>7}'.format('Bill')) \t\t# '***Bill' 출력" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tom \n", "Lady@@@\n", "Bill***\n" ] } ], "source": [ "print('{:<7}'.format('Tom')) \t\t# 'Tom ' 출력\n", "print('{:@<7}'.format('Lady')) \t\t# 'Lady@@@' 출력\n", "print('{:*<7}'.format('Bill')) \t\t# 'Bill***' 출력" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Tom Jones\n", "@@@Lady@@@\n", "***Bill***\n" ] } ], "source": [ "fss = '{:^10}Jones'\n", "print(fss.format('Tom')) \t\t# ' Tom Jones' 출력\n", "fss = '{:@^10}'\n", "print(fss.format('Lady')) \t\t# '@@@Lady@@@' 출력\n", "fss = '{:*^10}'\n", "print(fss.format('Bill')) \t\t# '***Bill***' 출력" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- 1250\n", "-0001250\n" ] } ], "source": [ "print('{:=8}'.format(-1250)) \t\t# '- 1250' 출력\n", "print('{:0=8}'.format(-1250)) \t\t# '-0001250' 출력" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lady@@@\n" ] } ], "source": [ "print(format('Lady', '@<7')) \t\t# 'Lady@@@' 출력" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lady@@@\n" ] } ], "source": [ "print('{:@<7}'.format('Lady')) \t\t# 'Lady@@@' 출력" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.3\t '기호' 문자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "results> 25, +25, 25\n" ] } ], "source": [ "print('results> {:}, {:+}, {:-}'.format(25, 25, 25))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "results> -25, -25, -25\n" ] } ], "source": [ "print('results> {:}, {:+}, {:-}'.format(-25, -25, -25))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.4\t 0으로 시작하는 문자(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 0을 사용하여 부족한 자리를 채운다.\n", "- 자리 맞춤앞의 채우기와 유사한 기능" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0000125 0000025156\n" ] } ], "source": [ "i, j = 125, 25156\n", "print('{:07} {:010}'.format(i, j))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00000375\n" ] } ], "source": [ "print('{:08}'.format(375))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0000125 0000025156\n" ] } ], "source": [ "i, j = 125, 25156\n", "print('{:0>7} {:0>10}'.format(i, j))" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0000000+25 +000000025\n" ] } ], "source": [ "print('{:0>+10} {:+010}'.format(25, 25))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.5\t천 단위 위치 구분자" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The USA owes 21,000,000,000 dollars.\n", "The sun is 93,000,000 miles away.\n" ] } ], "source": [ "fss1 = 'The USA owes {:,} dollars.'\n", "print(fss1.format(21000000000))\n", "fss2 = 'The sun is {:,} miles away.'\n", "print(fss2.format(93000000))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The amount on the check was $***4,500,000\n" ] } ], "source": [ "n = 4500000\n", "print('The amount on the check was ${:*>12,}'.format(n))" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The amount is 000,013,000\n" ] } ], "source": [ "print('The amount is {:011,}'.format(13000))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The amount is 0,000,013,000\n" ] } ], "source": [ "n = 13000\n", "print('The amount is {:012,}'.format(n))" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The amount is 0000013,000\n" ] } ], "source": [ "print('The amount is {:0>11,}'.format(n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.6\t 정밀도 제어" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1 + 1.6 = 4.8\n" ] } ], "source": [ "pi = 3.14159265\n", "phi = 1.618\n", "\n", "fss = '{:.2} + {:.2} = {:.2}'\n", "print(fss.format(pi, phi, pi + phi))" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14 + 1.62 = 4.76\n" ] } ], "source": [ "pi = 3.14159265\n", "phi = 1.618\n", "\n", "fss = '{:.3} + {:.3} = {:.3}'\n", "print(fss.format(pi, phi, pi + phi))" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.142 + 1.618 = 4.760\n" ] } ], "source": [ "pi = 3.14159265\n", "phi = 1.618\n", "\n", "fss = '{:.3f} + {:.3f} = {:.3f}'\n", "print(fss.format(pi, phi, pi + phi))" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 22.100\n", " 1000.007\n" ] } ], "source": [ "fss = ' {:10.3f}\\n {:10.3f}'\n", "print(fss.format(22.1, 1000.007))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 22,333.100\n", " 1,000.007\n" ] } ], "source": [ "fss = ' {:10,.3f}\\n {:10,.3f}'\n", "print(fss.format(22333.1, 1000.007))" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 22.70\n", " 3.14\n", " 555.50\n", " 29.00\n", " 1010.01\n" ] } ], "source": [ "fss = ' {:10.2f}'\n", "for x in [22.7, 3.1415, 555.5, 29, 1010.013]:\n", " print(fss.format(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.7 문자열에서 사용한 '정밀도(잘라 내기)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Super\n", "Excel\n", "Sam\n" ] } ], "source": [ "print('{:.5}'.format('Superannuated.')) # 'Super' 출력\n", "print('{:.5}'.format('Excellent!')) # 'Excel' 출력\n", "print('{:.5}'.format('Sam')) # 'Sam' 출력" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tom***\n", "Mike**\n", "Rodney\n", "Hannib\n", "Mortim\n" ] } ], "source": [ "fss = '{:*<6.6}'\n", "\n", "print(fss.format('Tom'))\n", "print(fss.format('Mike'))\n", "print(fss.format('Rodney'))\n", "print(fss.format('Hannibal'))\n", "print(fss.format('Mortimer'))" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tom***\n", "Mike**\n", "Rodney\n", "Hannib\n", "Mortim\n" ] } ], "source": [ "print(format('Tom', '*<6.6'))\n", "print(format('Mike', '*<6.6'))\n", "print(format('Rodney', '*<6.6'))\n", "print(format('Hannibal', '*<6.6'))\n", "print(format('Mortimer', '*<6.6'))" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tom**\n", "Rodney\n", "longer tha\n" ] } ], "source": [ "fss = '{:*<5.10}'\n", "\n", "print(fss.format('Tom'))\n", "print(fss.format('Rodney'))\n", "print(fss.format('longer than 10'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.8\t '타입' 지시자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 사양\n", " > **[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.9\t 이진수 출력하기" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "101 110 10000\n" ] } ], "source": [ "print('{:b} {:b} {:b}'.format(5, 6, 16))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- #: 기수 접두사 --> 0b, 0o, 0x" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0b111\n" ] } ], "source": [ "print('{:#b}'.format(7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.10\t8진수와 16진수 출력하기" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "77, 3f, 3F\n" ] } ], "source": [ "print('{:o}, {:x}, {:X}'.format(63, 63, 63))" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "77, 3f, 3F\n" ] } ], "source": [ "print('{0:o}, {0:x}, {0:X}'.format(63))" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0o77, 0x3f, 0X3F\n" ] } ], "source": [ "print('{0:#o}, {0:#x}, {0:#X}'.format(63))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.11\t백분율 출력하기" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You own 51.700000% of the shares.\n" ] } ], "source": [ "print('You own {:%} of the shares.'.format(.517))" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You own 51.700000% of the shares.\n" ] } ], "source": [ "print('You own {:8%} of the shares.'.format(.517))" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23.10% of 50.00% of 40...\n" ] } ], "source": [ "print('{:.2%} of {:.2%} of 40...'.format(0.231, 0.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.8.12\t이진수 예시" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter values in binary only!\n", "Enter b1:101\n", "Enter b2:1010\n", "Total is: 0b1111\n", "5 + 10 = 15\n" ] } ], "source": [ "def calc_binary():\n", " print('Enter values in binary only!')\n", " b1 = int(input('Enter b1:'), 2)\n", " b2 = int(input('Enter b2:'), 2)\n", " print('Total is: {:#b}'.format(b1 + b2))\n", " print('{} + {} = {}'.format(b1, b2, b1 + b2))\n", "\n", "calc_binary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.9\t변수-길이 필드 - [생략]" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a num: 1.234'" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Here is a num: {:{}.{}}'.format(1.2345, 10, 4)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a num: 1.234'" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Here is a num: {:10.4}'.format(1.2345)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Here is a num: 1.234'" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = 10, 4\n", "'Here is a num: {:{}.{}}'.format(1.2345, a, b)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hi there !'" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{:{}} {:{}}!'.format('Hi', 3, 'there', 7)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hi there !'" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{0:{1}} {2:{3}}!'.format('Hi', 3, 'there', 7)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pi is approx. 3.142'" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Pi is approx. {0:{1}.{2}f}'.format(3.141592, 8, 3)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pi is approx. 3.142'" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = 8, 3\n", "'Pi is approx. {0:{1}.{2}f}'.format(3.141592, a, b)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pi is approx. 3.142'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Pi is approx. {0:8.3f}'.format(3.141592)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## 5.10 정리해보자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 구구단 포맷팅" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 x 1 = 2\n", "2 x 2 = 4\n", "2 x 3 = 6\n", "2 x 4 = 8\n", "2 x 5 = 10\n", "2 x 6 = 12\n", "2 x 7 = 14\n", "2 x 8 = 16\n", "2 x 9 = 18\n", "\n", "3 x 1 = 3\n", "3 x 2 = 6\n", "3 x 3 = 9\n", "3 x 4 = 12\n", "3 x 5 = 15\n", "3 x 6 = 18\n", "3 x 7 = 21\n", "3 x 8 = 24\n", "3 x 9 = 27\n", "\n", "4 x 1 = 4\n", "4 x 2 = 8\n", "4 x 3 = 12\n", "4 x 4 = 16\n", "4 x 5 = 20\n", "4 x 6 = 24\n", "4 x 7 = 28\n", "4 x 8 = 32\n", "4 x 9 = 36\n", "\n", "5 x 1 = 5\n", "5 x 2 = 10\n", "5 x 3 = 15\n", "5 x 4 = 20\n", "5 x 5 = 25\n", "5 x 6 = 30\n", "5 x 7 = 35\n", "5 x 8 = 40\n", "5 x 9 = 45\n", "\n", "6 x 1 = 6\n", "6 x 2 = 12\n", "6 x 3 = 18\n", "6 x 4 = 24\n", "6 x 5 = 30\n", "6 x 6 = 36\n", "6 x 7 = 42\n", "6 x 8 = 48\n", "6 x 9 = 54\n", "\n", "7 x 1 = 7\n", "7 x 2 = 14\n", "7 x 3 = 21\n", "7 x 4 = 28\n", "7 x 5 = 35\n", "7 x 6 = 42\n", "7 x 7 = 49\n", "7 x 8 = 56\n", "7 x 9 = 63\n", "\n", "8 x 1 = 8\n", "8 x 2 = 16\n", "8 x 3 = 24\n", "8 x 4 = 32\n", "8 x 5 = 40\n", "8 x 6 = 48\n", "8 x 7 = 56\n", "8 x 8 = 64\n", "8 x 9 = 72\n", "\n", "9 x 1 = 9\n", "9 x 2 = 18\n", "9 x 3 = 27\n", "9 x 4 = 36\n", "9 x 5 = 45\n", "9 x 6 = 54\n", "9 x 7 = 63\n", "9 x 8 = 72\n", "9 x 9 = 81\n", "\n" ] } ], "source": [ "for i in range(2, 10):\n", " for j in range(1, 10):\n", " print(\"{0} x {1} = {2:2d}\".format(i, j, i*j))\n", " print()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 inch = 0.0 centi\n", "1 inch = 2.54 centi\n", "2 inch = 5.08 centi\n", "3 inch = 7.62 centi\n", "4 inch = 10.16 centi\n", "5 inch = 12.7 centi\n", "6 inch = 15.24 centi\n", "7 inch = 17.78 centi\n", "8 inch = 20.32 centi\n", "9 inch = 22.86 centi\n" ] } ], "source": [ "for el in range(10):\n", " print(el, 'inch =', el * 2.54, 'centi')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 inch = 0.00 centi\n", "1 inch = 2.54 centi\n", "2 inch = 5.08 centi\n", "3 inch = 7.62 centi\n", "4 inch = 10.16 centi\n", "5 inch = 12.70 centi\n", "6 inch = 15.24 centi\n", "7 inch = 17.78 centi\n", "8 inch = 20.32 centi\n", "9 inch = 22.86 centi\n" ] } ], "source": [ "for el in range(10):\n", " print(\"%d inch = %.2f centi\" % (el, el * 2.54))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 inch = 0.00 centi\n", "1 inch = 2.54 centi\n", "2 inch = 5.08 centi\n", "3 inch = 7.62 centi\n", "4 inch = 10.16 centi\n", "5 inch = 12.70 centi\n", "6 inch = 15.24 centi\n", "7 inch = 17.78 centi\n", "8 inch = 20.32 centi\n", "9 inch = 22.86 centi\n" ] } ], "source": [ "for el in range(10):\n", " print(\"{0} inch = {1:.2f} centi\".format(el, el * 2.54))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.11 복습 문제" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.12 실습 문제" ] } ], "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 }