python3とpython2では記法が異なっている点があるので注意が必要です。
print ("hello world!")
変数
msg = "hello world"
print (msg)
整数と小数の演算は小数、整数同士の割り算は、小数点以下切り捨てとなります。
繰り返し処理
print (u"無駄"*10)
\\, \*なども
改行表現
print ("""<html>
<body>
</body>
</html>""")
整数値と文字列はpythonでは明示する
print (5 + int("5"))
print ("i am " + str(20) + "years old.")
配列、存在チェック
sales = [200, 100, 342, 1230, 122]
print (100 in sales)
ソート、reverse
sales = [52, 100, 80, 45]
sales.sort()
print (sales)
タプル:変更不可
a = (2, 5, 8)
print (a * 3)
セット:重複を許さない
a = set([1, 2, 3, 4])
print (a)
差集合
a = set([1, 2, 3, 4])
b = set([4, 5, 6, 7])
print (b - a)
辞書
sales = {"yamada": 200, "yoshida": 300, "sakura": 240}
print (sales)
key、value、items(一覧)
sales = {"yamada": 200, "yoshida": 300, "sakura": 240}
print (sales.values())
文字列へのデータ組み込み
a = 10
b = 123.345
c = "sakaki"
d = {"yamada":200, "yoshimoto": 300}
print ("age: %d" % a)
条件分岐
score = 70
if score > 60:
print ("ok")
条件分岐2
score = 55
if score > 60:
print ("ok")
elif score > 50:
print ("soso")
else:
print ("NG!")
forループ
sales = [13, 235, 312, 2232]
for sale in sales:
print (sale)
繰り返し処理 ※インデントに注意
for i in range(10):
print(i)
空処理
def hello2():
pass
python module:
https://docs.python.org/3/library/index.html