目次
# 通常のコメントアウト(1行) # print "text_A" print "text_B" # この書き方も有効です # 複数行のコメントアウト ''' print "text_C" print "text_D" ''' # ネスト(入れ子)させた複数行コメントアウト ''' print "text_E" """ print 'text_F' """ '''
type(10) #整数型 <class 'int'> type(3.14) #浮動小数点型 <class 'float'> type("text") #文字列型 <class 'str'>
x = 10 # 初期化 print(x) # 変数xを出力 x = 100 # 代入 print(x) # 100 y = 3.14 z = x * y print(z) # 314.0 type(z) # <class 'float'>
array = [1, 5, 10, 15, 20] # 配列 print(array) # [1, 5, 10, 15, 20] len(array) # 配列の個数を取得 array[0] # 1 array[4] # 20 array[4] = 99 # 値を代入 print(array) # [1, 5, 10, 15, 99]
array = [1, 5, 10, 15, 20] # 配列 # インデックスの0番目から2番目まで取得(2番目は含まない) print(array[0:2]) # [1, 5] # インデックスの1番目から最後まで取得 print(array[1:]) # [5, 10, 15, 20] # 最初からインデックスの3番目まで取得(3番目は含まない) print(array[:3]) # [1, 5, 10] # 最初から最後の1個前まで取得 print(array[:-1]) # [1, 5, 10, 15] # 最初から最後の2個前まで取得 print(array[:-2]) # [1, 5, 10]
div = {'width' : 200} # ディクショナリを作成 div['width'] # 200 div['height'] = 150 # ディクショナリに要素を追加 print(div) # {'width': 200, 'height': 150}
beef = True chicken = False print(not chicken) # chickenではない=True print(beef and chicken) # beefかつchicken=False print(beef or chicken) # beefまたはchicken=True
value = "fish" if value == "beef": print("牛肉を食べる!") elif value == "chicken": print("鶏肉を食べる!") else: print("魚を食べる!")
array = [5, 10, 15] for num in array: print(num) # 5, 10, 15 for str in "Hello": print(str) # "H", "e", "l", "l", "o"
count = 0 while count < 10: count += 1 print (count)
def funcTest(): print("Hello world") funcTest() # "Hello world"
def funcTest(object): print("Hello world " + object) funcTest("Python!!") # "Hello world Python!!"以上、Pythonの基本的な処理の文法のまとめでした。 感想としては記述構成がかなりシンプルなので、これからプログラムを覚える人にもかなり取り組みやすい言語という印象です。