開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅲ.(Statements and Syntax)、Test Your Knowledge: Part Ⅲ Exercises、4-a, b, c, d, e, f.(Program logic alternatives)を解いてみる。
その他参考書籍
- Pythonチュートリアル 第2版
- Python クックブック 第2版 (原書(最新版))
- Programming Python
- Python Pocket Reference (Pocket Reference (O'Reilly))
4-a, b, c, d, e, f.(Program logic alternatives)
コード(BBEdit)
sample4.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
# a.
l = [1, 2, 4, 8, 16, 32, 64]
x = 5
print('a.')
i = 0
while i < len(l):
if 2 ** x == l[i]:
print('at index', i)
break
i += 1
else:
print(x, 'not found')
print('b.')
for n in l:
if 2 ** x == n:
print('at index', l.index(n))
break
else:
print(x, 'not found')
print('c.')
if 2 ** x in l:
print('at index', l.index(2 ** x))
else:
print(x, 'not found')
print('d.')
l1 = []
for x in range(7):
l1.append(2 ** x)
print(l1)
print('e.')
# yes
n = 2 ** x
if n in l:
print('at index', l.index(n))
else:
print(x, 'not found')
print('f.')
m = map(lambda x: 2**x, range(7))
print(m)
print(list(m))
l2 = [2 ** x for x in range(7)]
print(l2)
入出力結果(Terminal, IPython)
$ ./sample4.py a. at index 5 b. at index 5 c. at index 5 d. [1, 2, 4, 8, 16, 32, 64] e. at index 6 f. <map object at 0x10276fc88> [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] $
0 コメント:
コメントを投稿