Friday 9 March 2012

python;

今日 python でいじったのを書き散らかす(ほんとに書き散らかすだけ).
http://docs.python.org/tutorial/introduction.html
とかに主に従ってます.
家のデスクトップPC使うとこのように非常に横幅の広い画面でオソロシク快適な作業ができる.
御飯食べてまた書き足すねーーー(22 時前)



 ファイル読み込みとか. string とか.
>>> this = file("2nd.txt","w");
>>> this.write("test2\n");
>>> this = file("2nd.txt","a");
>>> this.write("and appending?");
>>> this.close();
 で
test2
and appending?
と書かれた 2nd.txt が(現在のディレクトリに)生成される.w とかは case-sensitive.
r read, w write, a append, U があるようだ.python から読むには
>>> this = file("2nd.txt","r");
>>> this.read() 'test2\nand appending?'
 .tex とかもテキストとして読んでくれた.' " は記号として普通に入れてもよいが,
例えば上の引数の中に"が含まれたりする場合は
>>> this = file("2nd.txt","a");
>>> this.write("\"What about this?\" Will this work?");
>>> this = file("2nd.txt","r");
>>> this.read()
'test2\nand appending?"What about this?" Will this work?'
こんなのもある.\ を使って.
>>> test = "and linebreaking\
...  here.";
>>> print test;
and linebreaking here. 
最初に r つけて raw string. 
>>> test = r"raw string as \n\
... this.";
>>> print test;
raw string as \n\
this. 
また """ か ''' で囲えば,そこをそのまま string として出してくれる(これもファイル出力に限らず).
>>> this=file("3rd.txt","w");
>>> this.write("""
...     This may look
...             a little strange.
... """);
>>> this=file("3rd.txt","r");
>>> this.read()
'\n\tThis may look \n\t\ta little strange.\n'
 まだまだ
>>> test1 = "Fine, what";
>>> test2 = "comes" "next?" #this only works with two literals.
>>> test = test1 + test2;
>>> print test
Fine, whatcomesnext?
>>> # line 2 should have been : test2 = " comes" " next?";
...
>>> test3 = "lol";
>>> test = test3 * 5;
>>> print test;
lollollollollol
>>> test = test3*2.3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>> # loooool
...
>>> test = "the quick brown fox";
>>> test [3]
' '
>>> test[1]
'h'
>>> test[:2]
'th'
>>> test[2:]
'e quick brown fox'
>>> # here, Unlike a C string, test[:1] = "here" results in an error.
>>> test[7:10]
'ck '
>>> test[3:2]
''
>>> test[-1] #the last character
'x'
>>> test[-3] #you know
'f'
>>> test[:-3] #as expected
'the quick brown '
>>> test[-3:] + "jumps"
'foxjumps'
>>> test = "the number";
>>> len(test)
10
まあ,添字で内容変えたりはできないとはいっても,このへん組み合わせれば同じ事はできる.
もっと賢いやり方もあるのかもしれないけどねー.
これ思ったけど blogger の compose で貼付け→まとめてインデントとかやると
かなりぐちゃぐちゃな HTML になるのね.
>>> a = [1,2,3,5];
>>> a += [3]
>>> a
[1, 2, 3, 5, 3]
>>> a += [[3]]
>>> a
[1, 2, 3, 5, 3, [3]]
>>> a[3]="five";
>>> a
[1, 2, 3, 'five', 3, [3]]
#now len(a). length.
>>> len(a)
# now nesting.
>>> b=[12,21];
>>> a=[1,b,4];
>>> a
[1, [12, 21], 4]
>>> a[0].append(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'append'
>>> a[1].append(2);
>>> a
[1, [12, 21, 2], 4]
>>> a.append("end");
>>> a
[1, [12, 21, 2], 4, 'end']
うむ.
プログラムの初歩といえば素数と完全数とフィボナッチ数.
>>> x1,x2 = 0, 1; #こういう書き方ができるの面白い
>>> while x2 < 50:
...     print x2;
...     x1, x2 = x2, x1 + x2; #これも
...
1
1
2
3
5
8
13
21
34

No comments:

Post a Comment