2014年2月27日木曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Statements and Syntax)、CHAPTER 11(Assignments, Expressions, and Prints)、Test Your Knowledge: Quiz 1, 2, 3, 4を解いてみる。

その他参考書籍

Quiz 1, 2, 3, 4

コード(BBEdit)

sample.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# 1
a = 0
b = 0
c = 0
print(a, b, c)

d = e = f = 'python'
print(d, e, f)

g, h, i = 0, 0, 0
print(g, h, i)

# 2
# リストの場合に注意が必要
a = b = c = [1, 2]
a.append(10)
print(a, b, c)
a.reverse()
print(a, b, c)

a = [1, 2]
b = [1, 2]
c = [1, 2]
a.append(10)
print(a, b, c)

b.reverse()
print(a, b, c)

# 3
L = [5, 1, 4, 2, 3]
L = L.sort()
print(L) # None

L = [5, 1, 4, 2, 3]
L.sort()
print(L)

# 4
with open('temp.txt', 'w') as f:
    print('Hello world!', file=f)

入出力結果(Terminal)

$ ./sample.py 
0 0 0
python python python
0 0 0
[1, 2, 10] [1, 2, 10] [1, 2, 10]
[10, 2, 1] [10, 2, 1] [10, 2, 1]
[1, 2, 10] [1, 2] [1, 2]
[1, 2, 10] [2, 1] [1, 2]
None
[1, 2, 3, 4, 5]
$ cat temp.txt
Hello world!
$

0 コメント:

コメントを投稿