Skip to content

Instantly share code, notes, and snippets.

@masazdream
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save masazdream/090cbd22ca1e87d032fd to your computer and use it in GitHub Desktop.

Select an option

Save masazdream/090cbd22ca1e87d032fd to your computer and use it in GitHub Desktop.
初めてのPython 書式1
# coding: UTF-8
# HelloWorld
print "Hello World"
# 変数データについて
# 大文字、小文字は区別される
#ex)
msg = "Hello World2"
print msg
# 数値 整数、小数、複素数
# 真偽値 True False
# None
# 関数・オブジェクト
# 要素が並んだもの
# 文字列:文字
# リスト:データ(配列)
# タプル:データ変更不可
# セット:データ重複不可
# 辞書:key と value のペア
# 演算子 + - * / // 商 % 余り ** べき乗
print 10 * 5
print 10 // 3
print 10 % 3
print 2 ** 3
print 5 + 1.3
print 5 + 2.0
print 5 / 3
print 5 / 3.0
# 文字列
# 日本語 u"こんにちは"
print "こんにちは"
print u"こんにちは"
print "hello" + "world3"
print u"無駄!" * 10
print "hello\nwor\tld3"
print """
<html>
<body>
</body>
</html>
"""
# 文字数 len 、検索 find 、切り出し []
s = "abcdefghijklmn"
print len(s)
print s.find("c")
print s.find("x")
print s[2]
print s[2:5]
print s[:5]
print s[2:]
print s[2:-1]
print len("こんにちは")
print len(u"こんにちは")
# 型の変換
print 5 + int("5")
print 5 + float("5")
age = 20
print "I am " + str(age) + " year old"
# リスト
sales = [255, 100, 353, 400, "aba"]
print len(sales)
print sales[2]
print 100 in sales
sales[1] = 10000
print sales[1]
print 100 in sales
print range(1, 10)
print range(3, 10)
print range(3 ,20 ,2)
sales2 = [50, 100, 80, 45]
print sales2
sales2.sort()
print sales2
sales2.reverse()
print sales2
day = "2013/12/15"
print day.split("/")
a = ["a", "b", "c"]
print "-".join(a)
# タプル
t = (2, 5, 8)
print len(t)
print t * 3
b = list(t)
print b
t2 = tuple(b)
print t2
# セット (集合型)
s1 = set([1, 2, 3, 4, 3, 2])
print s1
s2 = set([3, 4, 5])
print s2
print s1 - s2
print s2 - s1
print s1 | s2
print s1 & s2
print s1 ^ s2
# 辞書
sales = {"taguchi":200, "imai":400, "dotinst":500}
print sales
print sales["imai"]
sales["imai"] = 1000
print sales
print "taguchi" in sales
print "test" in sales
print sales.keys()
print sales.values()
print sales.items()
v1 = 10
v2 = 1.234234
v3 = "imai"
v4 = {"imai":400, "brian":500}
print "age: %d" % v1
print "age: %10d" % v1
print "age: %010d" % v1
print "price: %f" % v2
print "price: %.2f" % v2
print "name: %s" % v3
print "sales: %(imai)d" % v4
print "%d and %f" % (v1, v2)
# 条件分岐
score = 70
if score > 60:
print "OK!"
print "OK2!"
if score > 60 and score < 80:
print "OK3!"
score2 = 40
if 60 < score2 < 80:
print "OK4!"
elif score > 30:
print "OK?"
else:
print "NG!"
print "OK5!" if score > 60 else "NG2!"
print "OK5!" if score2 > 60 else "NG2!"
# ループ
f = [13, 3435, 31, 876]
sum = 0
for sale in f:
sum = sum + sale
print sale
else:
print sum # ループが終わったら一度処理する
for i in range(10):
if i == 3:
continue
if i == 5:
break
print i
# 辞書型ループ
dic = {"imai":1000, "brian": 300, "claud": 900, "mark": 130, "dave": 487}
for key, value in dic.iteritems():
print "key: %s, value: %d " % (key, value)
for key in dic.iterkeys():
print key
#whileループ
n = 100
while n < 110:
if n == 103:
n += 1
continue
if n == 106:
break
print n
n += 1
else :
print "end"
print n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment