Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Created October 13, 2020 11:53
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/4341551dec779b0eb1accffc62593d30 to your computer and use it in GitHub Desktop.
Save takatakamanbou/4341551dec779b0eb1accffc62593d30 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 ex04 Notebook その2\n",
"\n",
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## タプルを使う (pp.130-135)\n",
"「set(集合)を使う(pp.123-130)」はスキップします"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"これまで学んできた **リスト** や **ディクショナリ** は,「複数のデータを組にして扱う」ための **データ構造** (コンピュータ上でデータを扱いやすくするために用意された仕組み)を Python で実現したものです.\n",
"ここで扱う **タプル** (tuple) もまた, 「複数のデータを組にして扱う」ための仕組みです."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"リストは複数の値を `[` と `]` で囲んで定義しましたが,タプルの場合は `(` と `)` で囲んで定義します."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# タプルの定義\n",
"month_names = (\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"それ以外の書き方や性質はリストによく似ています."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 要素番号を指定して要素を取り出す\n",
"month_names[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# + 演算子を使って二つのタプルを連結する\n",
"month_names = month_names + (\"August\", \"September\", \"October\", \"November\", \"December\")\n",
"month_names"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"month_names[9]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"組み込み関数`len()`で要素数を取得したり,`in`演算子で要素を検索したりもできます."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(month_names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for key in [ \"October\", \"10月\", \"神無月\" ]:\n",
" print(\"\\\"\" + key + \"\\\"はこのタプルの要素\", end = \"\")\n",
" if key in month_names:\n",
" print(\"です\")\n",
" else:\n",
" print(\"ではありません\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"リストとタプルの最大の違いは,**タプルは要素の変更ができない**というところです.\n",
"以下はエラーになります."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 0番目の要素を変更しようとしてみる\n",
"month_names[0] = \"睦月\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`del` を使って要素を削除しようとしても同様にエラーになります."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"「そんな性質何がうれしいの?」と思われるかもしれません.この授業ではこれ以上深入りしませんが,この「要素を変更できない」という性質が必要な場面は結構あるのです.タプルはそういう場面で活躍します(詳しくはpp.132-135)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"最後に,**要素が1つだけのタプル**に関する注意."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 要素が1つだけのリスト\n",
"list1 = [\"ぼっち\"]\n",
"list1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"リストの場合,要素数 1 のリストは,上記のように単に `[`, `]` で囲めば定義できます.\n",
"しかし,タプルの場合,`(`, `)` で囲んだだけではうまくいきません"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 要素が1つだけのタプル(として正しく定義できてない)\n",
"tuple1 = (\"ぼっち\")\n",
"tuple1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の場合,括弧はタプルを表す記号ではなく演算順序を表すためにつける記号と解釈されてしまうので,タプルになっていません. p.132 を参考に,`tuple1`が要素数1のタプルとなるよう上記セルの内容を修正しなさい."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"右側に要素がならんでない `,` はなんだか寂しい...ぼっち感..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 練習\n",
"\n",
"|1月|2月|3月|4月|5月|6月|7月|8月|9月|10月|11月|12月|\n",
"|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"|睦月|如月|弥生|卯月|皐月|水無月|文月|葉月|長月|神無月|霜月|師走|\n",
"\n",
"ほげおくんは,上記の月の和名をならべたタプルを, `m` という変数名で作りたくて,次のように書きました.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = ('睦月', '如月', '弥生', '卯月', '皐月', '水無月')\n",
"print(m)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記では足りないのは明らかですね.以下のセルを修正して,全ての月が表示されるようにしてください."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 次の行に, m に要素を追加するコードを書く\n",
"\n",
"\n",
"# ここから下はいじらない\n",
"for i in range(len(m)):\n",
" print(str(i+1)+'月は'+m[i]+'ほげ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## 論理演算 (pp.135-138)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以前学んだ if 文の話のつづき.\n",
"if文などの条件式には,以下の例のように,演算結果が `True`(真) か `False`(偽) のいずれかになるような式を書くのでした."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4649 + 1314 == 5963"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"Hoge\" == \"hoge\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"教科書 p.136 に,このような式で使うことのできる演算子がいくつか紹介されています.\n",
"そちらも参考に,以下のセルを実行した結果がそれぞれどうなるか予想しながら実行してみましょう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5**(4-4)+9 == 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5 > 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"100 == 100.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"かなこ\" != \"かなこぉ↑↑\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[1, 2, 3] == [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"ほげ\" in \"ホゲとかHOGEとかHogeとか,もううんざりだほげ\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"ほげ\" in [ \"ホゲ\", \"HOGE\", \"Hoge\", \"ほげ\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"4949 in [ 1818, 2929, 4848, 5959 ]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"文字列に対して `<` や `>=` といった比較演算子を使うと..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"a\" < \"b\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"a\" > \"b\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"ab\" < \"b\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"abc\" < \"ab\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"this\" < \"that\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"このように,**辞書式順序**(一般的な辞書で使われるような順序)で大小関係が決まります.\n",
"ただし,**「文字コード」に基づいて計算される順序**ですので,数字や記号を含んだり,日本語文だったりする場合には,直感に反する結果や謎な結果となりますので,注意が必要です."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"ほげ\" < \"Hoge\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"高橋\" < \"田中\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"10\" < \"9\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の最後の例は,謎でも何でもなく,文字列として辞書式順序で比較しているのですから当然の結果ですね.\n",
"整数として比較させたければ, `int()` 使って変換する等しないといけません."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C言語で`int`型の変数`x`に格納された値が「0以上でありかつ100以下である」というような判定をしたい場合,次のような条件式を書きました.\n",
"```\n",
"0 <= x && x <= 100\n",
"```\n",
"この式で使っている演算子 `&&` は,「かつ」を意味するものでした.「または」の時は `||` でした. \n",
"\n",
"Python にも,このように複数の条件式を組み合わせる時に使う **論理演算子** があります.\n",
"ここでは,そのうち **`and`**,**`or`**と**`not`** の3つを紹介します."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = int(input(\"数値入力してね\"))\n",
"if 0 <= x and x <= 100:\n",
" print(\"0以上かつ100以下です\")\n",
"if 0 <= x or x <= 100:\n",
" print(\"0以上または100以下です\")\n",
"if not x <= 100:\n",
" print(\"100以下ではありません\")\n",
" \n",
"if not ((not 0 <= x) or (not x <= 100)): # (よだんだよん) ド・モルガンの法則 \n",
" print(\"ほげ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"入力する値をいろいろ変えて動作確認しましょう."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`not` は,`in` と組み合わせて次のように使うこともできます.`\"HOGE\"`が**見つからなかったら** `True` になります."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"HOGE\" not in \"GEHOGOHO\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ところで,C言語を学び始めたばかりの頃,`x`が「0以上でありかつ100以下である」を次のように書いて失敗した経験はありませんか?\n",
"```\n",
"0 <= x <= 100\n",
"```\n",
"実は Python ではこの書き方が通ってしまいます."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 60\n",
"0 <= x <= 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`x` の値をいろいろ変えて動作確認すると予想がつく通り,`and` でつながってると解釈されます."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 練習\n",
"\n",
"以下のそれぞれの文に書いたことを判定する条件式(書いてあることが成り立っているなら `True` になり,さもなくば `False` になる式)を書いてください."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. `x` が 100 と等しい\n",
"1. `x` の2乗が 100 より大きい\n",
"1. `x` か `y` のどちらかが 0 \n",
"1. `x` が 0 以上 100 未満\n",
"1. 「`x` が 0 以上 100 未満」または「`y` が 0 以上 100 未満」\n",
"1. `x` は 2以外の偶数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"答えは,このセル内に白字で書いてあります.マウスカーソルで選択してみてください.\n",
"\n",
"<ol>\n",
" <li><font color=\"white\">x == 100</font></li>\n",
" <li><font color=\"white\">x * x > 100</font></li>\n",
" <li><font color=\"white\">x == 0 or y == 0</font></li>\n",
" <li><font color=\"white\">0 &lt;= x &lt; 100 (0 &lt;= x and x &lt; 100)</font></li>\n",
" <li><font color=\"white\">0 &lt;= x &lt; 100 or 0 &lt;= y &lt; 100</font></li>\n",
" <li><font color=\"white\"> x != 2 and x % 2 == 0</font></li>\n",
"</ul>\n",
"</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"もういっちょ練習.\n",
"- 「西暦 `y` 年はうるう年」 うるう年かどうかの判定は,以下を受けから順に適用することでできる.例えば,1900年のはうるう年ではなく,2000年はうるう年. `and` や `or` をつないで一つの式で表してみよう\n",
" 1. 4で割り切れるならうるう年\n",
" 1. ただし,100で割り切れるならうるう年ではない\n",
" 1. ただし,400で割り切れるならうるう年\n",
"- 点 (`x`, `y`) は (0, 0) と (100, 100) を左上と右下の頂点とする正方形の内部にある"
]
},
{
"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.7.3"
},
"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