---
title: pf-6. 式の抽象化と関数
tags:  #python #google colaboratory #関数 #式の抽象化 #関数定義 #def #return #金子邦彦研究室  
author: [kunihikokaneko](https://www.docswell.com/user/6674398749)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/4EQX4LGL7P.jpg?width=480
description: トピックス：Python, Google Colaboratory, 関数, 式の抽象化, 関数定義, def, return  Python 入門（Google Colaboratory を使用）（全８回） https://www.kkaneko.jp/pro/pf/index.html  金子邦彦研究室ホームページ https://www.kkaneko.jp/index.html
published: January 29, 23
canonical: https://www.docswell.com/s/6674398749/ZXMQYK-2023-01-29-133521
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/4EQX4LGL7P.jpg)

pf-6. 式の抽象化と関数
（Python 入門，Google Colaboratory を使用）
（全８回）
URL: https://www.kkaneko.jp/pro/pf/index.html
金子邦彦
1


# Page. 2

![Page Image](https://bcdn.docswell.com/page/KJ46QD5571.jpg)

関数
• この関数の本体は
「return a * 1.1」
• この関数は，式「a * 1.1」に，名前 foo を付
けたものと考えることもできる
2


# Page. 3

![Page Image](https://bcdn.docswell.com/page/LE1VPZW27G.jpg)

式の抽象化
100 * 1.1
150 * 1.1
400 * 1.1
類似した複数の式
a * 1.1
変数 a を使って，複数
の式を１つにまとめる
（抽象化）
3


# Page. 4

![Page Image](https://bcdn.docswell.com/page/GEWL296272.jpg)

式
100 * 1.1
150 * 1.1
400 * 1.1
変数を含む式
関数
a * 1.1
def foo(a):
return a * 1.1
この関数は，式「a *
1.1」に，名前 foo を
付けたものと考えるこ
ともできる


# Page. 5

![Page Image](https://bcdn.docswell.com/page/47ZMD9Y4E3.jpg)

関数
100 * 1.1
a * 1.1
150 * 1.1
400 * 1.1
類似した複数の式
変数 a を使って，複数
の式を１つにまとめる
（抽象化）
式「a * 1.1」を含む
関数 foo を定義
関数 foo を使用．
100, 150, 400 は引数
5


# Page. 6

![Page Image](https://bcdn.docswell.com/page/YJ6N1KDGJV.jpg)

式の抽象化と関数
抽象化前
抽象化後
類似した複数の式
関数の定義と使用
実行結果
同じ
実行結果になる
6


# Page. 7

![Page Image](https://bcdn.docswell.com/page/GJ554P3XJ4.jpg)

抽象化がなぜ大切なのか
• プログラミングでの根本問題は何でしょうか？
• 誤り（バグ）の無いプログラムの作成
• プログラミングの一番の基礎は何でしょうか？
• 抽象化を行うこと．
• 抽象化により，繰り返し同じことを書くことが減り，
バグを防げる．
• プログラムの変更も簡単に．
7


# Page. 8

![Page Image](https://bcdn.docswell.com/page/9E2NV6ZQ7R.jpg)

演習
資料：9 ～ 13
【トピックス】
• 関数
• def
8


# Page. 9

![Page Image](https://bcdn.docswell.com/page/D7YP39RY7M.jpg)

① Google Colaboratory のWebページを開く
https://colab.research.google.com
9


# Page. 10

![Page Image](https://bcdn.docswell.com/page/VEN5VLDRJ8.jpg)

② 「ファイル」で，「ノートブックを新規作成」を選ぶ
③ Google アカウントでのログインが求められたときはログ
インする
10


# Page. 11

![Page Image](https://bcdn.docswell.com/page/Y79Y64GZE3.jpg)

④ コードセルを新規作成し，Python プログラムを入れる
def foo(a):
return a * 1.1
すべて半角文字
「*」は掛け算の記号
print(foo(100))
def foo(a) の直後に「:」
print(foo(150))
字下げ．
（ここでは，半角の空白を 2つ）
print(foo(400))
⑤ 実行結果を確認
11


# Page. 12

![Page Image](https://bcdn.docswell.com/page/G786ZQ8YJD.jpg)

• これでは動かない
正しくない字下げ
正しい字下げ
ここでは，半角の空白を 2つ
12


# Page. 13

![Page Image](https://bcdn.docswell.com/page/L7L9DXR9ER.jpg)

4x + 1 = 0 を解く
（Python の scipy.ptimize を利用）
⑥ コードセルを新規作成し，Python プログラムを入れる
import scipy.optimize
def foo(x) の直後に「:」
def foo(x):
字下げ．
（ここでは，半角の空白を 2つ）
return 4 * x + 1
print( scipy.optimize.fsolve(foo, 10) )
⑦ 実行結果を確認
13


# Page. 14

![Page Image](https://bcdn.docswell.com/page/4EMZPLRV7W.jpg)

Python 関連ページ
• Python まとめページ
https://www.kkaneko.jp/tools/man/python.html
• Python プログラミングの基本
Python Tutor, VisuAlgo, Code Combat を使用
https://www.kkaneko.jp/pro/po/index.html
• Python プログラム例
https://www.kkaneko.jp/pro/python/index.html
• 人工知能の実行（Google Colaboratory を使用）
https://www.kkaneko.jp/ai/ni/index.html
• 人工知能の実行（Python を使用）（Windows 上）
https://www.kkaneko.jp/ai/deepim/index.html
14


