最初のうちの備忘録。
標準出力・演算・ループ
新鮮です。主に扱ってきた言語が C++、C#、Java だからでしょうか。
でも拒否感は無いですし、行が少なくなるのはいいですね。
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 | |
# 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 |
関数
Java8でラムダ式が追加されたのを機に使おう!と思っていてあまり使っていないラムダ式。
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 | |
# 関数 | |
def hello(name, num=5): | |
print "hello %s! " % name * num | |
def hello2(name, num=4): | |
return "hello %s!" % name * num | |
#pass | |
def hello3(): | |
pass | |
hello("brain", 2) | |
hello("cluad", 4) | |
hello("david") | |
print hello2("mark") | |
# 変数スコープ | |
name = "imai" | |
def hello(): | |
name = "mark" | |
print name | |
def double(x): | |
return x ** 2 | |
print map(double, [2, 5, 8]) | |
print map(lambda x:x * x, [2, 5, 8]) |
クラス・オブジェクト
特に無し。
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 | |
__author__ = 'masahiro' | |
class User(object): | |
def __init__(self, name,): | |
self.name = name | |
def greet(self): | |
print "my name is %s!" % self.name | |
nico = User("Nico") | |
brian = User("Brian") | |
print nico.name | |
nico.greet() | |
brian.greet() |
継承
上のクラスを継承したクラス。
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 | |
import DotInstall3 | |
__author__ = 'masahiro' | |
class SuperUser(DotInstall3.User): | |
def shout(self): | |
print "%s! is SUPER!" % self.name | |
tom = SuperUser("tom") | |
tom.shout() |
インポート方法
なるほど、こうやってモジュールの中の一部をロード出来るんですね。
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 | |
__author__ = 'masahiro' | |
import math, random, numpy | |
from datetime import date | |
print math.ceil(5.2) | |
for i in range(5): | |
print random.random() | |
print date.today() |
0 件のコメント:
コメントを投稿