プログラミングのメモ

プログラミングの学び直し備忘録

Python:学習02

00 Python

Webフレームワーク

ライブラリ

科学/数学

  • SciPy(サイパイ)
  • scikit-learn(サイキット・ラーン)
  • Pandas(パンダス)
  • NumPy(ナンパイ)
  • Matpliolib(マットプロットリブ)

ディープラーニング

  • TensorFlow(テンソルフロー)
  • TFLearn(ティーエフラーン)
  • Chainer(チェイナー)
  • PyTorch(パイトーチ)
  • PyBrain(パイブレイン)

01 環境構築とHelloWorld

実環境

仮想環境(Anaconda)

ダウンロード

Anaconda | Individual Edition

インストール

仮想環境の作成

  • [Enviroment](左ペイン) →[Create](画面下)

  • ≪Create new enviroment≫ダイアログ
    ・「Name」:仮想環境名入力
    ・「Python」:チェック
    ・[Create]クリック

Jupyter Notebookで実行

Spyderで実行

02 データ型・オブジェクト・演算子・関数

リテラル・データ型

データ型 内容
数値 整数型(int)
浮動小数点数型(froat)
文字列 文字列型(str)
真偽 ブール型(bool)

数値型:整数(int)

10進

10

2進
>>> 0b1
1
>>> 0B10100
20
8進
>>> 0o1
1
>>> 0o26
22
16進
>>> 0x1
1
>>> 0xA6
166

数値型:浮動小数点数型(float)

0.000000000000001
1.0e-15

>>> 1.0e4
10000.0

文字列型:文字・文字列

真偽型:ブール型(bool)

None型:None型

オブジェクト・変数

演算子

関数・メソッド

03 条件分岐、繰り返し、リスト、関数

条件分岐

if

繰り返し

for

while

04 オブジェクト

クラス

インスタンスクラス
・スタティッククラス

メンバ変数

インスタンス変数
・クラス変数(スタティック変数)

メソッド

インスタンスメソッド
・クラスメソッド
・スタティックメソッド

プロパティ

・ゲッター
・セッター

#クラス
class MyClass:

    # クラス変数
    var_cls1 = 1
    var_cls2 = 2

    # コンストラクタ
    def __init__(self, arg1):
        #インスタンス変数
        self.var_inst1 = arg1
        self.var_inst2 = 3

    # メソッド:インスタンス
    def method_inst(self):
        print('method_inst -> '+ str(self.var_cls1))
        print('method_inst -> '+ str(self.var_cls2))

    # メソッド:クラス
    @classmethod
    def metod_cls(cls):
        print('metod_cls -> ' + str(cls.var_cls1))
        print('metod_cls -> ' + str(cls.var_cls2))

    # メソッド:スタティック
    @staticmethod
    def metod_static():
        print('metod_static')

#******************************************************
# メイン
#******************************************************
print('===[スタティック]===================')
MyClass.metod_static()

print('===[クラス]===================')
print(MyClass.var_cls1)
print(MyClass.var_cls2)
MyClass.metod_cls()

print('===[インスタンス]===================')
mc = MyClass(99)   # インスタンス生成
print(mc.var_inst1)
print(mc.var_inst2)

mc.method_inst()
mc.metod_cls()
mc.metod_static()

print('======================')
===[スタティック]===================
metod_static
===[クラス]===================
1
2
metod_cls -> 1
metod_cls -> 2
===[インスタンス]===================
99
3
method_inst -> 1
method_inst -> 2
metod_cls -> 1
metod_cls -> 2
metod_static
======================
'''
 インスタンス変数(self.変数)
  コンストラクタでインスタンス変数を初期化
'''
class MyCounter_inst:
    #コンストラクタ
    def __init__(self,max):
        #インスタンス変数
        self.max = max
        self.count = 0

    #メソッド
    def counter(self):
        print('cls -> ' + str(self.count))
        self.count += 1

'''
 クラス変数(static:静的メンバ)
'''
class MyCounter_static:
    #コンストラクタ
    #def __init__(self):
    #クラス変数(static)
    max = 5
    count = 0

    #メソッド
    def counter(self):
        print('cls -> ' + str(self.__class__.count))
        self.__class__.count += 1

#******************************************************
# メイン
#******************************************************

#===========================================
#インスタンス変数
#===========================================
mc = MyCounter_inst(3)   #3回以上で「OK」
for i in range(2):
    print(str(i))
    mc.counter()
    
print('===============')

#もう一度、最初から
print('もう一度、最初から')

#mc = MyCounter_inst() ※引数がないと、エラー
mc = MyCounter_inst(max = 3)
for i in range(5):
    if i >= mc.max :
        print('◆' + str(i))
        mc.counter()
    else:
        print(str(i))
        mc.counter()
print('===============')

print('######################################')

#===========================================
# クラス変数(static)
#===========================================
mc2 = MyCounter_static()
mc2.count = 3       #3回以上で「OK」
for i in range(2):
    print(str(i))
    mc2.counter()
print('===============')

#続きから
print('続きから')
mc2 = MyCounter_static()
for i in range(5):
    if i >= 5:
        print('◆' + str(i))
        mc2.counter()
    else:
        print(str(i) )
        mc2.counter()
print('===============')
0
cls -> 0
1
cls -> 1
===============
もう一度、最初から
0
cls -> 0
1
cls -> 1
2
cls -> 2
◆3
cls -> 3
◆4
cls -> 4
===============
######################################
0
cls -> 0
1
cls -> 1
===============
続きから
0
cls -> 2
1
cls -> 3
2
cls -> 4
3
cls -> 5
4
cls -> 6
===============

Python クラスについて - Qiita

05 GUI

06 機械学習

07 マルコフ連鎖で文章を作り出す

08 インターネットアクセス

09 ディープラーニングで画像認識

A1 Pythonの標準ライブラリ

A 2 Pythonの外部ライブラリ