This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# AProg2020 ex06 Notebook その1\n", | |
"\n", | |
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"----\n", | |
"## Python の基礎 (2)\n", | |
"この資料中に説明なくページ番号が出てくる場合,教科書のページ番号を表しています.\n", | |
"\n", | |
"前回の Notebook その3では,教科書の「Chap04-03 文字列型を使いこなす」(pp.167-176)の内容を学習(自習要素多めで)しました.\n", | |
"ここでは,「Chap04-05」の内容を学習しましょう(**教科書と順序変えてます**)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### リスト型,タプル型を使いこなす (pp.182-189)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### スライスの話(pp.186,187)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"L = [ 0, \"位置\", \"荷\", \"酸\", \"詞\", \"碁\", \"禄\", \"質\", \"蜂\", \"句\", \"beast\"]\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"上記のリスト `L` に対して,教科書に載ってる「リストからスライスで要素を取り出す」を試してみよう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"同様に,「要素の代入」,「要素の削除」をやってみよう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### リストで利用できるメソッド(pp.187-189)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"L = [ 0, \"位置\", \"荷\", \"酸\", \"詞\", \"碁\", \"禄\", \"質\", \"蜂\", \"句\", \"beast\"]\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"上記のリスト `L` に対して,教科書に載っているリスト型のメソッドをいろいろ使ってみよう.セルの実行順序に注意.リストの内容を変更するメソッドを複数実行する場合,その順序で結果が変わりますね.元の `L` に対して動作確認したいなら,「↑の`L`を定義するセルを実行した直後に対象のセルを実行する」というのもよいでしょう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### リストをソートする(pp.182-185)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"monk_fish_team = [158, 157, 163, 157, 145]\n", | |
"monk_fish_team" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"上記のリストを昇順/降順にソートしよう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"「ソート順をカスタマイズする」はスキップします." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### アンパック代入 (pp.185,186 および pp.201,202)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"pp.185, 186を読んで,以下を実行してみましょう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = 4649\n", | |
"y = 5963\n", | |
"y, x = x, y\n", | |
"print(x, y)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"上記の3行目のように複数のものを `,` 区切りならべ,まとめて代入する方法を **アンパック代入** といいます.ここでは二つの変数をならべてますが,もちろん3つ以上ならべることもできます.\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"C言語で上記の `x` と `y` の値の入れ替えをやりたいなら,次のように第3の変数を用意しないといけないところですね.\n", | |
"```\n", | |
"int x = 4649, y = 5963\n", | |
"int w;\n", | |
"\n", | |
"w = x;\n", | |
"x = y;\n", | |
"y = w;\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"実は,上記のアンパック代入の仕組みは,タプルを利用しています.たとえば,変数 `hoge` を次のようなタプルとしてみると..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hoge = (\"椀\", \"通\", \"スリ\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"次のような代入が可能です." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"one, two, three = hoge\n", | |
"print(one, two, three)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python では,明示的に `(`, `)` で囲まなくても, `,` 区切りで複数の要素をならべるとタプルと解釈してくれるのです." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"アンパック代入と同様のことが,関数の戻り値の受け渡しでもできます." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def f(a, b):\n", | |
" return a+b, a-b, a*b, a//b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"s, t, u, v = f(2, 6)\n", | |
"print(s, t, u, v)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"次のように引数をまとめて受け取ってみると,戻り値がタプルになっていることがわかります." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"stuv = f(2, 6)\n", | |
"stuv" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 文字列のフォーマット(pp.177-181)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"文字列に対する `format()` メソッドを使うと,C言語の `printf()` における書式指定文字列(`\"%d\"` みたいなの)でできることと似たことができます.ここでは簡単に紹介しますが,たくさん機能があって便利です.詳しくは教科書やウェブリファレンスへ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"S = \"そいつぁ{}だねぇ\"\n", | |
"print(S.format(\"ロック\"))\n", | |
"print(S.format(\"ほげ\"))\n", | |
"print(S.format(\"あれ\"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"S2 = \"「{}」だって? そいつぁ{}だねぇ\"\n", | |
"for s in [\"ロック\", \"ほげ\", \"あれ\"]:\n", | |
" print(S2.format(\"ほげほげ\", s)) # format() の引数の順に {} が置換される.数が合わないとエラー" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"S3 = \"{2}:「{1}のくせになまいきだ」\\n{0}:「{1}くん,きみはじつにばかだな」\"\n", | |
"SS = S3.format(\"ドラえもん\", \"のび太\", \"ジャイアン\") # format() の引数順に {0}, {1}, {2} が置換される\n", | |
"print(SS)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import math\n", | |
"print(\"{0:05d} {1:.1f} {1:.3f} {1:.5f}\".format(123, math.pi))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ディクショナリ型を使いこなす (pp.191-195)\n", | |
"\n", | |
"上記ページ範囲の内容については,授業では説明しません.必要に応じて教科書やウェブリファレンスを頼りましょう." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 練習" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"以下の2つのセルを上から順に実行すると,リスト `data` の最小値・最大値とそれぞれの要素番号が正しく出力されるように,上の方のセルに関数を定義しなさい." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# このセルに関数の定義を書く" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = [ -5, -9, -6, -3, -4, -6, -4, -9 ]\n", | |
"# 上記の関数たちを呼ぶ(このセルの内容は変更不可)\n", | |
"i, j = myargminmax(data)\n", | |
"print(\"最小値は{2}({0}番目),最大値は{3}({1}番目)です\".format(i, j, data[i], data[j]))" | |
] | |
}, | |
{ | |
"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