Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python入門(PyLadies Caravan in Aichi)

Avatar for kanan kanan
August 24, 2019

Python入門(PyLadies Caravan in Aichi)

Avatar for kanan

kanan

August 24, 2019
Tweet

More Decks by kanan

Other Decks in Technology

Transcript

  1. ▪自己紹介▪ ► PyLadies Caravan STAFF ► PyLadies Tokyo にもちょこちょこ参加 ►

    今年の趣味は旅行とキャンプの予定 ► 仕事はデータ分析屋さんと火消し屋さん。 ► お酒が好き。名古屋はどんなお酒が有名ですか?? かなん  @Addition_quince 福岡出身/東京在住
  2. Pythonって、どんな言語? Python の起源 1991年 オランダ人のグイド・ヴァン・ロッサム氏によって開発されたプログラミング 言語。 名前の由来は、イギリスのテレビ局BCCが製作・放送した大ヒットコメディ番組であ る 「空飛ぶモンティ・パイソン」からきているとされる。  グイド・ヴァン・ロッサム (出典「wikipedia」) 

    6年以上前の1989年12月、私はクリスマス前後の週の暇つぶしのため「趣味」 のプログラミングプロジェクトを探していた。オフィスは閉まっているが、自宅に はホームコンピュータがあるし、他にすることがなかった。私は最近考えていた 新しいスクリプト言語のインタプリタを書くことにした。それは、ABCからの派生 であり、Unix/Cハッカーの注意をひきつけるかもしれないと考えた。ちょっとした いたずら心から(『空飛ぶモンティ・パイソン』の熱烈なファンだったというのも理 由の1つ)、プロジェクトの仮称をPythonにした。 — グイド・ヴァンロッサム、「Programming Python」の序文
  3. Python特徴①:シンプルな文法! Python の文法は本当にシンプルなのか? 2つの値(a, b)の最大公約数を求めるプログラム をPython, Java, Rubyの3つの言語で比較。 ※最大公約数を求めるアルゴリズムはユークリッドの互除 法を使用 

    def gcd(a, b): while b > 0: a, b = b, a % b return a def gcd(a, b) a, b = b, a if a > b until a == 0 a, b = b % a, a end return b end private static long getKoyakusu(long a, long b) { long candidate = a; while (b % a != 0) { candidate = b % a; b = a; a = candidate; } return candidate; } Java  Ruby  Python 
  4. Python特徴②:ライブラリが豊富! ライブラリとは、 多彩な計算、データ加工を可能とする、モジュール(Pythonプログラ ム)群。 <例> ► datetime :日付時間処理 ► math

    :数学計算 ► numpy :行列演算 ► Pandas :データ加工 ► Matplotlib :グラフ描画 ► scikit-learn :機械学習 ► Chainer :深層学習
  5. Pythonの思想:The Zen of Python ► 1. Beautiful is better than

    ugly. ► 2. Explicit is better than implicit. ► 3. Simple is better than complex. ► 4. Complex is better than complicated. ► 5. Flat is better than nested. ► 6. Sparse is better than dense. ► 7. Readability counts. ► 8. Special cases aren't special enough to break the rules.  $ import this
  6. Pythonの思想:The Zen of Python ► 9. Although practicality beats purity.

    ► 10. Errors should never pass silently. ► 11. Unless explicitly silenced. ► 12. In the face of ambiguity, refuse the temptation to guess. ► 13. There should be one-- and preferably only one --obvious way to do it. ► 14. Although that way may not be obvious at first unless you're Dutch.
  7. Pythonの思想:The Zen of Python ► 15. Now is better than

    never. ► 16. Although never is often better than *right* now. ► 17. If the implementation is hard to explain, it's a bad idea. ► 18. If the implementation is easy to explain, it may be a good idea. ► 19. Namespaces are one honking great idea -- let's do more of those!
  8. まずはバージョンを確認 Windows: コマンドプロンプト Mac: ターミナル $ python –-version Python 3.7.0

    もし “Python 2.x.x”と表示されたら、 $ python3 --version と打ってみてください
  9. コマンドプロンプトやターミナル上で Pythonを実行してみる $ python Python 3.7.0 (default, Jun 28 2018,

    08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ► インタープリタ(対話)モード ► コマンドを発行すると対話的に結果が返ってくる ► >>>に変わったら、インタープリタモード中。
  10. コマンドプロンプトやターミナル上で Pythonを実行してみる >>> import this The Zen of Python, by

    Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex.        : ► The Zen of Python を表示させてみよう
  11. (参考)エディタ ► 実際にPythonでプログラムを書くときは、エディタと呼ばれるソ フトウェアを使うことが多い ► みんな自分の好みのエディタをメインで使っています。 ► Visual Studio Code (ビジュアルスタジオコード)

    ► PyCharm (パイチャーム) ► Atom (アトム) ► Notepad++ (ノートパッドプラスプラス) ► Sublime (サブライム) ► Jupyter Notebook (ジュピターノートブック)
  12. STEP1:データ形式 >>> 15 15 >>> おはよう Traceback (most recent call

    last): File "<stdin>", line 1, in <module> NameError: name 'おはよう' is not defined >>> ’15’ ’15’ >>> ’おはよう’ ’おはよう’ ► インタープリタモードで数字や文字を打ち込んでみよう その名前は定義さ れていないよってエ ラーがでる
  13. STEP1:データ形式 ► 数値や文字列など、データには形式がある 数値(整数、小数) 文字列 真偽 リスト ► 小数点以下が必要な場合は、 小数点をつければOK

    ► 文字列は引用符(’ ’または” ”) で囲う必要がある ► 2値のTrueかFalseで表される ► 数値として扱えるTrue:1/False0 ► 複数のデータをまとめるオブ ジェクト ► リストは[’A’,’B’,’C’]と表記 ► タプルや辞書型等もある
  14. STEP2:値の計算 >>> 10 + 2.5 12.5 >>> 3 - 5

    -2 >>> 2 * 5 10 >>> 10 / 5 2 ► 様々な値の計算ができ、電卓としても使えます 足し算、引き算、掛け算、割り 算等、様々な計算ができる
  15. STEP2:値の計算 >>> 5 % 2 1 >>> 3 ** 2

    9 >>> 8 / 2 + 2 6 >>> 8 / (2 + 2) 2 >>> ‘Pen’ + ‘Pineapple’ + ‘Apple’ + ‘Pen’ ‘PenPineappleApplePen’ ► 様々な値の計算ができ、電卓としても使えます +をつかって文字列の連結もで きる
  16. STEP3:組み込み関数やモジュールを利用 する >>> 47 + ‘都道府県’ Traceback (most recent call

    last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> str(47) + ‘都道府県’ ‘47都道府県’ ► Pythonには便利な関数が用意されています。 ► print()、sum()、round()など様々な組み込み関数がある +は数値(int)と文字(str)を一 緒に使えない
  17. STEP3:組み込み関数やモジュールを利用する >>> import datetime >>> datetime.date.today() datetime.date(2019, 1, 13) >>>

    print(datetime.date.today()) 2019-01-13 ► Pythonには組み込み関数以外にもモジュール(ライブラリ)に よって様々な処理を呼び出すことができます。 日付型のオブジェクトとして 生成される
  18. STEP4:変数を使ってみよう >>> import datetime >>> nextBD = datetime.date(2019,5,18) >>> print(nextBD)

    2019-05-18 >>> name = ‘かなん’ >>> print(name) ‘かなん’ ► 変数と呼ばれるデータを入れる箱を使うことで、処理の幅が広 がります。
  19. STEP4:変数を使ってみよう >>> import datetime >>> today = datetime.date.today() >>> nextBD

    = datetime.date(2019,5,18) >>> cnt = (nextBD – today).days >>> name = ‘かなん’ >>> print(name + ’の誕生日は’+ str(nextBD) + ’です。’) ‘かなんの誕生日は2019-05-18です。’ >>> print(name,’の誕生日まで’ ,cnt, ‘日です。’) ‘かなんの誕生日まで125日です。’ ► 次の誕生日まで何日かを表示してみましょう
  20. EXTRA:Pythonファイルを実行しよう import datetime today = datetime.date.today() nextBD = datetime.date(2020,5,18) cnt

    = (nextBD – today).days name = ‘かなん’ print(name + ‘の誕生日は’+ str(nextBD) + ’です。’) print(name,’の誕生日まで’ ,cnt, ‘日です。’) ► 先ほどインタープリタモードで打ったプログラムを  テキストエディタに打ち込み「birthday.py」という名前で保存しましょう。 ► ファイルの文字コードは「UTF-8」を指定してください。
  21. EXTRA:Pythonファイルを実行しよう ► 先ほどインタープリタモード終了して、保存したBD.pyを実行します ► Pythonファイルの実行は $ Python ~~.py >>> exit()

    ※BD.pyがあるディレクトリまで移動 ※WindowsおよびMacも移動はcd $ python birthday.py かなんの誕生日は2020-05-18です。 かなんの誕生日まで268日です。 バージョン確認の時、 $ python3 --version と打った方は、 $ python3 birthday.py