開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)の13章(while ループと for ループ)練習問題1.を解いてみる。
その他参考書籍
2.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- l = ['a','b','c','d','e'] # while loopとlen関数、for loop とrange関数とlen関数を使ってもできるけど # for loop で直接シーケンスを使った方がいい i = 0 while i < len(l): print(l[i]) i += 1 for i in range(len(l)): print(l[i]) # 良い方法 for x in l: print(x)
入出力結果(Terminal)
$ ./sample.py a b c d e a b c d e a b c d e $
ちなみにJavaScriptの場合。
コード(BBEdit)
// JavaScriptではpythonと違い、while loopかfor loopを使う方法のみ // for loopを使った方がいいのかな。。 var a = ['a','b','c','d','e'], result = "", i = 0; while (i < a.length) { result += a[i] + "\n"; i += 1; } for (i = 0, max = a.length; i < max; i += 1) { result += a[i] + "\n"; } $('#pre0').text(result);
0 コメント:
コメントを投稿