Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Created September 26, 2020 02:56
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/b83903ec2ea0c6f5bf38f13dac4d616f to your computer and use it in GitHub Desktop.
Save takatakamanbou/b83903ec2ea0c6f5bf38f13dac4d616f 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 ex03 Notebook その1\n",
"\n",
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## if文で条件分岐をする (pp.83-92)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.83-86を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"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": [
"# 文字列同士の比較\n",
"\n",
"if \"AUG\" == \"AUG\":\n",
" print(\"1番目はTrue\")\n",
"if \"AUG\" == \"aug\":\n",
" print(\"2番目はTrue\")\n",
"if \"あいう\" == \"あいう\":\n",
" print(\"3番目はTrue\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.86-88を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 文字列に対する in 演算子の働き\n",
"\n",
"if \"GAG\" in \"AUGACGGAGCUU\":\n",
" print(\"1番目はTrue\")\n",
"if \"恋と戦いはあらゆることが正当化されるのよ\" in \"正当化\":\n",
" print(\"2番目はTrue\")\n",
"if \"stumble\" in \"A horse may stumble though he has four legs\":\n",
" print(\"3番目はTrue\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`in` は,for文のときにも出てきましたね. **`x in y` という式は,`y` の要素に `x` が含まれていれば `True`,さもなくば `False` になります**(上記の2番目が `False` なことに注意)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# リスト同士の比較\n",
"\n",
"if [1, 2, 3, 4] == [1, 2, 3, 4]:\n",
" print(\"1番目はTrue\")\n",
"if [1, 2, 3] == [2, 3]:\n",
" print(\"2番目はTrue\")\n",
"if [1, 2, 3] == ['1', '2', '3']:\n",
" print(\"3番目はTrue\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記に2行追加して, `[1, 2, 3]` と `[2, 3, 1]` が等しいと判定されたら `4番目はTrue` と出力するようにしなさい.\n",
"実行して動作を確認しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# リストに対する in 演算子の働き\n",
"\n",
"if 2 in [2, 3, 5, 7, 11]:\n",
" print(\"1番目はTrue\")\n",
"if 21 in [13, 17, 19, 23, 29]:\n",
" print(\"2番目はTrue\")\n",
"if 'アッサム' in ['ダージリン', 'アッサム', 'オレンジペコ']:\n",
" print(\"3番目はTrue\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2番目3番目はよく考えないといけない例\n",
"\n",
"if 1 in [0, 1, 2, 3, 4]:\n",
" print(\"1番目はTrue\")\n",
"if [1, 2] in [0, 1, 2, 3, 4]: # これで 「1か2が含まれていたら True になるわけではない\n",
" print(\"2番目はTrue\")\n",
"if [1, 2] in [0, 1, [1, 2], 3, 4]:\n",
" print(\"3番目はTrue\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下はpp.88-92と同様の内容を説明したものですが,教科書とは違う例を使ってます.\n",
"**理解しながら読み進めて**,各セルを実行して動作確認しなさい."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python の if 文では,C言語と同様に if-else 型の条件判定も書ける. 2箇所の `:` を忘れずに.\n",
"```\n",
"if 条件式:\n",
" 条件が真だったときだけ実行するブロック\n",
"else:\n",
" 条件が偽だったときだけ実行するブロック\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# else の例 (教科書とはちょっと違う)\n",
"\n",
"if 2*3-2+4 == 10:\n",
" print(\"式1は10\")\n",
"else:\n",
" print(\"式1は10にならない\")\n",
"if 2**3-2+4 == 10:\n",
" print(\"式2は10\")\n",
"else:\n",
" print(\"式2は10にならない\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C言語で if-else が入れ子にできる(if-else の中に さらに if-else 書いたりできる)のと同じように, **Python の if 文も入れ子にできる**.\n",
"\n",
"```\n",
"int y = 10;\n",
"if (y < 18){\n",
" ...\n",
"}else{\n",
" if (y < 25){\n",
" ...\n",
" }else{\n",
" ...\n",
" }\n",
"}\n",
"```\n",
"C言語で上記の例に相当するものを, Python では例えば次のように書く.\n",
"1行目の 19 をいろいろ変えて動作確認しよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 10\n",
"if y < 18:\n",
" print(\"こども\")\n",
"else:\n",
" if y < 25:\n",
" print(\"夢も希望もある大人\")\n",
" else:\n",
" print(\"夢も希望もある大人...が眩しく見える大人\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5行目のif文の条件式は `y < 25` だけですが,このブロック(5行目以降)は4行目の `else` の中なので,「`y < 18`がFalse 」の時だけ実行されます.\n",
"したがって,「夢も希望もある大人」と出力されるのは,「`y < 18` が False かつ `y < 25` が True のとき」つまり「`y`が18以上25未満」のときだけですね. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Python では `elif` 文というものが使える. `else` と `if` を組み合わせた形**.\n",
"上記の例は,次のように書き直せる.先の例と同じように `y` の値をいろいろ変えて動作確認しよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 10\n",
"if y < 18:\n",
" print(\"こども\")\n",
"elif y < 25:\n",
" print(\"夢も希望もある大人\")\n",
"else:\n",
" print(\"夢も希望もある大人...が眩しく見える大人\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"-1\">(よだんだよん) Python には C言語の `switch` に相当するものはありません.`elif` ずらずら並べます.</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**注意**: 以下のセルを実行すると,最下行に入力待ちの箱が表示されるはず.入力すれば結果が出力されます.\n",
"実行が途中で止まってどうしようもない(`[*]`が出たままになる)ときは,↑の ■ ボタンを押していったん実行を停止させて実行をやり直すとよいでしょう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = int(input(\"【入力してね】 お歳は?\"))\n",
"print('{}歳か...'.format(y), end=' ')\n",
"if y < 18:\n",
" print(\"こども\")\n",
"elif y < 25:\n",
" print(\"夢も希望もある大人\")\n",
"else:\n",
" print(\"夢も希望もある大人...が眩しく見える大人\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"当然,for文やif文は組み合わせて使うことができる. `a_num` の値をいろいろ変えて実行してみよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# p.92 の例(ちょっと改良)\n",
"a_num = 57\n",
"for num in range(2, a_num//2+1): # range(a, b) は a から b-1 まで\n",
" if a_num % num == 0:\n",
" print(a_num, \"は素数ではありません\")\n",
" break # C言語同様にループを抜ける"
]
},
{
"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