Created
May 15, 2016 23:52
-
-
Save chuck0523/5e8594823963f48aaca6ad7b27cb129c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
# ↑日本語対応 | |
print "Hello world!" | |
# print: 出力 | |
msg = "Hello world" | |
print msg | |
# 変数名はケースセンシティブ | |
# データ型 | |
# 数値、真偽値(True, False)、None、関数・オブジェクト | |
# コレクションは以下のとおり。 | |
# 文字列、リスト、タプル(変更不可)、セット(重複不可)、辞書(キーと値のペア) | |
# 数値について。 | |
# 整数、少数、複素数 | |
# 演算子 + - * / // % ** | |
print 10 * 5 # 50 | |
print 10 // 3 # 3 | |
print 10 % 3 # 1 | |
print 2 ** 3 # 8 | |
# 整数と少数の演算 → 少数 | |
print 5 + 2.0 # 7.0 (7ではない) | |
# 整数同士の割り算 → 切り捨ての整数 | |
print 10 / 3 # 3 (3.333...ではない) | |
print 10 / 3.0 # 3.333... | |
# 文字列 | |
# ダブルクオーテーションで囲む。 | |
# 日本語にはuをつける。 u"こんにちは!" | |
# + * | |
print "hello " + "world" | |
print u"無駄!" * 10 | |
# エスケープ \n \t \\ | |
print "hello \n world! it\' a pen" | |
print""" | |
<html lang="utf-8"> | |
<body> | |
</body> | |
</html> | |
""" | |
# 改行付きで表示 | |
# len: 文字数 | |
# find: 検索 | |
# 切り出し: [] | |
s = "abcdefghi" | |
print len(s) # 9 | |
print s.find("c") # 2 | |
# findは0始まり | |
print s.find("x") # -1 | |
# not found | |
print s[2] # c | |
print s[2:5] # cde | |
print s[:5] # abcde | |
print s[2:] # cdefghi | |
print s[2:-1] # cdefgh | |
# 終端指定 | |
# オートボクシングはない。 | |
# 文字列 → 数値: int, float | |
# 数値 → 文字列: str | |
print 5 + int("5") # 10 | |
print 5 + float("5") # 10.0 | |
age = 20 | |
print "i am " + str(age) + " years old!" | |
# 今扱っているデータの型を意識する。 | |
# リスト | |
sales = [255, 100, 353, 400, "aba"] | |
# len: 要素数 | |
# + * [] | |
print len(sales) # 5 | |
print sales[2] # 353 | |
sales[2] = 100 | |
print sales[2] # 100 | |
# in | |
print 100 in sales # True | |
# range | |
print range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
print range(3, 10) # [3, 4, 5, 6, 7, 8, 9] | |
print range(3, 10, 2) # [3, 5, 7, 9] | |
# リストメソッド | |
sales = [50, 100, 80, 45] | |
# sort reverse | |
sales.sort() | |
print sales # [45, 50, 80, 100] | |
sales.reverse() | |
print sales # [100, 80, 50, 45] | |
d = "2016/05/16" | |
print d.split("/") # ['2016', '05', '16'] | |
a = ["a", "b", "c"] | |
print "-".join(a) # a-b-c | |
# タプル(変更ができない) | |
# 間違いを防いたり、計算速度が早くなる。 | |
a = (2, 5, 8) | |
# len + * [] | |
print len(a) # 3 | |
print a * 3 # (2, 5, 8, 2, 5, 8, 2, 5, 8) | |
# a[2] = 10 TypeError: 'tuple' object does not support item assignment | |
# タプル → リスト | |
b = list(a) | |
print b # [2, 5, 8] | |
# リスト → タプル | |
c = tuple(a) | |
print c # (2, 5, 8) | |
# セット(集合型) - 重複不可 | |
a = set([1, 2, 3, 4]) | |
print a # set([1, 2, 3, 4]) | |
a_a = set([1, 2, 3, 4, 1]) | |
print a_a # set([1, 2, 3, 4]) 最後の1はスルー | |
b = set([3, 4, 5]) | |
# 差集合 | |
print a - b # set([1, 2]) | |
# 和集合 | |
print a | b # set([1, 2, 3, 4, 5]) | |
# 積集合 | |
print a & b # set([3, 4]) | |
# どちらかにしかないもの | |
print a ^ b # set([1, 2, 5]) | |
# 辞書型 key, value | |
sales = {"chuck": 200, "other": 300, "boss": 700} | |
print sales # {'other': 300, 'chuck': 200, 'boss': 700} | |
print sales["chuck"] # 200 | |
sales["boss"] = 800 | |
print sales # {'other': 300, 'chuck': 200, 'boss': 800} | |
# in | |
print "chuck" in sales # True | |
# keys, values, items | |
print sales.keys() # ['other', 'chuck', 'boss'] | |
print sales.values() # [300, 200, 800] | |
print sales.items() # [('other', 300), ('chuck', 200), ('boss', 800)] | |
a = 10 | |
b = 1.234234 | |
c = "chuck" | |
d = {"me": 100, "you": 200} | |
print "age: %d" % a # age: 10 | |
print "age: %10d" % a # age: 10 | |
print "age: %010d" % a # age: 0000000010 | |
print "price: %f" % b # price: 1.234234 | |
print "price: %.2f" % b # price: 1.23 | |
print "name: %s" % c # name: chuck | |
print "sales: %(you)d" %d # sales: 200 | |
print "%d and %f" % (a, b) # 10 and 1.234234 | |
# 条件分岐 if | |
score = 70 | |
if score > 60: | |
print "ok" | |
print "OK" | |
# 論理演算子はand or not | |
# if score > 60 and score < 80: | |
# if 60 < score < 80: ← Good! | |
if score > 80: | |
print "OK" | |
elif score > 40: | |
print "soso" | |
else: | |
print "ng" | |
print "ok" if score > 60 else "ng" | |
# forループ | |
sales = [13, 3523, 31, 238] | |
sum = 0 | |
for sale in sales: | |
sum = sum + sale | |
print sum # 3805 | |
for sale in sales: | |
sum += sale | |
else: | |
print sum | |
for i in range(10): | |
print i # 0から9までを表示 | |
# continue, breakもある。 | |
# 辞書型でループ | |
users = {"chuck": 200, "you": 300, "boss": 500 } | |
for key, value in users.iteritems(): | |
print "key: %s value: %d" %(key, value) | |
# iterkeys(), itervalues()もある。 | |
# whileループ | |
n = 0 | |
while n < 10: | |
print n | |
n += 1 # 0から9まで表示 | |
else: # ループが終わったら実行される。 | |
print "end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment