Advertisement

Introduction of Python

Software Engineer
Apr. 11, 2012
Advertisement

More Related Content

Slideshows for you(20)

Advertisement

Recently uploaded(20)

Introduction of Python

  1. Introduction to Python 2012/04/11 Tomoya Nakayama
  2. Agenda Pythonって何? とにかく使ってみよう
  3. Pythonとの出会い もともとPerl使い 技術書を何冊か読む サンプルコードが結構な確率でPython これはPython知らなきゃまずいな… 使ってみると意外とおもしろい! これを機にPerlからPythonにシフトしよう!! 3
  4. Pythonって何? 4
  5. Pythonの歴史 Guide van Rossum が開発 1991年 0.90公開 1994年 1.0リリース 2000年 2.0リリース 2008年 3.0リリース Wikipedia 「グイド・ヴァンロッサ 5 ム」
  6. Pythonの特徴 スクリプト言語(インタプリタ) 動的型付け インデントが命 オブジェクト指向 6
  7. Pythonのいいところ 幅広く使われている 可読性 インデントが強制されているから 簡単 ライブラリが豊富 7
  8. プログラミング言語ランキング Language Ratings 1 Java 17.110% 2 C 17.087% 3 C# 8.244% 4 C++ 8.047% 5 Objective-C 7.737% 6 PHP 5.555% 7 (Visual) Basic 4.369% 8 JavaScript 3.386% 9 Python 3.291% 10 Perl 2.703% (出典) TIOBE Programming Community Index for March 2012 8
  9. オープンソースでも http://www.ohloh.net/languages/compare 9
  10. こんなところにPythonが (出典) Wikipedia 「Pythonを使っている製品あるいはソフトウェア の一覧」 10
  11. こんなところにも Gmail Google Groups Google Maps (出典) Wikipedia 「Pythonを使っている製品あるいはソフトウェア の一覧」 11
  12. ライブラリが豊富 標準ライブラリだけでも100種類以上 ファイル操作(CSV, HTML, XMLなど) インターネット(HTTPなど) GUI 単体テスト その他いろいろ… 詳細はこちら http://www.python.jp/doc/release/library/index.html 標準以外にも数多くのパッケージ 12
  13. 使ってみよう 13
  14. まずはインストール Windowsインストーラをダウンロードする インストーラを実行する おしまい 14
  15. コマンドライン実行環境の起動 スタートメニューから「Python2.7」⇒「Python (command line)」 コマンドプロンプトのような対話式の実行環境が 起動する 15
  16. お決まりの… print “Hello, Python.” 16
  17. Pythonの特徴をもう一度 スクリプト言語(インタプリタ) 動的型付け インデントが命 オブジェクト指向 17
  18. 動的型付け 変数の型は動的に変化する PerlとかPHPと同じ foo = “string” foo = 7 18
  19. インデントが命 プログラムの階層はインデントで表現 次の2つの例では結果が異なる (1) if score >= 80: print “Good!n” print “Your score is ” + str(score) + “n” (2) if score >= 80: print “Good!n” print “Your score is ” + str(score) + “n” 19
  20. C言語で書くと… (1) if (score >= 80) { printf(“Good!n”); printf(“Your score is %dn”, score); } (2) if (score >= 80) { printf(“Good!n”); } printf(“Your score is %dn”, score); 20
  21. 制御構造:if if score >= 80: Print “Good!” elif score >= 60: Print “So, so.” else: Print “No Good.” 21
  22. 制御構造:while n = 0 while n < 10: print n n = n + 1 22
  23. 制御構造:for Javaでいう拡張forループ array = [1, 2, 3, 4, 5] for n in array: print n 23
  24. データ構造:リスト(1) いわゆる「配列」 array1 = [1, 2, 3, 4, 5] array2 = [1, 2, [3, 4], 5] array3 = [1, 2, “3”, “4”, 5] array4 = range(2, 6, 1) いろんな参照方法 array1[0] array1[1:3] array1[-2] 24
  25. データ構造:リスト(2) いろんな操作 len(array1) array1.append(6) array3.remove(“3”) array1.reverse() 25
  26. データ構造:タプル リストと同じように使えるが、後から操作できな い tpl = (1, 2, 3, 4, 5) print tpl[0] tpl[0] = 10# これはNG 26
  27. データ構造:辞書 キーと値のペア dic1 = {'name': 'John', 'age': 25} print dic1['name'] dic2 = { 'john': {'name': 'John', 'age': 25}, 'bob': {'name': 'Bob', 'age': 20} } # ネストもOK print dic2['bob']['age'] 27
  28. リスト内包表記 (1) array = [60, 92, 12, 54, 88] print len([n for n in array if n >= 60]) (2) ax = [“x1”, “x2”, “x3”] ay = [“y1”, “y2”, “y3”] az = [“z1”, “z2”, “z3”] print [(x, y, z) for x in ax for y in ay for z in az] 28
  29. 関数の定義 def my_double(x): return x * 2 num = my_double(5) 29
  30. クラスの定義 class Derived(Base): # Baseクラスを継承したDerivedクラ ス def __init__(self): # __init__はコンストラクタ(名前固定) def public_method(self, arg1, arg2): # publicメソッド def __private_method(self, arg1, arg2): # 先頭に__を付けるとprivateに。 obj = Derived() obj.public_method(arg1, arg2) obj.__private_method(arg1, arg2) # これはエラー 30
  31. ダックタイピング(duck typing) "If it walks like a duck and quacks like a duck, it must be a duck" 「もしもそれがアヒルのように歩き、アヒル のように鳴くのなら、それはアヒルである」 31
  32. ダックタイピング class Duck: def sound(self): return “quack” class Cat: def sound(self): return “myaa” def let_sound(obj): print obj.sound() let_sound(Duck()) let_sound(Cat()) 32
  33. サンプルプログラム 33
  34. twitterアプリの製作 特定のユーザーのツイートを取得 twitter APIを利用 レスポンスをJSONで取得 ツイートの時刻とテキストを出力 34
  35. プログラム作成の前に… twitter API https://dev.twitter.com/docs/api/1/get/statuses/user_timeline JSON 「キー: 値」の形式でデータを表現 35
  36. プログラム # -*- coding: utf-8 -*- import urllib import json scr_name = 'NHK_PR' # twitterのスクリーンネーム url = 'https://twitter.com/statuses/user_timeline.json' + '?screen_name=' + scr_name result = urllib.urlopen( url, proxies={'https': 'http://proxy:8080'} ) result = json.loads(result.read()) for tweet in result: print '[%s] %s' % (tweet['created_at'], tweet['text']) 36
  37. まとめ 37
  38. まとめ 簡単でしょ? 使ってみてくださいね。 38
  39. おまけ 39
  40. Pythonの亜種 IronPython .NET frameworkで動くPython .NET frameworkのライブラリが使える Visual Studioが使えるっぽい(フォームデザイナと か) Jython Java VMで動くPython ちょっとバージョン古めかな… 40
  41. IronPython import clr clr.AddReferenceByPartialName(“System.Windows.Forms”) from System.Windows.Forms import * MessageBox.Show( “Are you OK?”, “Test”, MessageBoxButtons.YesNo, MessageBoxIcon.Question) 41
  42. Jython JavaのコードからPythonを呼び出す感じ。 import org.python.util.PythonInterpreter; public class JythonTest { public static void main(String[] args) { PythonInterpreter pyi = new PythonInterpreter(); pyi.exec(“print 'Hello, world.'”); pyi.execfile(“hello.py”); // ファイル実行もOK } } 42
  43. Django Pythonで作られたWebフレームワーク モデルを書くだけで テーブルを勝手に作ってくれたり データ編集画面を自動で作ってくれたり 43
  44. Python入門者向けサイト ほぷしぃ http://www.isl.ne.jp/pcsp/python/ PythonWeb http://www.pythonweb.jp/ 44
  45. ご清聴ありがとうございました 45
Advertisement