「.NET 開発基盤部会 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
複数のプログラミングパラダイムに対応している。
Python 2.x はレガシー
以下のインストーラーから(推奨)で使用しているもの。
>python
>python Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z >
>python --version Python 3.9.5
pip3 install XXXX
>python -m pip install --upgrade pip
#ref(): File not found: "VSCode.png" at page "Python"
python --version
python
>>>1+1 2
>>>type(10) <class 'int'>
>>>x=10 >>>print(x) 10
>>>a=[1,2,3,4,5] >>>print(a) [1,2,3,4,5] >>>a[0] 1 >>>a[4] 5 >>>a[4]=99 >>>a[4] 99 >>>print(a) [1,2,3,4,99]
>>>dic={'hoge1':1} >>>dic['hoge2']=2 >>>print(dic) {'hoge1': 1, 'hoge2': 2}
>>>hungry = True >>>sleepy = False >>>type(hungry) <class 'bool'> >>>not hungry False >>>hungry and sleepy False >>>hungry or sleepy True
>>>hungry = True >>>if hungry: ... print("im hungry") # 半角スペースのインデントが必要 ... im hungry >>>hungry = False >>>if hungry: ... print("im hungry") # 半角スペースのインデントが必要 ...else: ... print("im not hungry") # 半角スペースのインデントが必要 ... print("im sleepy") ... im not hungry im sleepy
>>>for i in [1,2,3]: ... print(i) # 半角スペースのインデントが必要 ... 1 2 3
>>>def hello(): ... print("hello!") ... >>>hello() hello! >>>def hello(object): ... print("hello " + object + "!") ... >>>hello("hoge") hoge hello!
対話モードを終了するには、 exit() を実行する。
テキストファイルに
print("im hungry")
と書いて、拡張子を*.pyとして保存する(例 hungry.py)。
cdでカレントディレクトリに移動して、
>python hungry.py im hungry
class クラス名: def __init__(self, 引数, ...): #コンストラクタ ... def メソッド名1(self, 引数, ...): #メソッド1 ... def メソッド名2(self, 引数, ...): #メソッド2 ...
class Man: def __init__(self, name): self.name=name print("inited!") def hello(self): print("hello " + self.name + "!") def goodbye(self): print("good-bye " + self.name + "!") m=Man("hoge") m.hello() m.goodbye()
>>> import numpy as np >>> a=np.array([1,2,3,4]) >>> print(a) [1 2 3 4] >>> np.ndim(a) # 配列の次元 1 >>> a.shape # 配列の形状 (4,) >>> a.shape[0] # 次元の要素数 4
行列とは、
列↓ ┌ ┐ 行│○ ○│ →│ │ │○ ○│ └ ┘
>>> a=np.array([[1,2],[3,4],[5,6]]) >>> print(a) [[1 2] [3 4] [5 6]] >>> np.ndim(a) # 配列の次元 2 >>> a.shape # 配列の形状 (3, 2)
A行列 * B行列 =C行列
┌ ┐┌ ┐ ┌ ┐ │a1 b1││a2 b2│ │a1a2+b1c2 a1b2+b1d2│ │ ││ │ = │ │ │c1 d1││c2 d2│ │c1a2+d1c2 c1b2+d1d2│ └ ┘└ ┘ └ ┘ A行列 B行列 C行列 2行2列 2行2列 2行2列
┌ ┐┌ ┐ ┌ ┐ │a1 b1││a2 b2 c2 d2│ │a1a2+b1e2 a1b2+b1f2 a1c2+b1g2 a1d2+b1h2│ │ ││ │ │ │ │c1 d1││e2 f2 g2 h2│ = │c1a2+d1e2 c1b2+d1f2 c1c2+d1g2 c1d2+d1h2│ │ │└ ┘ │ │ │e1 f1│ │e1a2+f1e2 e1b2+f1f2 e1c2+f1g2 e1d2+f1h2│ └ ┘ └ ┘ A行列 B行列 C行列 3行2列 2行4列 3行4列
┌ ┐┌ ┐ ┌ ┐ │a1 b1││a2│ │a1a2+b1b2│ │ ││ │ = │ │ │c1 d1││b2│ │c1a2+d1b2│ └ ┘└ ┘ └ ┘ A行列 B行列 C行列 2行2列 2行1列 2行1列
NumPyのdot(ドット積)メソッドを使用する。
>>> a=np.array([[1,2,3],[4,5,6]]) >>> b=np.array([[1,2],[3,4],[5,6]]) >>> c=np.array([[1,2],[3,4]]) >>> d=np.array([7,8]) >>> a array([[1, 2, 3], [4, 5, 6]]) >>> b array([[1, 2], [3, 4], [5, 6]]) >>> c array([[1, 2], [3, 4]]) >>> d array([7, 8]) >>> np.dot(a, b) array([[22, 28], [49, 64]]) >>> np.dot(b, c) array([[ 7, 10], [15, 22], [23, 34]]) >>> np.dot(b, d) array([23, 53, 83]) >>> np.dot(a, c) # A行列の列数とB行列の行数が一致しない。 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: shapes (2,3) and (2,2) not aligned: 3 (dim 1) != 2 (dim 0)