{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 2. 파이썬 언어의 기본문형\n", "***\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 1 파이썬 예약어 및 내장 함수\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-1 예약어 (Reserved Words)\n", "- 예약어 (또는 키워드)\n", " - 파이썬에서 이미 문법적인 용도로 사용되고 있기 때문에 변수명등의 식별자로 사용하면 안되는 단어들" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-2 예약어의 종류 알아보기" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n", "\n", "33\n" ] } ], "source": [ "import keyword\n", "\n", "print(keyword.kwlist)\n", "print()\n", "print(len(keyword.kwlist))\n", "\n", "# python3에 추가됨 : 'False', 'None', 'True', 'nonlocal' \n", "# python3에서는 제거됨 : 'exec', 'print'\n", "#\n", "# Python2 : \n", "# ['and', 'as', 'assert', 'break', 'class', \n", "# 'continue', 'def', 'del', 'elif', 'else', \n", "# 'except', 'exec', 'finally', 'for', 'from', \n", "# 'global', 'if', 'import', 'in', 'is', 'lambda', \n", "# 'not', 'or', 'pass', 'print', 'raise', 'return', \n", "# 'try', 'while', 'with', 'yield']\n", "#\n", "# 31" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-3 내장 함수 (Built-in Function)\n", "- 별도의 모듈(Module)의 추가 없이 기본적으로 제공되는 함수들\n", "- 참고 사이트\n", " 1. 내장(Built-in) 함수: https://docs.python.org/3.6/library/functions.html\n", "- 대표적인 내장 함수\n", " - abs, max, min, pow, chr, str, range, type, ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- abs(x)\n", " - 수치형 자료 x에 대해 x의 절대값을 반환하는 함수" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "3\n" ] } ], "source": [ "print(abs(3))\n", "print(abs(-3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- max(s)\n", " - 시퀀스 자료형(문자열, 리스트, 튜플)을 입력받아 그 자료가 지닌 원소 중 최대값을 반환하는 함수" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "y\n" ] } ], "source": [ "print(max(1, 2))\n", "print(max([1, 2, 3]))\n", "print(max(\"python\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- min(s)\n", " - 시퀀스 자료형(문자열, 리스트, 튜플)을 입력받아 그 자료가 지닌 원소 중 최소값을 반환하는 함수" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "1\n", "h\n" ] } ], "source": [ "print(min(1, 2))\n", "print(min([1, 2, 3]))\n", "print(min(\"python\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- pow(x,y)\n", " - 수치형 자료형 x, y에 대해 x의 y승을 반환하는 함수" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n", "27\n", "0.5\n" ] } ], "source": [ "print(pow(2, 4))\n", "print(pow(3, 3))\n", "print(pow(2, -1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- chr(i)\n", " - 정수 형태의 Unicode값을 입력으로 받아 그에 해당하는 문자를 반환하는 함수\n", " - 인수 i의 범위: 0부터 0x10ffff (Decimal표현으로 1114111) 까지" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "A\n", "0\n" ] } ], "source": [ "print(chr(97))\n", "print(chr(65))\n", "print(chr(48))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- str(object)\n", "
str(object='') -> str
\n", " - 임의의 객체 object에 대해 해당 객체를 표현하는 문자열을 반환하는 함수\n", " - Create a new string object from the given object." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "[1, 2]\n" ] } ], "source": [ "print(str(3))\n", "print(str([1, 2]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- range\n", "
 \n",
    "  range(stop) -> range object\n",
    "range(start, stop[, step]) -> range object 
\n", " - 수치형 자료형으로 start, stop, step 등을 입력받아 start부터 stop까지의 정수값들이 순차적으로 들어 있는 range object를 리스트로 반환하는 함수\n", " - Python2에서는 range()의 결과로 곧바로 리스트가 반환됨\n", " - 인수가 하나(stop)인 경우\n", " - 0(디폴트 값)부터 stop-1까지의 정수 리스트를 반환한다.\n", " - 인수가 두 개(start, stop)인 경우\n", " - start부터 stop-1까지의 정수 리스트를 반환한다.\n", " - 인수가 세 개(start, stop, step)인 경우\n", " - start부터 stop-1까지의 정수를 반환하되 각 정수 사이의 거리가 step인 것들만 반환한다.\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "range(0, 10)\n", "range(3, 10)\n", "range(3, 10, 2)\n", "\n", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[3, 4, 5, 6, 7, 8, 9]\n", "[3, 5, 7, 9]\n" ] } ], "source": [ "print(range(10))\n", "print(range(3, 10))\n", "print(range(3, 10, 2))\n", "# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "# [3, 4, 5, 6, 7, 8, 9]\n", "# [3, 5, 7, 9]\n", "print()\n", "\n", "print(list(range(10)))\n", "print(list(range(3, 10)))\n", "print(list(range(3, 10, 2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- type(object)\n", " - 임의의 객체 object의 자료형을 반환하는 함수" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n" ] } ], "source": [ "print(type(-1))\n", "print(type('abc'))\n", "print(type([1, 2, 3]))\n", "# \n", "# \n", "# \n", "# type 에서 class로 바뀜" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 2 파이썬 식별자와 변수 사용\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-1 식별자 만드는 법" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 파이썬 식별자는 변수, 함수, 모듈, 클래스 또는 객체를 식별하는데 사용되는 이름이다.\n", "- 식별자의 조건\n", " - 대소문자 구별함\n", " - 식별자는 문자 A~Z 또는 a~z과 언더바(\\_)로 시작할 수 있다. \n", " - 식별자 첫 시작을 제외하고 식별자 내에 숫자(0~9)를 사용할 수 있다.\n", " - 특수문자 @, $, % 등은 식별자의 어느 자리에도 올 수 없다.\n", " \n", " - 예를 들어 다음과 같은 것은 식별자가 될 수 없음\n", " - 1abc, @file, %x, a%a, a$a, a@a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-2 변수명 만들 때 조심할 점" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 예약어, 내장함수, 모듈 이름을 변수명으로 만드는 일이 없도록 할것" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12345\n" ] } ], "source": [ "print(str(12345))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object is not callable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\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[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object is not callable" ] } ], "source": [ "str = 'abc'\n", "\n", "print(str(12345))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-3 변수의 생성 및 사용\n", "- 파이썬에서 변수가 생성되는 시점은 해당 변수에 임의의 값이 할당될 때이다." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "a = 1\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 변수의 생성 없이 곧바로 사용할 수 없다." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'b' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'b' is not defined" ] } ], "source": [ "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-4 변수의 삭제\n", "- del 이라는 예약어 사용" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "ename": "NameError", "evalue": "name 'b' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'b' is not defined" ] } ], "source": [ "b = 2\n", "print(b)\n", "\n", "del b\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 3 파이썬 기초 문형\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-1 주석문" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#이것은 주석입니다.\n", "import sys #이것도 주석입니다." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-2 연속라인" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "connected lines\n" ] } ], "source": [ "a = 1\n", "b = 3\n", "if (a == 1) and \\\n", "(b == 3):\n", " print('connected lines')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "connected lines\n" ] } ], "source": [ "a = 1\n", "b = 3\n", "if (a == 1) and (b == 3):\n", " print('connected lines')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-3 할당문 (Assignment)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 1\n", "b = a" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "can't assign to operator (, line 1)", "output_type": "error", "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[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n" ] } ], "source": [ "1 + 3 = a" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "a = 1\n", "a = a + 1\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 4\n", "0 0 0\n", "3.5 5.6\n" ] } ], "source": [ "c, d = 3, 4\n", "print(c, d)\n", "\n", "x = y = z = 0\n", "print(x, y, z)\n", "\n", "e = 3.5; f = 5.6\n", "print(e, f)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 두 변수의 값을 swap하는 방법" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.6 3.5\n" ] } ], "source": [ "e = 3.5; f = 5.6\n", "e, f = f, e\n", "print(e, f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 아래에서 b = c + d는 식(Expression)이 아니라 문(Statement)이기 때문에 a에 할당될 수 없다." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "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" ] } ], "source": [ "a = (b = c + d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-4 확장 할당문" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "a = 1\n", "a += 4\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "a = 10\n", "a -= 3\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n" ] } ], "source": [ "a = 10\n", "a *= 2+3\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-5 객체와 할당\n", "- 객체의 변수는 해당 객체의 레퍼런스를 지니고 있음\n", "- a = 1이라는 Statement에서 a는 이름, 1은 객체이며 a 변수는 1이라는 객체를 가리킨다.\n", " - 즉, a 변수는 1 객체의 레퍼런스를 지니고 있음" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[10, [1, 2, 3], 20]\n", "['x', [1, 2, 3], 'y']\n" ] } ], "source": [ "a = [1,2,3]\n", "b = [10, a, 20]\n", "c = ['x', a, 'y']\n", "\n", "print(a)\n", "print(b)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![image](../images/assignment.png)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1000, 3]\n", "[10, [1, 1000, 3], 20]\n", "['x', [1, 1000, 3], 'y']\n" ] } ], "source": [ "a[1] = 1000\n", "\n", "print(a)\n", "print(b)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 4 콘솔 입출력\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4-1 콘솔 입력" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 콘솔 (Console)\n", " - 윈도우즈에서는 Command창, 리눅스/맥에서는 Terminal창\n", " - 각 IDE (예, 이클립스)에서는 별도의 콘솔 창이 제공됨\n", "- Python2에서, raw_input(): 문자열 입력 내장함수\n", "- Python3부터는 input()만을 사용\n", " - raw_input() 내장함수는 지원하지 않음" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name?홍길동\n" ] } ], "source": [ "# raw_input 에서 input 으로 변경\n", "# name = raw_input('name?')\n", "name = input('name?')" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "홍길동\n" ] } ], "source": [ "print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- int(): 문자열을 정수로 변환하는 내장함수\n", "
int(x=0) -> integer\n",
    "int(x, base=10) -> integer
" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "0\n" ] } ], "source": [ "print(int(3.14)) # For floating point numbers, this truncates towards zero. 0이 되는 방향으로 소숫점 이하를 버림\n", "print(int()) # return 0 if no arguments are given. 그냥 int()는 0을 리턴" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "int : 10\n", "10\n", "\n", "\n", "int : 10\n", "10\n", "\n" ] } ], "source": [ "# k = int(raw_input('int : '))\n", "k = input('str : ')\n", "print(k)\n", "print(type(k))\n", "\n", "print()\n", "\n", "k = int(input('int : '))\n", "print(k)\n", "print(type(k))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Python3에서 input()의 기능\n", "# Read a string from standard input. The trailing newline is stripped." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "? input" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "expr:30+50\n", "30+50\n" ] } ], "source": [ "k = input('expr:')\n", "print(k)\n", "# expr:30 + 50\n", "# 80" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4-2 콘솔 출력" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- print: 화면에 자료를 출력하는 보편적인 statement\n", "- 하나의 print로 두 개 이상의 자료를 출력할 때에는 , 사용" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9 2\n" ] } ], "source": [ "print(4 + 5, 4 - 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 세미콜론(;)은 순차적으로 입력된 각 statement를 분리함 " ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "print(1); print(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Python2에서 기본적으로 print는 마지막에 줄바꿈을 수행한다. 그러나 콤마(,)가 마지막에 있으면 줄바꿈을 하지 않음\n", "- Python3에서는 콤마를 붙이나 안붙이나 상관없이 줄바꿈을 수행" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "\n", "1\n", "2\n" ] } ], "source": [ "print(1),\n", "print(2)\n", "#1 2\n", "print()\n", "print(1)\n", "print(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- print를 여러번 사용하여 두 개 이상의 자료를 newline 없이 출력할 때에는 end=' '를 사용 (디폴트는 end='\\n')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1, 2\n", "\n", "1 2\n" ] } ], "source": [ "print(1, end=\", \")\n", "print(2)\n", "\n", "print()\n", "\n", "print(1, end=\" \")\n", "print(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- +연산자는 숫자와 문자열에 대한 연산을 지원하지 않는다." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m12\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'spam'\u001b[0m\u001b[0;34m)\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'" ] } ], "source": [ "print(12 + 'spam')" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12spam\n" ] } ], "source": [ "print('12' + 'spam')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일

" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 1 }