開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 13.(Advanced Iteration: Looping Like Crazy) の SHARPEN YOUR PENCIL(No. 9129) を取り組んでみる。
SHARPEN YOUR PENCIL(No. 9129)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- print('1.') data = list(range(1, 9)) print(data) evens = [num for num in data if num % 2 == 0] print(evens) print('2.') data = [1, 'one', 2, 'two', 3, 'three', 4, 'four'] print(data) words = [num for num in data if isinstance(num, str)] print(words) print('3.') data = list('So long and thanks for all the fish'.split()) print(data) title = [word.title() for word in data] print(title)
入出力結果(Terminal, IPython)
$ ./sample2.py 1. [1, 2, 3, 4, 5, 6, 7, 8] [2, 4, 6, 8] 2. [1, 'one', 2, 'two', 3, 'three', 4, 'four'] ['one', 'two', 'three', 'four'] 3. ['So', 'long', 'and', 'thanks', 'for', 'all', 'the', 'fish'] ['So', 'Long', 'And', 'Thanks', 'For', 'All', 'The', 'Fish'] $
0 コメント:
コメントを投稿