Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active September 26, 2020 07:24
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/1de897056186c09bc5ee2ea367b78552 to your computer and use it in GitHub Desktop.
Save takatakamanbou/1de897056186c09bc5ee2ea367b78552 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 その3\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.103,104)\n",
"\n",
"変数は,関数の内や外で定義できますが,定義した場所で扱われ方が変わるのかどうか考えてみましょう."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"まずは,関数定義の中で変数を定義してみる例."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def test_func(arg1):\n",
" # 数値の引数に100を足して表示する関数\n",
" inner_var = 100\n",
" print(arg1+inner_var)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_func(10) # 関数を呼び出す - 110と表示される\n",
"print(inner_var) # 関数内で定義した変数を表示(エラーになる)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"「`inner_var`なんてのは定義されてへん」ってエラーになった.\n",
"**関数定義のブロックの中で定義された変数は,その中だけの存在なので,その関数の外では使えない.**\n",
"これは,C言語の場合と同様である."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"じゃあ,同じ名前の変数があったらどうなるだろう?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def test_hoge(x):\n",
" hogehoge = 1314 # 変数 hogehoge に数を代入\n",
" return x + hogehoge"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hogehoge = 1818 # 変数 hogehoge に数を代入\n",
"rv = test_hoge(4649) # ここで hogehoge の値は変わるのか?\n",
"print(\"関数 test_hoge() の戻り値は\", rv, \"でっせ\")\n",
"print(\"変数 hogehoge の値は\", hogehoge, \"でっせ\") # hogehoge はいくつ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`test_hoge()` の中で `hogehoge` に `1314` を代入しているが,この変数 `hogehoge` は外の世界の変数 `hogehoge` とは別の存在なので,外の世界の変数 `hogehoge` には `test_hoge()` を呼び出す前の値が入ったままになっていることがわかる."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"それでは,次の例はどうなるだろう?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def test_hoge2(x):\n",
" return x + hogehoge # この関数の中では hogehoge 定義してないよ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hogehoge = 1818\n",
"rv = test_hoge2(4649) \n",
"print(\"関数 test_hoge() の戻り値は\", rv, \"でっせ\")\n",
"print(\"変数 hogehoge の値は\", hogehoge, \"でっせ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`test_hoge2()` の中には `hogehoge` という変数の定義はない.\n",
"しかし,外の世界には `hogehoge` という名前の変数が存在しており,`1818` という値だったので,その値を使った計算が実行された.\n",
"\n",
"見つけにくいバグの原因になるので,変数名にはなるべくユニークなもの(面白いという意味ではなく,一意なもの,他と異なるものという意味)を付けるようにした方がよい."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"さらにもう一例."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def test_hoge3(x):\n",
" return x + hogefugahenapiyo # この関数の中でも外でもこんな変数は使ってない"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rv = test_hoge3(4649) \n",
"print(\"関数 test_hoge() の戻り値は\", rv, \"でっせ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上の例のように,外でも定義されてない変数使ったら当然エラーになる."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"次は,pp.105,106の例."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# リストを受け取ってその要素の数の分散を返す関数\n",
"def calc_variance(a_list):\n",
"\n",
" total = sum(a_list) # リストの合計\n",
" length = len(a_list) # リストの要素数(長さ)\n",
" mean = total/length # 算術平均を求める\n",
" variance = 0 # 分散を計算するための変数\n",
"\n",
" for height in a_list:\n",
" variance = variance+(height-mean)**2 # 身長から平均を引いて二乗\n",
" variance = variance/len(monk_fish_team) # 足した数値を要素数で割って分散を求める\n",
"\n",
" return variance # 求めた分散を戻り値として返す"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# リストを定義する\n",
"monk_fish_team = [158, 157, 163, 157, 145]\n",
"volleyball_team = [143, 167, 170, 165]\n",
"pravda_team = [127, 172, 140, 160, 174]\n",
"\n",
"# 分散を計算する\n",
"monk_team_variance = calc_variance(monk_fish_team)\n",
"volley_team_variance = calc_variance(volleyball_team)\n",
"pravda_team_variance = calc_variance(pravda_team)\n",
"\n",
"# 標準偏差を計算する\n",
"print(monk_team_variance**0.5)\n",
"print(volley_team_variance**0.5)\n",
"print(pravda_team_variance**0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"「あんこうチーム(monk_fish_team)」,「アヒルさんチーム(volleyball_team)」,「プラウダ高校の5人(pravda_team)」の中では,プラウダ高校のひとたちの身長の分散が大きいようです."
]
}
],
"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