{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python入門：変数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "コンピュータは「0」と「1」だけからなる機械言語を使って動いています。人がコンピュータと会話するためには、人間と機械両方が分かる「プログラミング言語」が必要です。\n",
    "\n",
    "コンピュータが発明されてから、多くのプログラミング言語が作られています。Python言語のほかに、有名なものでは、Fortran言語、C言語、Java言語、JavaScript言語などがあります。\n",
    "\n",
    "本日は、以下のPython言語の基礎概念を解説します。\n",
    "\n",
    "- 変数（変数の役割、変数の使用方法）\n",
    "- 制御構文（for文）\n",
    "\n",
    "（教科書「Python超入門」のChapter 1,3を参照してください。）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 補足\n",
    "\n",
    "CESに限らず、Windows、MacOS、Linuxの中でPythonのコードを実行することも可能です。教科書の4-5ページを参考にしてください。34-39ページにもコードの実行方法の説明があります。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Python言語の変数\n",
    "\n",
    "プログラミングの変数は、**プログラムで利用するデータを一時的に保存する入れ物**のようなものです。\n",
    "\n",
    "「x=5」というプログラムでは、「=」の右辺の値５を左辺の変数「x」に代入します。「=」は「**代入演算子**」と呼ばれています。\n",
    "\n",
    "以下の例で、変数x, yに他の値を代入してみましょう。変数の値を確認するために、「print()」という関数を使って値を出力することができます。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "51\n",
      "151\n",
      "1000\n"
     ]
    }
   ],
   "source": [
    "# 例 ( x,y に他の値を代入してみましょう。)\n",
    "x=51\n",
    "print(x)\n",
    "\n",
    "y=x+100\n",
    "print(y)\n",
    "\n",
    "x=1000\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 変数の役割\n",
    "\n",
    "コンピュータのプログラミングでは、機械的な考え方が必要です。必ず「値をどの変数に格納するか」を意識してください。\n",
    "\n",
    "### 例：変数の値の入れ替え\n",
    "以下では、二つの変数の値の入れ替えの例によって、変数の役割を体験してみます。\n",
    "\n",
    "下記のコードでは、変数x, yはそれぞれ値を持っています。二つの変数の値の入れ替えを行います。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=100\n",
    "y=200"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以下の失敗の例を実行してみて、値の入れ替えが正しくできていない理由を考えてください。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200 200\n"
     ]
    }
   ],
   "source": [
    "x=100\n",
    "y=200\n",
    "\n",
    "#失敗の例１\n",
    "x=y\n",
    "y=x\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 100\n"
     ]
    }
   ],
   "source": [
    "x=100\n",
    "y=200\n",
    "\n",
    "#失敗の例２\n",
    "y=x\n",
    "x=y\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "変数の値が無くならないように、第3変数を用意することが必要です。以下は値の入れ替えの成功の例です。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200 100\n"
     ]
    }
   ],
   "source": [
    "x=100\n",
    "y=200\n",
    "\n",
    "#成功の例\n",
    "z=x # xの値を一時的にzに格納します。\n",
    "x=y\n",
    "y=z # zが保持している元々のxの値をyに代入します。\n",
    "print(x,y)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 補足\n",
    "<blockquote>\n",
    "    <b>a) 変数に関わる用語</b> <br>\n",
    "    変数に関する処理には一般的に「プログラミング用語」が使用されます。以下の用語を覚えてほしいです。\n",
    "\n",
    "- 変数を作成して、（初めて）値を代入することを**初期化**と言う。\n",
    "- 変数を作成すること　－＞ 変数を「**宣言**」する。変数を「**定義**」する。\n",
    "- 値を変数に入れること　－＞変数に値を「**代入する**」「**格納する**」。変数が値を「**保持する**」。\n",
    "\n",
    "</blockquote>\n",
    "\n",
    "<blockquote>\n",
    "    <b>b) 変数名のルール</b> <br>\n",
    "    \n",
    "    変数の名前付けには、以下のルールがあります。\n",
    "\n",
    "- 大文字・小文字のアルファベットと数字、\"_\"（アンダーバー）を組み合わせる。\n",
    "- 数字から始まらないこと。\n",
    "- Python言語の文法で既に使用している単語（「予約語」）でないこと。例えば「if」。\n",
    "    \n",
    "</blockquote>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 演習1 \n",
    "\n",
    "変数a, b, c, dはそれぞれ値を持っています。変数の値を以下のように入れ替えてください。\n",
    "\n",
    "$$\n",
    "\\left(\\begin{array}{c} a \\rightarrow  b \\\\    b \\rightarrow c\\\\ c \\rightarrow  d\\\\ d  \\rightarrow  a\\end{array} \\right) \n",
    "$$\n",
    "例えば、最初に$a=1,b=2,c=3,d=4$の場合、入れ替え後には$b=1,c=2,d=3,a=4$となります。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4\n"
     ]
    }
   ],
   "source": [
    "# 演習の答えをここに書いてください。\n",
    "a=1; b=2; c=3; d=4\n",
    "print(a,b,c,d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 変数の「型」\n",
    "\n",
    "Python言語では、数字を扱うことができます。数字の中にも「整数(int)」「浮動点小数(float)」の区別があります。\n",
    "\n",
    "数字以外に、「文字列(str)」「リスト(list)」などの変数の型があります。「type()」を使えば、変数の型を調べることができます。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=100\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=10.1\n",
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "c1=\"計算機演習A\"\n",
    "c2='計算機演習B'\n",
    "print(type(c1), type(c2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=[1,2,3]\n",
    "type(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "変数の型によって、同じ演算子でも演算の意味は変わります。\n",
    "\n",
    "以下の二つの例では、「+」という演算子は異なる意味を持っています。\n",
    "最初の例では、「+」は「足し算」を意味しています。2番目の例では、「+」は「文字列の結合」を意味しています。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110.01\n"
     ]
    }
   ],
   "source": [
    "a=100\n",
    "b=10.01\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "計算機演習A\n"
     ]
    }
   ],
   "source": [
    "c1 = \"計算機演習\"\n",
    "c2 = \"A\"\n",
    "print(c1+c2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### キャストを使った型の変換"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "文字列と数値を結合する場合、同じ型にしないといけません。以下のコードでは、course_name と score は異なる型であるため、実行するとエラーとなります。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "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<ipython-input-17-282740a57b9d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mcourse_name\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[1;32m      2\u001b[0m \u001b[0mscore\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mcourse_name\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mscore\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": [
    "course_name = \"計算機演習の成績＝\"\n",
    "score = 100\n",
    "course_name + score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "「str()」という関数を使ってscoreを文字列に変換すれば、問題が解消します。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'計算機演習の成績＝100'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "course_name + str(score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代わりに、\"100\"のような文字列を数字に変換することも可能です。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'int'> 100\n",
      "<class 'float'> 90.5\n"
     ]
    }
   ],
   "source": [
    "score = int(\"100\")\n",
    "print(type(\"100\"))\n",
    "print(type(score), score)\n",
    "score = float(\"90.5\")\n",
    "print(type(score), score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 補足\n",
    "\n",
    "以下の例では、a, bはそれぞれ整数ですが、割り算のときにa, bが一度小数（float）に変換されて値が計算されています。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'> 5.0\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = 2\n",
    "x = a/b\n",
    "print(type(x), x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 リストの使用\n",
    "\n",
    "リスト(list)はデータの並びを扱うための型です。以下の例では、リストの初期化ができます。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"taro\", \"jiro\",\"saburo\"]\n",
    "age_list = [12,7,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "リストに値を追加するためには、appendという関数を使います。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['taro', 'jiro', 'saburo', 'shirou']\n",
      "[12, 7, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "name_list.append(\"shirou\")\n",
    "age_list.append(3)\n",
    "print(name_list)\n",
    "print(age_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "リストの各要素はインデックス番号によってアクセスできます。特に、リスト内の要素は０から数え始めます。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "taro\n",
      "jiro\n",
      "saburo\n"
     ]
    }
   ],
   "source": [
    "print(name_list[0])\n",
    "print(name_list[1])\n",
    "print(name_list[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "リストの各要素に順番にアクセスするために、for文を使うことができます。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "taro\n",
      "jiro\n",
      "saburo\n",
      "shirou\n"
     ]
    }
   ],
   "source": [
    "for name in name_list:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "7\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for age in age_list:\n",
    "    print(age)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### rangeの使用\n",
    "\n",
    "連続した数字の並びを作成するために、range関数が便利です。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "numbers = range(4)\n",
    "for x in numbers:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### rangeの使用例\n",
    "\n",
    "以下のコードの表示結果を確認してください。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "taro\n",
      "jiro\n",
      "saburo\n",
      "shirou\n"
     ]
    }
   ],
   "source": [
    "# 名前を順番に出力します。\n",
    "for index in range(4):\n",
    "    print(name_list[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "7\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 年齢を順番に出力します。\n",
    "for index in range(4):\n",
    "    print(age_list[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The age of taro is 12.\n",
      "The age of jiro is 7.\n",
      "The age of saburo is 4.\n",
      "The age of shirou is 3.\n"
     ]
    }
   ],
   "source": [
    "for index in range(4):\n",
    "    print(\"The age of \" + name_list[index] + \" is \" + str(age_list[index]) + \".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 演習2\n",
    "\n",
    "forループによって、以下のような結果を出力してください。\n",
    "\n",
    "```\n",
    "The score of taro is 100.\n",
    "The score of jiro is 90.\n",
    "The score of saburo is 80.\n",
    "The score of shirou is 70.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "#　ここに解答を書いてください。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 演習3\n",
    "\n",
    "range()を使って、以下のような数列のリストを作成してください。\n",
    "```\n",
    "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "#　ここに解答を書いてください。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## レポートの提出について\n",
    "\n",
    "本授業の演習1、2、3を解き、新しいNotebookに演習の内容と解答を書いて、提出してください。"
   ]
  },
  {
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
