0
1

More than 1 year has passed since last update.

Python 忘備録

Last updated at Posted at 2020-01-03

趣旨

python 関係でググる前にこれ読んどけ的な団員向けメモ。

  • セミコロンはいらない
  • 変数は宣言不要
  • 字下げはスペースでやる。TABは許されない。
  • None を使う。NULL, null, nil, undefined などは無い
  • True, False は先頭大文字。true, false ではない
  • if, for, while は末尾にコロンが必要。括弧は不要。
  • beginend はない
  • if, for, with, while, try, def などの本文はインデントも必要。
  • else は字下げしてコロンも必要
  • 論理和、論理積、否定は or, and, not
  • switch/case はない。elif を使う。
  • a++ は不可。a+=1
  • next ではなく continue
  • 剰余は % (mod ではない)
  • ライブラリ読み込みは import, as で短縮できる
  • 引数受け取りは sys.argv。仕様は C と同じで0番目にコマンド名が入る。でも argparse 推奨。
  • print は改行が入る。end="" を入れと改行されない。
  • キャストは明示的に必要 int(a) とか
  • double はない。float はある。
  • printf 的な構文は % を使う。"%02d"%val みたいな。
  • f = open("file","r") または with open("file","r") as f:
  • write はアスキー書き込み、改行は入らない。バイナリは別途
  • コメントアウトは #。複数行は ``` (バッククオーテーションx3)
  • range(0,x) は 0 から x-1 まで
  • 強制終了は sys.exit(error_code)
  • 配列の初期化は a = [] とか a = [1,2,3] とか
  • 配列の要素追加は arr.append(item)
  • 連想配列の初期化は a = {} とか a = {"hoge":2,"huga": "xx"} とか。
  • 連想配列の参照は a['key'] = value
  • ビット演算は C と同じ( &: 論理積, |: 論理和, >>: 右シフト, <<:左シフト、 ^: 排他的論理和、 ~: ビット反転 )

正規表現はこのあたり:https://qiita.com/luohao0404/items/7135b2b96f9b0b196bf3

json はこのあたり:https://qiita.com/unchainendo/items/7865bdcdaadd62f2e435

とりあえず下のコードを一度みておくべき。

syntax.py
# import cv2
# import tensorflow as tf
import sys

def main():
    x = 0
    y = 2

    args = sys.argv
    if len(args) < 2:
        print( "%s: too few args"%args[0] )
        sys.exit(1)
    else:
        try:
            x = int(args[1])
        except:
            print( "%s is not a number."%args[1] )
            sys.exit(2)

    z = x * y
    print( "x * y = %d, "%z, end="" )

    arr = []
    for i in range(0,x):
        arr.append(i)

    sum = 0
    for a in arr :
        sum += a
        
    wstr = "sum:%d"%sum
    rstr = ""
    print( wstr )
    
    with open("test.txt", "w") as fw:
        fw.write( wstr )
    
    with open("test.txt", "r") as fr:
        rstr = fr.read()
        print( "read data =", rstr )

    h = {}
    h['hoge'] = x
    print( "hash['hoge'] = %d"%h['hoge'] )

if __name__ == "__main__":
    main()

型関連

a = 10
print(float(a)
# 10.0
b = 10.01
print(int(b))
# 10
c = "1.01"
d = "-1.01"
print(int(c))
# エラーになる
print(float(c))
print(float(d))
# 1.01
# -1.01
e = "0x10"
print( int(e,16) )
# 16
f = 10.01
print( str(f) )
# '10.01'

数値計算

  • int: 整数肩に変換。絶対値が小さくなる整数へ切り捨て・切り上げ
  • math.floor:小数を切り捨て
  • math.ceil: 小数を切り上げ
  • round:小数を四捨五入
  • random.random(): 0から1の間の小数の乱数を発生させる。(要 import random
.py
import math
a = 1.11111
b = -2.22222
str = "-3.14159"
print( int(a) )
# 1
print( int(b) )
# -2
print( math.floor(a) )
# 1
print( math.floor(b) )
# -3
print( math.ceil(a) )
# 2
print( math.ceil(b) )
# -2
print( round(a) ) 
# 1
print( round(b) )
# -2
import random
print( random.random() )
# 0.48396949711696924 など

正規表現

ファイル読み書き

子プロセス実行

配列の初期化、操作

連想配列

JSON 関連

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1