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

Python入門(PyLadies Caravan in Hokkaido)

kanan
December 21, 2019

Python入門(PyLadies Caravan in Hokkaido)

kanan

December 21, 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.4

    もし “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. >>> ► インタープリタ(対話)モード ► コマンドを発行すると対話的に結果が返ってくる ► >>>に変わったら、インタープリタモード中。 $ python3 --version と打った人は原則 python を python3 と読み替えが必要です。 $ python3
  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. STEP1:データ形式 >>> 15 15 >>> おはよう Traceback (most recent call

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

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

    10 >>> 'Pen' + 'Pineapple' + 'Apple' + 'Pen' 'PenPineappleApplePen' ► 様々な値の計算ができ、電卓としても使えます 足し算、引き算、掛け算、割り 算等、様々な計算ができる +をつかって文字列の連結もできる
  14. STEP3:変数を使ってみよう >>> name = '桃太郎' >>> print(name) '桃太郎' >>> animal

    = ['いぬ', '猿', 'キジ', '鬼'] >>> print(animal) ['いぬ', '猿', 'キジ', '鬼'] ► 変数と呼ばれるデータを入れる箱を使うことで、処理の幅が広 がります。
  15. STEP4:組み込み関数やモジュールを利用する >>> 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)を一 緒に使えない
  16. STEP4:組み込み関数やモジュールを利用する >>> import datetime >>> datetime.date.today() datetime.date(2019, 10, 05) >>>

    print(datetime.date.today()) 2019-10-05 ► Pythonには組み込み関数以外にもモジュール(ライブラリ)によって 様々な処理を呼び出すことができます。 ※ライブラリを使う時は、import を使って読み込みが必要 日付型のオブジェクトとして 生成される import ••• as ◾◾ ↓ •••ライブラリを読み込んで、 ◾◾という略称をつけるよ
  17. STEP4:組み込み関数やモジュールを利用する >>> import requests Traceback (most recent call last): File

    "<stdin>", line 1, in <module> ImportError:No module named requests >>> exit() $ pip install pyladies $ pip install requests ► ライブラリには標準ライブラリと、外部ライブラリがある 外部ライブラリはインストールが必要!! 試しにHTTP通信ライブラリをインストールしてみよう 冒頭でpython3 --version と打った人はpip3 install ••• >>> import requests >>>
  18. エディやIDE(統合開発環境) ► 実際にPythonでプログラムを書くときは、エディタやIDE(統合開発 環境)と呼ばれるソフトウェアを使うことが多い ► 自分の好みを見つけるとよい ► Visual Studio Code (ビジュアルスタジオコード)

    ► PyCharm (パイチャーム) ► Atom (アトム) ► Notepad++ (ノートパッドプラスプラス) ► Sublime (サブライム) ► Jupyter Notebook (ジュピターノートブック)
  19. EXTRA:次の誕生日まで何日か計算しよう import datetime name = 'かなん' today = datetime.date.today() next_birth

    = datetime.date(2020, 5, 18) cnt = (next_birth - today).days print(name + 'の誕生日は'+ str(next_birth) + 'です。') print(name, 'の誕生日まで' , cnt, '日です。')
  20. EXTRA:関数を作ると便利になるよ import datetime def birthday(name, y, m, d): today =

    datetime.date.today() next_birth = datetime.date(y, m, d) cnt = (next_birth – today).days print(name + 'の誕生日は' + str(next_birth) + 'です。') print(name, 'の誕生日まで' , cnt, '日です。') birthday('かなん', 2020, 5, 18) birthday('まーや', 2020, 5, 16) 関数化