Help us understand the problem. What is going on with this article?

Pythonを書くための最低限の文法メモ

More than 1 year has passed since last update.

初心者用メモ!

予約語

予約語以外にもlist,abs,strなど組み込み関数に使われているので避ける!
詳細は以下を見るべし。
http://qiita.com/cm3/items/6a856c44dd92632aa54f

定数

  • True - 真
  • False - 偽
  • None - 値がない時に使われる。

bool演算

bool
a and b         # a も b も真であれば真
a or b          # a または b が真であれば真
not a           # a が偽であれば真

Noneの処理判別

none
if hoge is None:
  #Noneのときの処理

if hoge is not None:
  #Noneじゃないとき

配列(list)

array
values = [1, 2, 3, 4]
print values[0] #1
print "length=" + str( len(values) ) #length=4

push
list = ["A", "B", "C"]

list.append("D")
print list     # ["A", "B", "C", "D"]

そのほか関連の関数
http://www.pythonweb.jp/tutorial/list/

オブジェクト(dict)

dict
d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
dict
for k, v in d.items():
    print k, v             # Tanaka 80, Yamada 30, Suzuki 40

for k in d.keys():
    print k, d[k]          # Suzuki 40, Yamada 30, Tanaka 80

for v in d.values():
    print v                # 80, 30, 40

for k, v in d.iteritems():
    print k, v             # Tanaka 80, Yamada 30, Suzuki 40

for文

for
#0〜9までループ
for i in range(0, 10):
    print "hoge " + str(i)
for
#配列の中身を走査
values = [ "hoge", "fuge", 123 ]
for value in values:
    print value

if文

if
if hoge < 1: 
    print "fuga1"
elif hoge < 2:
    print "fuga2"
else:
    print "fuga3"

cast

cast
print "hoge" + str( 100 ) #hoge100
print 10+int("5") #15

数字を丸める

floor
import math
math.floor(x)    
math.ceil(x)

関数

function
def hoge(a,b):
    print "hoge";
    return "hoge" + a + b

#呼び出し
hoge("AAA","BBB")#hogeAAABBB

__name__

よく見かける特別な変数__name__。メインプログラムのときは__name__=="__main__"となる。importするとファイル名が入る。それらを区別するために以下のような表記をよくつかうようだ。

`__name__`
if __name__ = "__main__":
    print "メインプログラムだよ"
#else:
#   print "importされてるよ"

jsonを使う

使い方
http://www.python-izm.com/contents/application/json.shtml
読み込んだり保存したり
http://d.hatena.ne.jp/fenrifja/20130306/1362571700

クラス書く

http://www.tohoho-web.com/python/class.html#class

importについて

http://python.matrix.jp/pages/tips/import.html

参考

http://www.tohoho-web.com/python/index.html

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away