http://docs.python.org/tutorial/introduction.html
とかに主に従ってます.
家のデスクトップPC使うとこのように非常に横幅の広い画面でオソロシク快適な作業ができる.
御飯食べてまた書き足すねーーー(22 時前)
ファイル読み込みとか. string とか.
で>>> this = file("2nd.txt","w");>>> this = file("2nd.txt","a");
>>> this.write("test2\n");
>>> this.write("and appending?");
>>> this.close();
test2と書かれた 2nd.txt が(現在のディレクトリに)生成される.w とかは case-sensitive.
and appending?
r read, w write, a append, U があるようだ.python から読むには
>>> this = file("2nd.txt","r");.tex とかもテキストとして読んでくれた.' " は記号として普通に入れてもよいが,
>>> this.read() 'test2\nand appending?'
例えば上の引数の中に"が含まれたりする場合は
>>> 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\最初に r つけて raw string.
... here.";
>>> print test;
and linebreaking here.
>>> test = r"raw string as \n\また """ か ''' で囲えば,そこをそのまま string として出してくれる(これもファイル出力に限らず).
... this.";
>>> print test;
raw string as \n\
this.
>>> 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)6# 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