Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Created September 26, 2020 03:16
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/8c1148a9e2f83bf24fa158b3928b9125 to your computer and use it in GitHub Desktop.
Save takatakamanbou/8c1148a9e2f83bf24fa158b3928b9125 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 その2\n",
"\n",
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## 関数を使う (pp.93-106)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 関数とは,引数,戻り値 (pp.93-97)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 組み込み関数 abs を,引数として 10 を渡して呼び出す\n",
"abs(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"abs(-200)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下の文は上の2つ目の例の説明である.「★(1)★」などに当てはまるものを答えなさい.\n",
"\n",
"> 「★(1)★」という名前の関数に「★(2)★」として `-200` を渡して呼び出すと, `200` が返ってきた.関数が返す値(この例では `200`)は「★(3)★」値とも呼ばれる.\n",
"\n",
"正解は,以下に白字で書いてあります.マウスでドラッグして反転させると見えるはず.\n",
"- ★(1)★: <font color=\"white\"> abs </font>\n",
"- ★(2)★: <font color=\"white\"> 引数(ひきすう) </font>\n",
"- ★(3)★: <font color=\"white\"> 戻り(または返り) </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"組み込み関数 `int()` は,引数として渡されたものを整数に変換して返す関数である."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"10\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の例では引数は1つだが,実は2つ目の引数も指定することができる."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"10\", 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"10\", 16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"第二の引数に自然数 `p` を指定すると,第一の引数の内容を `p`進法で表現された数と解釈して変換した数を10進数で返す."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"ff\", 16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pythonの関数は複数の引数をとることができる.\n",
"それだけならC言語の関数と同じだが,上記の例に示すように,**渡すことのできる引数のうち一部だけを指定しても動作するように関数を作ることもできる**点は,C言語の場合とは異なっている.ただし,そのような関数の作り方は次回以降に学ぶ."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 関数を定義する (pp.97-102)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"関数の定義の仕方(引数なしの場合)\n",
"```\n",
"def 関数名():\n",
" 関数の中身のブロック\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 運命の戦車を教えてくれる関数\n",
"def destiny_tank():\n",
" tanks = [\"IV号戦車D型\", \"III号戦車J型\", \"チャーチル Mk.VII\",\n",
" \"M4シャーマン\", \"P40重戦車\", \"T-34/76\"] \n",
" num = input(\"好きな数字を入力してください:\")\n",
" idx = int(num) % len(tanks) # 入力値をリストの番号に変換. % のおかげで大きな数が入力されても大丈夫\n",
" print(\"あなたの運命の戦車は\")\n",
" print(tanks[idx])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 関数 destiny_tank() を呼び出す\n",
"destiny_tank() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"関数の定義の仕方(引数ありの場合)\n",
"```\n",
"def 関数名(引数1, 引数2, ...):\n",
" 関数の中身のブロック\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 運命の戦車を教えてくれる関数 ver.2\n",
"def destiny_tank2(num):\n",
" tanks = [\"IV号戦車D型\", \"III号戦車J型\", \"チャーチル Mk.VII\",\n",
" \"M4シャーマン\", \"P40重戦車\", \"T-34/76\"]\n",
" idx = num % len(tanks)\n",
" print(\"あなたの運命の戦車は\")\n",
" print(tanks[idx])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = input(\"なんか数入力して\") # input() の戻り値は文字列\n",
"\n",
"# x の値を int() 使って整数に変換したものを引数として関数 destiny_tank2() を呼ぶ\n",
"destiny_tank2(int(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import randint # random モジュールの randint() 関数を使えるようにするおまじない\n",
"x = randint(0, 100) # randint() の使い方が気になるひとは p.386\n",
"print(x)\n",
"destiny_tank2(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記のセルは実行するたびに異なる値が `x` に代入される.何度か実行してみよう."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"値を返したいときは,`return`文を使う"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 運命の戦車を教えてくれる関数 ver.3\n",
"def destiny_tank3(num):\n",
" tanks = [\"IV号戦車D型\", \"III号戦車J型\", \"チャーチル Mk.VII\",\n",
" \"M4シャーマン\", \"P40重戦車\", \"T-34/76\"]\n",
" idx = num % len(tanks)\n",
" return tanks[idx] # 結果を戻り値として返す => 戻り値は文字列"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import randint # 上で import してるはずだけどここだけ実行したときのために...\n",
"x = randint(0, 100)\n",
"tank = destiny_tank3(x)\n",
"print(\"今日あなたが乗るべき幸運の戦車は\", tank, \"です\")\n",
"print(\"明日あなたが乗るべき幸運の戦車は\", destiny_tank3(randint(0, 100)), \"です\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"関数の名前を間違えたら(存在しない関数を呼び出した)どうなるか実験してみよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"destiny_tank4(1818)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"というエラーになった. 「`destiny_tank4` なんちう名前は定義されてまへん」ってさ. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"関数 `destiny_tank()` は引数なしの関数やけど,引数渡して実行したらどうなるかな?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"destiny_tank(1818)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"「`destiny_tank()` は引数 0 個しか受け取らへんのに 1 つ渡されたで」って言っている. *argument* が引数.\n",
"*positional argument*ってのには意味があるのだけど,その話はいずれまた."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter Notebook では,セルを実行した順序が結果に影響する.\n",
"ある関数を定義したセルを実行せずにその関数を呼び出すセルを実行したら,当然エラーになる.\n",
"その時出るエラーは,上記の\n",
"> name `hoge` is not defined\n",
"\n",
"と同様.例えばこの notebook を後で復習しようとしたときとか,そういう目に遭ったら思い出そう."
]
}
],
"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