Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Created September 26, 2020 03:32
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/497521618464326b2fa460877d3a0fef to your computer and use it in GitHub Desktop.
Save takatakamanbou/497521618464326b2fa460877d3a0fef 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 ex02 Notebook その2\n",
"\n",
"AProgのページ https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## 複合演算子,型について (pp.58-63)\n",
"この部分は教科書では「文字列を使う」の一部となっていますが,内容的には文字列以外の話を含んでいるので,この資料ではセクションを独立させて説明することにしました."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.58,59を**読んで理解したら**,次のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lylic2 = \"ずっと笑顔ばかりを選んで\"\n",
"lylic2 += \"泣き顔見せるのを迷ってた\"\n",
"lylic2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 60\n",
"x -= 1\n",
"x *= 100\n",
"x += 63\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C言語等と違い,**Python にはインクリメント演算子 `++` やデクリメント演算子 `--` は存在しない**.以下はエラーになる."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**値を1つ増やしたければ,`+= 1`とする**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x += 1\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.60,61を**読んで理解したら**,次のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"day = 25\n",
"date = day + \"日\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記がエラーになる理由は,「変数`day`が表す整数型の値」と「文字列`\"日\"`」という,データ型が異なるもの同士を`+`演算子によって加算しようとしたからである.この例の場合,変数`day`が文字列を表すならばエラーにはならない.以下を実行してみよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"day = \"25\"\n",
"date = day + \"日\"\n",
"date"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C言語で変数を使う場合,`int`, `float`, `double`, `char`等の型をあらかじめ指定して宣言する必要があった.\n",
"`int x` と宣言した変数`x`に異なる型の値を代入しようとすると,エラーになるか,または無理やり変換される(`x = 1.23`とした場合など).\n",
"\n",
"しかし,Pythonの場合,**変数を使う際に「この変数は○○型」とあらかじめ決めておく必要はない**.上記の変数`day`の使用例から分かるように,`25`を代入することではじめは整数を表していた変数に,あとで`\"25\"`という文字列を代入しても問題ない.変数`day`は最初は整数型だったが,後から文字列型に変化した,ということになる.\n",
"\n",
"(**よだんだよん**)プログラミング言語の世界では,C言語のように変数の型を宣言時に決めておく(後で変えられない)ものを**静的型付け**,Python のようにあらかじめ決めておかない(実行中に変えられる)ものを**動的型付け**という.詳しいことが知りたいひとはこちらへ:\n",
"- [Wikipediaの「静的型付け」](https://ja.wikipedia.org/wiki/%E9%9D%99%E7%9A%84%E5%9E%8B%E4%BB%98%E3%81%91)\n",
"- [Wikipediaの「動的型付け」](https://ja.wikipedia.org/wiki/%E5%8B%95%E7%9A%84%E5%9E%8B%E4%BB%98%E3%81%91)\n",
"- [Wikipediaの「型推論」](https://ja.wikipedia.org/wiki/%E5%9E%8B%E6%8E%A8%E8%AB%96)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記は「Pythonにはデータ型の区別がない」ということを意味しているわけではないことに注意.**Pythonにもデータ型の区別はある**.これまでに,**整数型**,**浮動小数点型**,**文字列型**の3種類が登場している.\n",
"\n",
"**組み込み関数**(Pythonに標準で用意されている関数)の `int()`, `float()`, `str()` を使うことで,データ型を変換できる.pp.61,62を**読んで理解したら**,次のセルを**結果がどうなるかよく考えながら**実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(46.49)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"18\"*3) # 文字列として *3 してから int に変換するから..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int(\"hoge\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float(\"1.23e+1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(5959)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(123/10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"次のようにすれば,`day`は整数型のままで望みの結果が得られる."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"day = 25\n",
"date = str(day) + \"日\"\n",
"date"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## リストを使う(前半, pp.63-68)\n",
"ついでに簡単なグラフの描画も"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.63-65を**読んで理解したら**,次のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps = [15.1, 15.4, 15.2, 15.4, 17.0, 16.9] # 1950, 1960, 1970, 1980, 1990, 2000年の東京都の平均気温"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 以下は,Notebook上でグラフを描くための準備.今は何やってるかわからなくてもよい\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# リスト tokyo_temps の要素番号(0, 1, ...)を横軸に,要素の値を縦軸にグラフを描く.デフォルトは折れ線グラフ.\n",
"plt.plot(tokyo_temps)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.65-67を**読んで理解したら**,次のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[5] - tokyo_temps[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ここまでの話から,Pythonの**リスト**はC言語の**配列**に似てるように思えるかもしれない.しかし,実際には結構違うところがある.どこが違うか考えながらこの先の学習を進めてみよう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[6] # このリストは要素数が 6 個だから,要素番号(インデックス)は 0 から 5 まで.だからこれは..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[-1] # これはどうなる? C言語の配列だったらまともな動作はしないが..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[-2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[-6]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokyo_temps[-7]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 0 | 1 | 2 | 3 | 4 | 5 |\n",
"|:---:|:---:|:---:|:---:|:---:|:---:|\n",
"| 15.1 |15.4 |15.2| 15.4 | 17.0 | 16.9 |\n",
"| **-6** | **-5** |**-4** |**-3** | **-2** | **-1** |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.67-68を**読んで理解したら**,次のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e_tokyo_temps = [13.6, 13.5, 14.2, 14.8, 14.8] # 1900, 1910, 1920, 1930, 1940年の東京都の平均気温\n",
"tokyo_temps2 = e_tokyo_temps+tokyo_temps\n",
"tokyo_temps2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(tokyo_temps2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(よだんだよん) リストの話からはずれた余談.plt.plot() に2つのリストを指定すると,1つ目を横軸,2つ目を縦軸の値として使ってグラフを描いてくれる.2つのリストは要素数が一致してないといけない."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = [1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000]\n",
"plt.plot(y, tokyo_temps2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**リストの要素には様々なデータ型のものが混在しても構わない**(教科書では説明されてないところ).したがって,こんなリストもあり."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L = [ 1, 2, 3.14159, \"ほげ\", 5963]\n",
"print(L)\n",
"print(L[3])\n",
"print(L[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**リストもリストの要素になれる**ので,こんなんもあり."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L2 = [ 1, 2, 3.14159, \"ほげ\", [5, 9, 6, 3]]\n",
"print(L2)\n",
"print(L2[3])\n",
"print(L2[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## リストを使う(後半, pp.68-76)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.68-70を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz = [\"れに\", \"あかり\", \"かなこ\", \"しおり\", \"あやか\", \"ゆきな\"]\n",
"mcz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz[5] = 'ももか'\n",
"mcz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"del mcz[0]\n",
"print(mcz)\n",
"print(mcz[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下のセルの1行目,2行目に書き加えて,L の3番目(0から数えて3番目)の要素を `\"ふが\"` にして,2番目の要素を削除しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L[3] = \"ふが\"\n",
"del L[2]\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.70-72を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"momotamai = mcz[1:3]\n",
"momotamai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz[:2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcz[1:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の `[1:3]`, `[:2]`, `[1:]` のような要素番号の指定の仕方をすると,元のリストの一部を切り出したリストを得ることができる.\n",
"これを**スライス**という. 元のリストから要素が削除されるわけではない.\n",
"\n",
"`L`という名前のリストに対して `L[a:b]` とすると,`L[a]` から `L[b]` **の1つ手前**まで,すなわち `L[a]` から `L[b-1]` までの要素を含むリストが得られる.**`:`(コロン) の後ろに指定した番号の要素は含まれない**ということである.慣れないと間違いやすいので要注意.\n",
"\n",
"上記の例から分かるように,コロンの前/後を省略すると「先頭から」/「末尾まで」と指定することになる."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下のセルの2行目をスライスを使って書き換えて,出力が `['参', 4, '五', 6, 7, 'はち']` となるようにしなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L2 = [ 0, 1, 2.0, \"参\", 4, \"五\", 6, 7, \"はち\", 9, 10]\n",
"print(L2[8:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"同様に,`[0, 1, 2.0, '参']` および `['はち', 9, 10]` となるようにしなさい.コロンの前後どちらかの数を省略する書き方をすること."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"リスト `L2` は10個の要素を含んでいる.コロンの後の数を省略しない書き方で8番目以降の要素を取り出すにはどうしたらよいだろう.\n",
"考えてから,次の2つを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(L2[8:9])\n",
"print(L2[8:10])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#(よだんだよん)\n",
"print(L2[:])\n",
"print(L2[5:-1])\n",
"print(L2[8:3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.72-74を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"city_temps = [\n",
" [14.8, 14.8, 15.1, 15.4, 15.2, 15.4, 17.0, 16.9], # 東京都\n",
" [10.0, 10.4, 11.5, 11.2, 10.9, 10.6, 11.8, 12.2], # 秋田市\n",
" [16.0, 15.5, 15.9, 16.4, 15.9, 15.6, 17.5, 17.1] #熊本市\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"city_temps[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"city_temps[2][7]- city_temps[2][0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Notebook上でグラフを描くための準備\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.plot(city_temps[0]) # 東京都のグラフを描画\n",
"plt.plot(city_temps[1]) # 秋田市のグラフを描画\n",
"plt.plot(city_temps[2]) # 熊本市のグラフを描画"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記の2次元配列のようなものは,**リストのリスト**として実現している.C言語の2次元配列と違って,外側のリストの要素となるリストたちは要素数が異なっても構わないので,こんなリストも作れる."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L3 = [ mcz, [11, 22, 33], L2 ]\n",
"L3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L3[2][8]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.74,75を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"monk_fish_team = [158, 157, 163, 157, 145]\n",
"sum(monk_fish_team)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"max(monk_fish_team)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"min(monk_fish_team)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"上記で使っている `sum()`, `max()`, `min()` は,Pythonに標準で備わっている**組み込み関数**である."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pp.75,76を**読んで理解したら**,以下のセルを実行しなさい."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(monk_fish_team)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"monk_sum = sum(monk_fish_team) \n",
"monk_len = len(monk_fish_team) \n",
"monk_mean = monk_sum/monk_len\n",
"monk_mean"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.bar([0, 1, 2, 3, 4], monk_fish_team)\n",
"plt.plot([0, len(monk_fish_team)], [monk_mean, monk_mean], color='red')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`len()` も Python の組み込み関数である.引数にリストを渡せばその要素数を返してくれる.\n",
"**`len()`に文字列を渡すと**..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(\"ほげほげほげ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"-1\">(よだんだよん) グラフを描く matplotlib のバージョンが上がって,環境によっては上記では教科書の図と同じ見た目のグラフにならないかも.その場合は以下のようにしたらよいだろう.</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = [0, 1, 2, 3, 4]\n",
"plt.bar(x, monk_fish_team)\n",
"plt.plot([x[0], x[-1]], [monk_mean, monk_mean], color='red')"
]
},
{
"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