Python

「Python」の編集履歴(バックアップ)一覧に戻る

Python - (2011/03/15 (火) 17:51:32) のソース

* python の勉強メモ

** 全体
- ソースコード内に全角文字を入れるとき
-- コード内に全角文字を入れるときはコードの頭を以下みたくする。(UTF8の場合)
 #!/usr/bin/env python
 # -*- coding: utf8 -*-
 
 ちなみに入れないとこんな風になる
 sys:1: DeprecationWarning: Non-ASCII character '\xef' in file dic-api-report.py on line 40,
 but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

** 基本
- if文の作り方
 if a == b:
   # 処理
 elif c == d:
   # 処理
 else:
   # 処理
- class の作り方
 def class Class:
   # コンストラクタ
   def __init__(self, foo, bar):
     self.foo = foo
     self.bar = bar
 
   # toString的なもの
   def __repr__(self):
     print 'Class: foo-%s, bar-%s' %(self.foo,self.bar)

** 文字列
- 文字列の長さがほしいときは以下。
 len("abc")

- 文字列を分割するときは以下。
 "abc/def/ghi"[:-1].split('/')
 
 ちなみに全角文字で区切るときはuつける
 "abcあdefあghi"[:-1].split(u'あ')

** 配列
- 配列に値を入れるときは、こんな感じ。
 array = []
 array.append("elem")

** Dictionary
- Dictionaryに値を入れるときは、こんな感じ。
 map = {}
 map["key"] = "value"