【lesson2】Pythonの基本(1) 演算子、フローコントロール
データ型
※単純型と複合型を区分せず(後ろのほうは複合型だけど)
たくさんありすぎで、覚えきれない。使うとき、その都度勉強すればいいと思う。
詳しくは下記を参照。
boolean型(真偽値)
number型(数値)
整数型(int)
浮動(ふどう)小数点型(float)
複素数(complex)
※8進数、10進数、16進数が存在している
String型(文字列)
※エスケープ:\を使う:'I\'m Hiroki' 'Hello\nworld'
※三重クォート:"""
Byte型(バイト)
List型 (リスト)
Tuple型(タプル)
Range型(数値シーケンス)
set型(集合体)
dict型(マッピング、辞書)
演算子
| Operation | Result | Full documentation | 
|---|---|---|
| x + y | sum of x and y 足し算 | |
| x - y | difference of x and y 引き算 | |
| x * y | product of x and y 掛け算 | |
| x / y | quotient of x and y 割り算 | |
| x // y | floored quotient of x and y 割り算のあまりを省いたもの | |
| x % y | remainder of x / y 割り算の余り | |
| -x | x negated | |
| +x | x unchanged | |
| abs(x) | absolute value or magnitude of x | abs() | 
| int(x) | x converted to integer | int() | 
| float(x) | x converted to floating point | float() | 
| complex(re, im) | a complex number with real part re, imaginary part im. im defaults to zero. | complex() | 
| c.conjugate() | conjugate of the complex number c | |
| divmod(x, y) | the pair (x // y, x % y) | divmod() | 
| pow(x, y) | x to the power y | pow() | 
| x ** y | x to the power y | 
詳しくは下記を参照。
分岐
if, elif, else
if [condition]:
    <do some thing>
elif [other condition]:
    <do some other things>
else:
    <do some thing>
ループ
for
for [variable] in [scope]:
    <loop>
else:
    <end loop do this (if break from loop skip this)>
while [condition]:
    <loop>
else:
    <end loop do this (if break from loop skip this)>
例外処理(try, except, else, finally, raise)
try:
[try to do sth]
[try to do sth]
[except [expression 1]:
    [exception1 process...]
[except [expression 2]:
    [exception2 process...]
....
[except [expression n]:
    [exception n process...]
[else:    # if not in above expression    else process...]
    [do process that are not listed above]
    [final process...]
raise NameError('except msg')
raise ValueError(1)
with構文(with)
with expression [as target] [, expression [as target]]... :
    suite...
即ち、with構文を用いると、withブロックが開始するときに自動的に初期化を行い、終了した際に、オブジェクトの終了処理が自動的に呼ばれます。
例えば、with構文を使ってファイル処理を行うと、わざわざファイルをcloseしなくて、自動的に行ってくれる。
wfp = open('filename.txt', 'w')
wfp.write('file operation without with statement.')
wfp.close()    # ファイルを閉じないといけない
with open('filename.txt', 'w') as wfp:
    wfp.write('file operation with \'with\' statement.')
    wfp.write('when exit \'with\' block, file is automatically closed')
    # close処理を呼ばなくても、自動的に実行してくれる
import tensorflow as tf
with tf.Session():        #with構文で自動的に初期化と終了処理を行う
    input1 = tf.constant([12.0, 1.0, 1.0, 1.0])
    input2 = tf.constant([2.0, 2.0, 2.0, 2.0])
    output = tf.add(input1, input2)
    result = output.eval()
    print("result: ", result)
関数の定義
def構文を用いて、関数を定義する。
def add(x, y):
    return (x + y)
def factorial(x):
    if x < 0:
        return 0    #err: cannot calculate factorial of negative number
    elif x==0:
        return 1
    else:
        return x * factorial(x-1)
factorial(5)
120
引数のデフォルト値
関数を定義するとき、デフォルト値を定義することができる。引数の値を指定していない場合はデフォルト値を使う。
def test_function_3(arg1, arg2 = "default value"):
    pass
可変長引数
関数定義時に引数名に*か**をつけると可変長引数となり、呼び出し時に任意の数の引数を指定することができる。*は複数のtuple引数
**は複数のキーワード引数
tuple引数
def func1(*arg):
    for i in arg:
        print("arg=",i)
func1(1,3,4,65,5)
1,3,4,65,5
def func2(**kwarg):
    for i in kwarg.keys():
        print("key=",i,"value=",kwarg[i])
func2(name='lhw',age=52,email="a@b.c")
key= name value= lhw
key= age value= 52
key= email value= a@b.c
クラスの定義
class Person:
    def __init__(self, name, age, nation):
        self.name = name
        self.age = age
        self.nation = nation
def greet(self):
        return("Hello, my name is %s!" % self.name)
    def get_name(self):
        return self.name
    def get_nation(self):
        return self.nation
ll=Person(name="lhw",age=51,nation="Chinese")
print(ll.greet())
print(ll.get_name())
lhw
クラス定義の継承
上記Personクラスを継承するように、中国人を定義する。
class Chinese(Person):
    def __init__(self, name, age):
        super().__init__(name, age, "Chinese")
    def greet(self):
        return("Ni Hao!!")
勉強しやすく、手軽にプログラミングできるのはメリットであるが、Pythonという言語は、俺のようなc/c++の人間から見ると、まったくメリットの少ない言語だと思っている。Computer Science/Engineering専攻以外の人間はパソコンを使って科学計算や事務処理など軽くプログラミングするには最適だと思う。
なぜならば、まずはコンパイルではなく、インタープリタ方式はPythonの最大の弱点だと思う。MLなどの場合、本格的な計算/トレーニングが必要な場合、何週間も実行した挙句、最後に「構文が間違っている」というエラーが出てきたら、最悪だよね(もちろん文法をチェックしてくれるツール、visual studio codeとかがあるけど)。
また、変数の型が改めて(明示的に)定義しないのも、とても不便だと思う。これも間違いやすく頭痛の種。
最後に、ブロックはインデントにより決まる(関数やクラスを定義するとき)ことは全くの理解不能。確かにソースコードはちゃんとインデントを使って分かりやすく書いたほうがよくて、この良い習慣を守ったら、ついでにブロックも定義できたのが便利は便利だけど、「{、}」や「begin、end]など使うとそんなに面倒か!?と思われるくらいだね。
ただし、Tensorflowをはじめ、いろんなフレームワークはPythonを使っていて、しかも、たくさんのモデルが開発され、機能が豊富というメリットあり、勉強する価値があると思う(c/c++は開発すれば同じく機能の拡充できるけどね)。
Pythonとよく似ているコンパイル方式の言語を開発できればいいけどなぁ。
※参考:https://docs.python.org/3/reference/compound_stmts.html

0 Comments:
Post a Comment
<< Home