2014年8月4日月曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅱ.(Types and Operations)、Chapter 7.(String Fundamentals)、Test Your Knowledge: Quiz 2.を解いてみる。

その他参考書籍

Test Your Knowledge: Quiz 2.

Yes.

コード(BBEdit)

sample2.py

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

s = 'spam egg python'
l = ['spam', 'egg', 'python', 1, 2, 3, 4, 5]

for x in [s[:], s[:2], s[2:], s[2:4], s[1:5:2],
          s[1:5:-1], s[5:1:-1], s[5:1:-2]]:
    print(x)

for x in [l[:], l[:2], l[2:], l[2:4], l[1:5:2],
          l[1:5:-1], l[5:1:-1], l[5:1:-2]]:
    print(x)

入出力結果(Terminal, IPython)

$ ./sample2.py
spam egg python
sp
am egg python
am
pm

e ma
em
['spam', 'egg', 'python', 1, 2, 3, 4, 5]
['spam', 'egg']
['python', 1, 2, 3, 4, 5]
['python', 1]
['egg', 1]
[]
[3, 2, 1, 'python']
[3, 1]
$

0 コメント:

コメントを投稿