プログラミング 1 ( 第 5 回 ) ループ処理 (for 文 ) range() 関数とリストによるシーケンス集合表現 1. Chapter 3.2 For Loops 1. もう一つのループ処理 2. シーケンス集合とコード例 2. Chapter 3.4 A Few Words About Using Floats 1. 浮動小数点数の取り扱い 3. 演習 1. 演習 1 4: 初めてのレポート 2. 演習 5: if 文, 関数の利用 3. 演習 6: while 文 4. 宿題 講義ページ : http://ie.u-ryukyu.ac.jp/~tnal/2016/prog1/ 2016 年度 : プログラミング 1 1
Chapter 3.2, 3.4 の補足 3.2 For Loops 3.4 A Few Words About Using Floats 2016 年度 : プログラミング 1 2
3.2 For Loops ( 反復 ) *2 つ目の反復制御 range () 関数 range(stop): 0 stop-1 までの int 型オブジェクトを生成 range(start, stop): start stop-1 x = 4 for i in range(0, x): print(i) for 文 in として指定されたシーケンス集合に対して (1)1 つずつ要素を取り出し (2) その要素を対象としてブロックを実行 ( 反復処理 ) (3) 全要素に対して (2) を実行し終えたら for 文を終了 シーケンス集合の例 : str, range, list 2016 年度 : プログラミング 1 3
シーケンス集合の例 1(str 型オブジェクト ) # コード 1 for c in 'abc': print(c) # 結果 1 a b c # srt 型オブジェクト ( 復習 ) >>> len('abc') 3 >>> 'abc'[0] 'a' >>> 'abc'[1] 'b' >>> 'abc'[2] 'c' >>> enemy = 'naltoma' >>> enemy[0] 'n' 2016 年度 : プログラミング 1 4
シーケンス集合の例 2(range 型オブジェクト ) # コード 2 index = 0 word = 'abc' for index in range(len(word)): print('word[{0}] = {1}'.format(index, word[index])) # 実行結果 2 word[0] = a word[1] = b word[2] = c {}: brace str.format() メソッド - 対象文字列は {} で区切られた置換フィールドを含む - 置換フィールドを何で置き換えるかを引数で指定 - e.g., '{0} and {1}'.format('spam', 'eggs') 'pi = {0:.3f}'.format(3.14159) 参考 : str.format() メソッドチュートリアル : http://docs.python.jp/3/tutorial/inputoutput.html 2016 年度 : プログラミング 1 5
シーケンス集合の例 3(list 型オブジェクト ) # コード 3 items = ['I did it all', 4, 'love'] index = 0 for i in items: print('items[{0}] = {1}'.format(index, i)) index += 1 # 結果 3 items[0] = I did it all items[1] = 4 items[2] = love list ( リスト ) 型オブジェクト - 順序付けられたオブジェクト集合 - リスト名 = 集合名 - リスト名 [ インデックス ] = 指定した順番のオブジェクト - e.g., items[0] = 'I did it all' 2016 年度 : プログラミング 1 6
3.4 A Few Words About Using Floats ( 浮動小数点数を使う際の補足 ) # コード例 4 ( 教科書版を少し編集 ) x = 0.0 for i in range(10): x = x + 0.1 if x == 1.0: print('{0} = 1.0'.format(x)) else: print('{0}!= 1.0'.format(x)) # 実行結果 4 0.9999999999999999!= 1.0 コンピュータにおける数 :2 進数 丸め誤差 (rounded error): 整数は適切に表現できるが 小数点のある数は必ずしも表現できない 参考 : 5 分で覚えるIT 基礎の基礎 ゼロから学ぶ2 進数第 4 回 : http://goo.gl/xvrn6n 倍精度浮動小数点数 : https://ja.wikipedia.org/wiki/ 倍精度浮動小数点数 2016 年度 : プログラミング 1 7
大きすぎる数字と小さすぎる (0 より小 さい ) 数を同時に扱うのは難しい # 1000 桁の整数に 0.1 を足してみる >>> bignumber = 10**1000 >>> bignumber += 0.1 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: int too large to convert to float - int なら 1000 桁でも適切に表現できる - float 演算しようとすると Overflow( 桁あふれ ) # 100 桁の整数に 0.1 を足してみる >>> bignumber = 10**100 >>> bignumber += 0.1 >>> >>> print(bignumber) 1e+100 指数表記 (exponential notation) 1e+100 = 1 * 10**100 0.0001 = 1 * 10**(-4) = 1e-04 これは誤差で片付けられる値? >>> print('{0:f}'.format(bignumber)) 10000000000000000159028911097599180468360808563945281389781327 557747838772170381060813469985856815104.000000 2016 年度 : プログラミング 1 8
Reserved words, 予約語 https://goo.gl/rezdan 一覧 ( 赤丸は今回出てきた予約語 ) 2016 年度 : プログラミング 1 9
演習 演習 1 4: 初めてのレポート 演習 5: if 文, 関数の利用 演習 6: while 文の利用 2016 年度 : プログラミング 1 10
補足 1 ペアプログラミングを始める前に 記入漏れ 実施日 と 報告者 前回の復習確認 何をやったっけ? これはこうやれば良いんだっけ? 2016 年度 : プログラミング 1 11
補足 2 ペアプログラミングのやり方 7 ステップ 作業を決める 最初の目標を決める パートナーを頼りにし 支えてやる driver: 仕事を終わらせることに専念 observer: 横から観察し 疑問 改善 簡潔化など大局的な問題について考える 喋る 一人で悩む のは十秒程度に留める 一緒に相談しながら考える練習 お互い何をやっているか把握する 頻繁に同期をとる 喜ぶ 交代する 分業ではない (observer= 観察しながら気づいたことをコメント ) 二人で 2,3 分考えても分からない場合には 手を上げて質問しよう 2016 年度 : プログラミング 1 12
演習 演習 1 演習 4: 初めてのレポート https://ie.u-ryukyu.ac.jp/~tnal/2016/prog1/week2-ex.html レポートの作成手順は授業ページを参照 演習 5: if 文と関数 https://ie.u-ryukyu.ac.jp/~tnal/2016/prog1/week3-ex.html 演習 6: while 文 https://ie.u-ryukyu.ac.jp/~tnal/2016/prog1/week4-ex.html ペア プログラミング https://ie.u-ryukyu.ac.jp/~tnal/2016/prog1/week2-pairprogramming.html driver, observer (navigator) 2016 年度 : プログラミング 1 13
宿題 復習 : 適宜 ( これまでの内容 ) 予習 : 教科書読み 4 章 4 Functions, scoping, and abstraction ( 冒頭 1 ページ ) 4.1 Functions and Scoping (4.1.1 Function Definitions) # 終了済み 4.1.2 Keyword Arguments and Default Values 4.1.3 Scoping 4.2 Specifications (4.3 Recursion) スキップ (4.4 Global Variables) スキップ 4.5 Modules 復習 予習 ( オススメ ): paiza Python 入門編 1: プログラミングを学ぶ ( 全 9 回 ) https://paiza.jp/works/python3/primer プログラミングスキルチェック * レベル設定のある課題集 https://paiza.jp/challenges/info 2016 年度 : プログラミング 1 14
参考文献 教科書 : Introduction to Computation and Programming Using Python, Revised And Expanded Edition Python 3.5.1 documentation, https://docs.python.org/3.5/index.html str.format メソッド, http://docs.python.jp/3/tutorial/inputoutput.html 5 分で覚える IT 基礎の基礎 ゼロから学ぶ 2 進数第 4 回 2 進数で小数を表す方法, http://itpro.nikkeibp.co.jp/members/itpro/itbasic/200206 24/1/?rt=nocnt 倍精度浮動小数点数, https://ja.wikipedia.org/wiki/ 倍精度浮動小数点数 2016 年度 : プログラミング 1 15