Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Created September 26, 2020 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatakamanbou/262ebb5602139830ad8cb6ad74acf32a to your computer and use it in GitHub Desktop.
Save takatakamanbou/262ebb5602139830ad8cb6ad74acf32a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AProg2020 ex02 Notebook その3\n",
"\n",
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## for文でループを使う (pp.77-82)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.77-79を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz = ['れに', 'かなこ', 'しおり', 'あやか', 'ももか']\n",
"for member in mcz:\n",
" print(member)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python の for 文の書き方は,C言語とはかなり違っている.\n",
"```\n",
"for ループ変数 in シーケンス:\n",
" 繰り返し実行するブロック\n",
"```\n",
"- **シーケンス** には,リストのように要素を一つずつ取り出せるものを指定.上の例ではリストを表す変数 `mcz` を指定している.\n",
"- シーケンスの先頭から順に取り出した要素が **ループ変数** に代入され,それぞれの要素ごとに **繰り返し実行するブロック** が実行される.\n",
"- for の行の最後には `:`(コロン)をつける.その次の行から,**インデント**(字下げ)して**繰り返し実行するブロック**を書く."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上で「シーケンスの先頭から順に**取り出した**要素がループ変数に代入され」\n",
"と書いたが,例えば上の例でリスト `mcz` から要素が削除されてしまうわけではない."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下の実行結果がどうなるか予想してから,実行してみなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in [1, 3, 5, 7, 9]:\n",
" print(x, x**2)\n",
"print(\"ほげ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の3行目をインデントして再実行してみなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L2 = [ 0, 1, 2.0, \"参\", 4, \"五\", 6, 7, \"はち\", 9, 10]\n",
"L2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下の実行結果がどうなるか予想してから,実行してみなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in L2[3:8]:\n",
" print(x, end=\" \") # このように書くと,改行する代わりにスペースを出力する"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$ x_1, x_2, \\dots , x_N $ という $N$ 個の実数値があったとき,これらの平均を $\\mu$ とおくと,\n",
"\\begin{align}\n",
"\\mu = \\frac{1}{N}\\sum_{n=1}^{N} x_n\n",
"\\end{align}\n",
"となる.また,標準偏差を $\\sigma$ (分散は $\\sigma^2$)とおくと,\n",
"\\begin{align}\n",
"\\sigma^2 = \\frac{1}{N}\\sum_{n=1}^{N} (x_n - \\mu)^2\n",
"\\end{align}\n",
"となる.\n",
"\n",
"pp.79-81を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"monk_fish_team = [158, 157, 163, 157, 145]\n",
"\n",
"total = sum(monk_fish_team)\n",
"length = len(monk_fish_team)\n",
"mean = total/length\n",
"variance = 0\n",
"\n",
"for height in monk_fish_team:\n",
" variance += (height-mean)**2\n",
"\n",
"variance = variance/length\n",
"variance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"variance**0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"volleyball_team = [143, 167, 170, 165]\n",
"\n",
"total2 = sum(volleyball_team)\n",
"length2 = len(volleyball_team)\n",
"mean2 = total2/length2\n",
"variance2 = 0\n",
"\n",
"for height in volleyball_team:\n",
" variance2 += (height-mean2)**2\n",
"\n",
"variance2 = variance2/length2\n",
"variance2**0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"-1\">(よだんだよん) 後日学ぶことだが,Pythonでは,プログラミング言語として標準で備わっている機能に加えて,様々な拡張機能(科学技術計算,機械学習,データ分析,画像処理,ゲーム開発,etc.)を読み込んで動作させることができる.\n",
"例えば,科学技術計算のモジュール NumPy ( https://www.numpy.org/ ) を用いると,上記の計算はこんな感じに書ける. </font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # NumPy モジュールを np という名前で使えるようにする\n",
"vec = np.array([143, 167, 170, 165]) # NumPy の配列\n",
"print(np.mean(vec)) # 平均\n",
"print(np.sqrt(np.var(vec))) # 分散の平方根 = 標準偏差"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.81,82を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for cnt in range(10):\n",
" print(cnt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"この例では出力が `0` から `9` までなことに注意. **`range(n)` は `0` から `n-1` までの整数**のシーケンスを返す.\n",
"\n",
"「これやと複雑な繰り返しとか書きにくいやん,3から始めるとか,奇数だけとか…」と思いました?\n",
"実は組み込み関数 `range()` にはもっと機能があるので,いろいろできます.教科書p.197参照."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"savings = 100\n",
"for i in range(15):\n",
" savings = savings+savings*0.05\n",
"\n",
"savings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"リストの要素を順に取り出して処理する繰り返しは以下のその1のように書けるが,その2のように,組み込み関数 `len()` と `range()` を使って書くこともできる."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# その1\n",
"for x in L2:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# その2\n",
"for i in range(len(L2)): # 0 から (L2 の要素数) - 1 まで\n",
" print(i, L2[i]) # これだとループ変数を要素番号として扱える"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"-1\">(よだんだよん) 組み込み関数 `enumerate()` を使う手もある(p.199).</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i, x in enumerate(L2):\n",
" print(i, x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## if文で条件分岐をする (pp.83-85)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"今回は,if文による条件分岐の基本的な事柄のみ解説します.\n",
"\n",
"pp.83-85を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if 2*2*2+2 == 10:\n",
" print(\"2*2*2+2は10\")\n",
"if 2+2*2+2 == 10:\n",
" print(\"2+2*2+2は10\")\n",
"if (2+2)*2+2 == 10:\n",
" print(\"(2+2)*2+2は10\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python の if 文の最も基本的な書き方は,次の通り.\n",
"```\n",
"if 条件式:\n",
" 条件が真だったときだけ実行するブロック\n",
"```\n",
"- __条件式__には,例えば`x` に整数が代入されているとした場合の `x > 0` や `x == 5953` のように,結果が **真 (True)** か **偽(False)** のいずれかになるような式を書く.\n",
"- if の行の最後には `:`(コロン)をつける.その次の行から,**インデント**(字下げ)して**条件が真だったときだけ実行するブロック**を書く."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 数の比較\n",
"\n",
"if 1 == 1:\n",
" print(\"1番目はTrue\")\n",
"if 5^(4-4)+9 == 10: # 教科書はこうなっているが, `^` は本当は`**` と書きたかったのかもしれない\n",
" print(\"2番目はTrue\")\n",
"if 2 < len([0, 1, 2]):\n",
" print(\"3番目はTrue\")\n",
"if sum([1, 2, 3, 4]) < 10:\n",
" print(\"4番目はTrue\")"
]
},
{
"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"
},
"toc": {
"base_numbering": 1,
"nav_menu": {
"height": "146px",
"width": "258px"
},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "目次",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment