山内セミナーⅠ(2022/06/15)

関連サイトと資料

サンプルプログラム - モジュールの使い方

import random
  
print(random.randint(1, 20))
    

import random as rnd
  
print(rnd.randint(1, 20))
    

from random import randint
  
print(randint(1, 20))
    

from random import randrange, randint

print(randint(1, 20))
print(random.randrange(1, 20))
    

import matplotlib.pyplot as plt
  
def fit(x, y):
  x_mean = sum(x) / len(x)
  y_mean = sum(y) / len(y)
  
  x_centered = [item - x_mean for item in x]
  y_centered = [item - y_mean for item in y]
  
  xy = [item_x * item_y for item_x, item_y in zip(x_centered, y_centered)]
  xx = [item * item for item in x_centered]
  sum_xy = sum(xy)
  sum_xx = sum(xx)

  a = sum_xy / sum_xx
  b = y_mean - a * x_mean
  
  return (a, b)
  
# サンプルデータ
x = [-0.03, 0.78, 2.07, 2.77, 4.10, 5.38, 5.99, 6.84, 8.12, 8.89, 9.43]
y = [0.88, 2.45, 2.43, 4.07, 5.49, 6.46, 7.02, 8.27, 8.70, 10.23, 10.65]
    
# 一次関数の推測を行う
a, b = fit(x, y)
  
y2 = [num * a + b for num in x]
plt.plot(x, y2)
plt.plot(x, y, 'o')
plt.show()
    

サンプルプログラム - モジュールの作り方

myutil.py
def fact(x):
    result = 1
    if x == 0:
        return result
    for num in range(1, x + 1):
        result *= num
    return result
  
def fizzbuzz(x):
    result = str(x)
    if x % 3 == 0 and x % 5 == 0:
        result = 'FizzBuzz'
    elif x % 3 == 0:
        result = 'Fizz'
    elif x % 5 == 0:
        result = 'Buzz'
    return result
  
def fib(x):
    if x == 0:
        return 0
    elif x == 1:
        return 1
    return fib(x - 1) + fib(x - 2)
  
PI = 3.14159
    

import myutil  # myutilモジュールをインポート
from myutil import fib  # myutilモジュールからfib関数をインポート
from myutil import fizzbuzz as fb  # myutil.fizzbuzz関数をfbとしてインポート
  
print(myutil.PI)  # myutilモジュールで定義されている変数PIを利用
  
for num in range(5):
  print(myutil.fact(num))  # myutilモジュールのfact関数を利用
  
for num in range(5):
  print(fib(num))
  
for num in range(1, 6):
  print(fb(num))
    

somemod.py
def _myhelper(x):
    return x * 2
  
def myfunc1(x):
    result = _myhelper(x)
    return result
  
def myfunc2(x):
    result = _myhelper(x) * 2
    return result
    

from somemod import *
  
print(myfunc1(2))
print(myfunc2(2))
print(_myhelper(2))
    

import somemod
print(somemod._myhelper(2))
    

import sys
print(sys.path)
    

サンプルプログラム - パッケージ

mypkg/mymath.py
def fact(x):
  result = 1
  if x == 0:
    return result
  for num in range(1, x + 1):
    result *= num
  return result
  
def fizzbuzz(x):
  result = str(x)
  if x % 3 == 0 and x % 5 == 0:
    result = 'FizzBuzz'
  elif x % 3 == 0:
    result = 'Fizz'
  elif x % 5 == 0:
    result = 'Buzz'
  return result
  
def fib(x):
  if x == 0:
    return 0
  elif x == 1:
    return 1
  return fib(x - 1) + fib(x - 2)
  
PI = 3.14159
    

mypkg/greet.py
def hello(whom):
  return 'Hello ' + str(whom)
    

mypkg/__init__.py
    

import mypkg
print(mypkg.mymath.fact(5))
    

import mypkg.mymath
print(mypkg.mymath.fact(5))
    

from mypkg import mymath  # mypkgパッケージのmymathモジュールをインポート
from mypkg.mymath import fact  # mypkg.mymathモジュールからfact関数をインポート
  
print(fact(5))
print(mymath.fizzbuzz(15))
    

print(dir())
from mypkg import *
print(dir())
    

mypkg/__init__.py
from mypkg.mymath import fact, fizzbuzz, fib
from mypkg.greet import hello
__all__ = ['fact', 'fizzbuzz', 'fib', 'hello']
    

サンプルプログラム - クラスの基礎知識

class Point:
  def __init__(self, x=0.0, y=0.0):
    self.x = x
    self.y = y
    

point1 = Point(1.0, 1.0)
point2 = Point()
print(f'point1: ({point1.x}, ({point1.y})')
print(f'point2: ({point2.x}, ({point2.y})')
    

from math import sqrt
print(sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2))
    

from math import sqrt
  
class Point:
  def __init__(self, x=0.0, y=0.0):
    self.x = x
    self.y = y
  
  def difference(self, point=None):
    if not point:
      point = Point()  # 原点を表すPointクラスのインスタンスを生成
    return sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2)
    

point1 = Point(1.0, 1.0)
point2 = Point()
point3 = Point(5, 4)
  
print(point1.difference(point2))
print(point1.difference())
print(point3.difference(point1))