SlideShare a Scribd company logo
1 of 52
K3Python講座2018 第5回
オブジェクト指向入門コース 第1回
前々回までの復習
(o・∇・o)ヤミーーーーーー
FOR
for [変数] in [リスト]:
# 実行するプログラム
リストの中身を順に実行していくもの!(Pythonでは)
他言語ではこれはforeachなどという名前であること
が多い
WHILE
while [条件式or真偽値]:
# 実行するプログラム
条件式or真偽値がTrue(真)である限り、
実行するプログラムを繰り返す
break,continue
FOR文/WHILE文から無理やり抜け出すbreak
一つしか抜け出せないので、入れ子になってい
る場合は工夫が必要
FOR文/WHILE文の途中で実行をやめ、それらの
最初に戻るのがcontinue
関数とは
関数:func(x)
print(x * x)
3
2
-2
変数の値(今回はx)のみを変えて、同じプログラムを実行する
場合によっては値を戻すこともある(数学の関数のイメージ)
9
4
4
関数とは
よく使う似たプログラム(変数のみ違う等)を1
箇所にまとめて、書くコードの量を減らすことが
出来る
そのプログラムに間違いがあったときも1箇所直
すだけでOK
予め用意された関数(printなど)は組み込み関
数と言う
関数
def [関数名](引数(複数ある場合カンマで区切る)):
# 実行するプログラム
return [返す値] # 戻す値があるときのみ
再帰関数
ぐるぐるぐるぐるグルコサミン
再帰とは
もう一度帰ってくること。
【デジタル大辞泉より】
再帰とは
① 再び帰ること。
② ヨーロッパ諸語の文法で、主語
と目的語が同一者であること。
【大辞林 第三版より】
再帰関数とは
関数の中で自分自身を呼び出すもの
イメージできますか?
再帰の例
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(10) plusplus(9) + 10
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(9) plusplus(8) + 9
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(8) plusplus(7) + 8
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(7) plusplus(6) + 7
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(6) plusplus(5) + 6
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(5) plusplus(4) + 5
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(4) plusplus(3) + 4
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(3) plusplus(2) + 3
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(2) plusplus(1) + 2
再帰構造
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(1) 1
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
plusplus(8)の結果がほしいよ!!
plusplus(2)
plusplus(1)の結果がほしいよ!!
plusplus(1)
1やで!!
1
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
plusplus(8)の結果がほしいよ!!
plusplus(2)
1 + 2 = 3
やで!!
3
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
36 + 9 = 45やで!!
45
plusplus(8) = 36
plusplus(10)
plusplus(9)の結果が
ほしいよ!!
45
plusplus(10) = 45 + 10 = 55
plusplus(10)で行われている計算
plusplus(10)
= plusplus(9) + 10
= plusplus(8) + 9 + 10
= plusplus(7) + 8 + 9 + 10
= plusplus(6) + 7 + 8 + 9 + 10
= plusplus(5) + 6 + 7 + 8 + 9 + 10
= plusplus(4) + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(3) + 4 + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(2) + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(1) + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
練習問題
階乗を行う関数を再帰関数を使って作れ。
def fact(n):
# 再帰を用いたプログラムを書く
# 今回はnに1以上が与えられるとして書いて良い
print(fact(10)) # 10の階乗(3628800)をプリント
def fact(n):
if n == 1:
return 1
else:
return n * fact(n - 1)
print(fact(10)) # 3628800
フラクタル
図形の一部を適当に持ってきた時、図形全体と相似な図形と
なる図形のこと。
プログラミングとしては一般に再帰関数で描画される図形
有名なフラクタル
カントール集合
シェルピンスキーのギャスケット
コッホ曲線
フラクタル①
シェルピンスキーのギャスケット
フラクタル②
コッホ曲線
【ちょっと休憩】
Git/GitHubについて
便利だよ!!(*>△<)(天゚∀゚)
Gitって何
こういうのダメだよね
Git
バージョン管理を簡単にしてくれるツールです
詳しい説明は省きますが、簡単に保存した好きな時期
のファイルに戻したり、並列して同じファイルに違う
変更を加えていったり出来ます
興味がある方は各自で調べてみてくださいませ
GitHub
Gitで管理しているファイルをアップロードして公開できるサイト
(クラウドの一種)
イメージとしてはコードにとってのミニSNSのようなもの
企業の方が採用に使うこともある
非公開のところでは企業が実際のソフトウェアを共同開発してい
たりするようです
オブジェクト指向超入門
オブジェクト指向プログラミングの考え方について
キャラクターを作る
RPGのキャラクターを作るのに必要な変数は…?
体力
攻撃力
防御力
素早さ
画像
etc…
リストを使う
どの数字がどの値だか分かりにくい…
player1 = [100, 30, 20, 10, “pl1.jpg”]
HP = player1[0]
ImageUrl = player1[4]
辞書を使う
さっきよりは分かりやすいけど、
ダブルクオーテーションが多すぎて書きにくい
player1 = {"hp":100, "atk":30, "def":20,
"spd":10, "url":"pl1.jpg"}
HP = player1[“hp”]
ImageUrl = player1[“url”]
変数だけで頑張る
見やすく書きやすいが、行数を食う
動的に、即ち繰り返しなどを用いて複数作れない
pl1_hp = 100
pl1_atk = 30
pl1_def = 20
pl1_spd = 10
pl1_url = "pl1.jpg"
クラスを使おうというご提案
クラスとは、変数を集め、それを扱うため
の関数を機能として付属させた設計図
のようなもの
クラスを使おうというご提案
クラスを使おうというご提案
プレイヤークラスを基に生成
player1インスタンス
hp = 100, atk = 30, defe = 20
spd = 10, url = “pl1.jpg”
player1.hpやplayer1.atkのように
[インスタンス名].[変数名]で扱う
Playerクラス
変数: hp, atk, defe, spd, url
関数: damage(atk): 相手の攻撃力を引数
として取り、ダメージを受ける
player3インスタンス
player2インスタンス
クラスを使おう
player1インスタンス
player1.hp =
100
player1.atk =
30
player1.defe =
100
player1.spd =
10
player1.url =
“pl1.jpg”
__init__関数
インスタンスを作るときに
呼び出される
damage関数
hpをへらすための関数
コードにしてみる
class Player:
def __init__(self, _hp, _atk, _def, _spd, _url): # 変数の初期設定
self.hp = _hp
self.atk = _atk
self.defe = _def
self.spd = _spd
self.url = _url
def damage(self, enemyAtk):
dmg = enemyAtk - self.defe
if dmg < 1:
dmg = 1
self.hp -= dmg
各変数の値を設定する
selfが頭についているものはクラス全体の変数
selfが頭についていない変数はその関数が終わったら死ぬ変数
最初の引数はクラス自体を表す
コードにしてみる
player1 = Player(100, 30, 20, 10, "pl1.jpg")
print(player1.hp) # HPの100を出力
player1.damage(50) # Atk=50で攻撃され30ダメージ
print(player1.hp) # HPの70を出力
player2 = Player(100, 80, 10, 5, "pl2.jpg")
player1.damage(player2.atk) # pl2からpl1に攻撃
# Atk=80で攻撃され60ダメージ
print(player1.hp) # HPの10を出力
練習問題①
先程あげたPlayerクラス(Scrapboxに載っています)
に、そのプレイヤーの情報をプリントする関数
statusを追加してください。
player1 = Player(100, 30, 20, 10, "pl1.jpg")
player1.status()
# HP: 100, Atk: 30, Def: 20, Spd: 10
# というような出力ができればOK
練習問題②-トランプの大枠を作る
①トランプを表すCardクラスを作る
変数:suit(スート), number(数字)
関数:__init__(初期化子)のみ
②山札を表すDeckクラスを作る
変数:cards(Cardのリスト)
関数:__init__(ここでcardsにトランプ52枚を入れシャッフル),
draw(カードを引く関数=1つ選びリストから抜く&returnで返す)
余裕のある人はTkやpygameを使った描画関数の作成等もチャレンジ!
練習問題②-トランプの大枠を作る
class Card:
def __init__(self, _suit, _number):
# (self.)suitと(self.)numberを定義
class Deck:
def __init__(self):
# (self.)cardsの定義&シャッフル
# シャッフルはrandom.shuffle(リスト)がオススメ
def draw(self):
# 1枚カードを引き、取り除いてreturn
練習問題②-トランプの大枠を作る
# テスト用プログラム例
deck = Deck()
for c in deck.cards:
print("{} {}".format(c.suit,c.number))
while len(deck.cards) != 0:
card = deck.draw()
print("You draw:{} {}".format(card.suit,card.number))
# これで全カードが二度出力されれば良い
# 順番は同じでなくてもOK→Deckクラスのdraw関数に依存

More Related Content

Recently uploaded

MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。iPride Co., Ltd.
 
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイルLoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイルCRI Japan, Inc.
 
情報を表現するときのポイント
情報を表現するときのポイント情報を表現するときのポイント
情報を表現するときのポイントonozaty
 
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアルLoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアルCRI Japan, Inc.
 
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdfネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdfTakayuki Nakayama
 
Keywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltdKeywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltdkokinagano2
 
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用KLab Inc. / Tech
 
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介Hyperleger Tokyo Meetup
 

Recently uploaded (8)

MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
 
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイルLoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
 
情報を表現するときのポイント
情報を表現するときのポイント情報を表現するときのポイント
情報を表現するときのポイント
 
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアルLoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
 
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdfネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
 
Keywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltdKeywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltd
 
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
 
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
 

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

K3Python2018 OOP-01 FULL