Skip to content

Instantly share code, notes, and snippets.

@pon-x
Created February 15, 2021 00:49
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/4bcba56e15975f9a6a5f200347777511 to your computer and use it in GitHub Desktop.
Save pon-x/4bcba56e15975f9a6a5f200347777511 to your computer and use it in GitHub Desktop.
StartUpHubTokyo Pythonプログラミング講座 練習ノート 20210228
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": [
"5 + 7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(3 + 6)\n",
"print(9 + 2)"
]
},
{
"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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# キーボード入力\n",
"\n",
"input(\"入力してください : \")"
]
},
{
"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": [
"m = \n",
"d = "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"mを2倍して、さらに5を足した結果をmmに代入してください。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mm = "
]
},
{
"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": [
"## 論理演算\n",
"論理積(AND条件)、論理和(OR条件)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## まずはbool型の説明\n",
"\n",
"5 == 5 # 真"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5 == 6 # 偽"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## いずれかが真なら、真になるのがOR条件\n",
"\n",
"if 1 == 2 or 1 == 1:\n",
" print(\"A\")\n",
"else:\n",
" print(\"B\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## いずれも真なら、真になるのがAND条件\n",
"\n",
"if 1 == 2 and 1 == 1:\n",
" print(\"A\")\n",
"else:\n",
" print(\"B\")"
]
},
{
"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": [
"## 繰り返す回数が決まっていない場合はWhileを使う。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1から1ずつ増えていくが、9を超えたら終わる。\n",
"\n",
"i = 1\n",
"\n",
"while i <= 9:\n",
" print(i)\n",
" i += 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# こんなやりかたもある\n",
"\n",
"i = 1\n",
"\n",
"while True:\n",
" print(i)\n",
" i += 1\n",
" if i > 9:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## おみくじをつくろう。"
]
},
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/markdown"
},
"source": [
"> 作業手順\n",
"1. 乱数を発生させる。\n",
"1. 乱数とおみくじの関係を定義する。\n",
"1. 連続してひく。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> おみくじの種類は以下の通りにします。 \n",
"大吉、中吉、小吉、末吉、凶"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.乱数を発生させる。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"i = random.randint(0,4)\n",
"\n",
"i"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.乱数とおみくじの関係を定義する。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#-----------------乱数----------------\n",
"\n",
"i = random.randint(0,4)\n",
"\n",
"#-----------------乱数----------------\n",
"\n",
"\n",
"\n",
"\n",
"#------------------- i をおみくじとして解釈して表示する -------------------\n",
"# 相手の手の確認\n",
"if i == 0:\n",
" print(\"大吉\")\n",
"elif i == 1:\n",
" print(\"中吉\")\n",
"・・・\n",
"#------------------- i をおみくじとして解釈して表示する -------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.連続してひく。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
" \n",
" \n",
" #-----------------乱数----------------\n",
"\n",
" i = random.randint(0,4)\n",
"\n",
" #-----------------乱数----------------\n",
"\n",
"\n",
"\n",
"\n",
" #------------------- i をおみくじとして解釈して表示する -------------------\n",
" # 相手の手の確認\n",
" if i == 0:\n",
" omikuji = \"大吉\"\n",
" elif i == 1:\n",
" omikuji = \"中吉\"\n",
" elif i == 2:\n",
" omikuji = \"小吉\"\n",
" elif i == 3:\n",
" omikuji = \"末吉\"\n",
" else:\n",
" omikuji = \"凶\"\n",
" \n",
" print(\"###\" * 8)\n",
" print(\"###\", omikuji, \" がでました\", \"###\")\n",
" print(\"###\" * 8)\n",
" #------------------- i をおみくじとして解釈して表示する -------------------\n",
" \n",
" \n",
" \n",
" \n",
" ##################################################### \n",
" ### 勝敗がついたらwhileのループを抜けてください。###\n",
" ##################################################### \n",
" \n",
" \n",
" next = input(\"もう一回引く場合は「Y」を押してください!!\\n\")\n",
"\n",
" \n",
" ##### ↓↓IF文でループを抜ける処理をかく↓↓ #####\n",
" \n",
"\n",
" \n",
" ##### ↑↑IF文でループを抜ける処理をかく↑↑ ##### \n",
"\n",
" \n",
" \n",
" \n"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment