Skip to content

Instantly share code, notes, and snippets.

@pon-x
Last active March 20, 2021 04:42
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 pon-x/119ff97821bb21feef197c00a0ca97a5 to your computer and use it in GitHub Desktop.
Save pon-x/119ff97821bb21feef197c00a0ca97a5 to your computer and use it in GitHub Desktop.
StartUpHubTokyo Pythonプログラミング講座 練習 20210320
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 現役高校教員が教えるPythonプログラミング講座"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 代表的なショートカットコマンド(かっこ内はMAC)\n",
"\n",
"|操作内容\t|コマンド\t|\n",
"|:-----------|:------------:|\n",
"|セルを実行し、下のセルに移動する\t|Shift + Enter|\n",
"|セルを実行する\t|Ctrl(?) + Enter|\n",
"|セルを実行し、下にセルを追加する\t|Alt + Enter|\n",
"|コメントアウト\t|Ctrl(?) + /|\n",
"|上にセルを追加する\t|Ctrl(?) + m -> a|\n",
"|下にセルを追加する\t|Ctrl(?) + m -> b|\n",
"|選択しているセルをカット\t|Ctrl(?) + m -> d|"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### このセルの上にセルを追加してみよう。\n",
"### このセルの下にセルを追加してみよう。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## プログラムを実行してみよう"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"123 + 456"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"123 * 456"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"10 ** 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(123 +456)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Python\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"1 + 1 =\", 1 + 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 変数を使おう"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"文字式$x+5$について、$x$の値を変えて変化を見てみよう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#x+5のxに2を代入する\n",
"#イコールの左に変数を、イコールの右に代入したい数をかく\n",
"x = 2\n",
"\n",
"x + 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#xにx+2を代入する。\n",
"x = 5\n",
"\n",
"x = x + 2\n",
"\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#xに2を足す。\n",
"x = 5\n",
"\n",
"x += 2\n",
"\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#xに2を引く。\n",
"x = 5\n",
"\n",
"x -= 2\n",
"\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#変数xに'py'を代入し、定数'thon'と連結する。\n",
"x = 'py'\n",
"\n",
"x + 'thon'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問題1\n",
"次のとおりプログラムを書いてみよう。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"あなたの誕生日の月をmに代入し、日をdに代入してください。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"mを2倍して、さらに5を足した結果をmmに代入してください。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"mmに50をかけて、dを足し、さらに250を引いてください。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"最後に出力された結果があなたの誕生日になっていれば、プログラムがうまく書けています。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## IF文"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$n$が20以上であれば「成年」を表示しましょう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 「if」の後にはコロン「:」をつける。\n",
"# 「if」の処理の中身の記述する際は、インデントをつける。\n",
" \n",
"n = 30\n",
"\n",
"if n >= 20:\n",
" print(\"成年\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# n = 15にしてみる。\n",
" \n",
"n = 15\n",
"\n",
"if n >= 20:\n",
" print(\"成年\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$n$が20以上であれば「成年」を表示し、そうでなければ「未成年」を表示しましょう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 「else」の後も「if」と同じように、コロン「:」やインデントをつける。\n",
"\n",
"n = 30\n",
"\n",
"if n >= 20:\n",
" print(\"成年\")\n",
"else:\n",
" print(\"未成年\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$n$が20未満であれば「未成年」を表示し、20であれば「はたち」と表示し、そうでなければ「成年」を表示しましょう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# nの値を変えて、何度か試してみましょう。\n",
"\n",
"n = 10\n",
"\n",
"if n < 20:\n",
" print(\"\") # ここに追記する\n",
"elif n == 20:\n",
" print(\"\") # ここに追記する\n",
"else:\n",
" print(\"\") # ここに追記する\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## FOR文"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range(<数値>)の使い方"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(10)で0から10までの範囲を作ることができる\n",
"range(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(10)の範囲の中身は、0以上10未満の整数\n",
"for n in range(10):\n",
" print(n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(1, 10)で1から10までの範囲を作ることができる\n",
"range(1, 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(1, 10)の範囲の中身は、1以上10未満の整数\n",
"for n in range(1, 10):\n",
" print(n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(1,10,2)で、2ごとに増える1から10までの範囲を作ることができる\n",
"range(1,10,2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#range(1,10)の範囲の中身は、2ごとに増える1以上10未満の整数\n",
"for n in range(1,10,2):\n",
" print(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問題2\n",
"次のとおりプログラムを書いてみよう。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1から100までの奇数を表示しましょう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1から100までの奇数のうち3の倍数のみを表示しましょう。<br>\n",
"ヒント : 5 % 3 ・・・ 5を3で割った余りを算出(この場合は2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <本日のメインテーマ>1%の確率であたるくじは、100回引けば1回はあたるのか!?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 理論値をかんがえてみる。\n",
"当たる確率は$\\frac{1}{100}$、\\\n",
"はずれる確率は$\\frac{99}{100}$\\\n",
"\\\n",
"\\\n",
"100回連続で外れる確率は、\\\n",
"$\\frac{99}{100} \\times \\frac{99}{100} \\times \\frac{99}{100} \\times \\cdots = (\\frac{99}{100})^{100}$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = (99 / 100) ** 100\n",
"\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ということは、100回引いて1回以上は当たる確率は、1から上記の確率を引けばよい。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.確率は、その事象が起こる場合と起こらない場合を足すと1になる。\n",
"\\\n",
"100回くじを引いて100回ともはずれる確率を上記の通り求めました。\\\n",
"なので、1から上記確率pをひいた値が、100回くじを引いて少なくとも1回は当たる確率になります。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 理論値が本当に正しいのかシミュレーションしてみよう。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"100回くじを引いて、当たりが1回以上出るかを確かめてみる。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1から100までの乱数を発生させる\n",
"import random\n",
"\n",
"random.randint(1, 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1から100のうち、1が当たり、2~100ははずれとします。\n",
"\n",
"i = random.randint(1, 100)\n",
"\n",
"if i == 1:\n",
" print(\"\") # ここに追記する\n",
"else:\n",
" print(\"\") # ここに追記する"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 100回くじをひく\n",
"\n",
"for r in range(100):\n",
" i = random.randint(1, 100)\n",
"\n",
" if i == 1:\n",
" print(\"当たり\")\n",
" else:\n",
" print(\"はずれ\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1回以上あたりが出るかをたしかめるだけなので、当たりが出たら処理を終わりにする。\n",
"for r in range(100):\n",
" i = random.randint(1, 100)\n",
"\n",
" if i == 1:\n",
" print(\"当たり\")\n",
" break # breakを使うと途中で処理を終える\n",
" else:\n",
" print(\"はずれ\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 上記の処理を繰り返す回数を格納する変数\n",
"lot = 1000\n",
"\n",
"\n",
"# 当たりの回数を格納する変数 => 初期値0\n",
"hit = 0\n",
"\n",
"\n",
"\n",
"# 1000回繰り返す\n",
"for r in range(lot):\n",
" \n",
" # 100回試行する\n",
" for i in range(100):\n",
" p = random.randint(1, 100)\n",
"\n",
" # あたったらループをぬける\n",
" if p == 1:\n",
" hit += 1\n",
" break # breakを使うと途中で処理を終える\n",
" \n",
"print(\"試行回数 : \", ) # ここに追記する\n",
"print(\"当たり  : \", ) # ここに追記する\n",
"print(\"確率  : \", ) # ここに追記する"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"試行回数が増えると理論値に近づいていくことを確認してみよう。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 10万回繰り返す\n",
"lot = 100000\n",
"\n",
"# 当たりの回数を格納する変数 => 初期値0\n",
"hit = 0\n",
"\n",
"for r in range(lot):\n",
"\n",
" for i in range(100):\n",
" i = random.randint(1, 100)\n",
"\n",
" if i == 1:\n",
" hit += 1\n",
" break\n",
"\n",
" if r != 0 and : # ここに追記する\n",
" \n",
" print(\"試行回数 : \", ) # ここに追記する\n",
" print(\"確率  : \", ) # ここに追記する\n",
" print(\"#--------------------------------#\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### おまけ グラフにしてみる"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 10万回繰り返す\n",
"lot = 100000\n",
"lotArray = []\n",
"\n",
"# 当たりの回数を格納する変数 => 初期値0\n",
"hit = 0\n",
"hitArray = []\n",
"\n",
"for r in range(lot):\n",
"\n",
" for i in range(100):\n",
" i = random.randint(1, 100)\n",
"\n",
" if i == 1:\n",
" hit += 1\n",
" break\n",
"\n",
" if r != 0 and r % 100 == 0:\n",
" \n",
" lotArray.append(r)\n",
" hitArray.append(hit / r)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## グラフを作成する\n",
"# 実測値と理論値を描画\n",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"\n",
"fig = plt.figure(figsize = (20, 10))\n",
"ax = fig.add_subplot(111)\n",
"ax.grid()\n",
"\n",
"ax.plot(lotArray, hitArray)\n",
"ax.plot([0, lot], [1-p, 1-p])\n",
"\n",
"ax.legend([\"measured value\", \"theoretical value\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下のサイトでは、円グラフで可視化してみました。\\\n",
"https://www.pon-x.jp/entry/2020/11/09/090651"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment