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

Python入門(PyLadies Caravan in Aichi)

kanan
August 24, 2019

Python入門(PyLadies Caravan in Aichi)

kanan

August 24, 2019
Tweet

More Decks by kanan

Other Decks in Technology

Transcript

  1. 大人のためのPython入門
    PyLadies Caravan in Aichi

    View Slide

  2. ■自己紹介■
    ► PyLadies Caravan STAFF
    ► PyLadies Tokyo にもちょこちょこ参加
    ► 今年の趣味は旅行とキャンプの予定
    ► 仕事はデータ分析屋さんと火消し屋さん。
    ► お酒が好き。名古屋はどんなお酒が有名ですか??
    かなん  @Addition_quince
    福岡出身/東京在住

    View Slide

  3. ※ Attention ※
    今回のお話は 、
    これからPythonを始める方向けに、
    ちょっとだけPythonについて知ってみる
    ためのコンテンツです。

    View Slide

  4. はじめようPython Life★

    View Slide

  5. Pythonって、どんな言語?
    Python とは
    汎用プログラミング言語で、その特徴はこんなかんじ。
    • オープンソースである
    • 文法がシンプルであり、コードが少量で済む
    • Web開発、データ解析(AI)、ゲームといった幅広い分野で使用
    • 多彩なライブラリサポートで高度な計算も容易
    『 Youtube 』 『 EverNote 』 『 Instagram 』に利用されています。

    View Slide

  6. Pythonって、どんな言語?
    Python の起源
    1991年 オランダ人のグイド・ヴァン・ロッサム氏によって開発されたプログラミング
    言語。
    名前の由来は、イギリスのテレビ局BCCが製作・放送した大ヒットコメディ番組であ

    「空飛ぶモンティ・パイソン」からきているとされる。 
    グイド・ヴァン・ロッサム (出典「wikipedia」) 
    6年以上前の1989年12月、私はクリスマス前後の週の暇つぶしのため「趣味」
    のプログラミングプロジェクトを探していた。オフィスは閉まっているが、自宅に
    はホームコンピュータがあるし、他にすることがなかった。私は最近考えていた
    新しいスクリプト言語のインタプリタを書くことにした。それは、ABCからの派生
    であり、Unix/Cハッカーの注意をひきつけるかもしれないと考えた。ちょっとした
    いたずら心から(『空飛ぶモンティ・パイソン』の熱烈なファンだったというのも理
    由の1つ)、プロジェクトの仮称をPythonにした。
    — グイド・ヴァンロッサム、「Programming Python」の序文

    View Slide

  7. View Slide

  8. 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 

    View Slide

  9. Python特徴①:シンプルな文法!
    Python の文法の特徴的な構文ルール「インデント」
    ● インデントは、行頭に空白を入れて文字を入れて字下げを行う事
    ● 同じインデントのまとまりを1つのブロックと識別
    Def hikaku(x):
      f x < 10:
        print(‘少ない’)
       else:
        print(‘たくさん’)
    1インデントは空白4つで表
    されることが多い

    View Slide

  10. Python特徴②:ライブラリが豊富!
    ライブラリとは、
    多彩な計算、データ加工を可能とする、モジュール(Pythonプログラ
    ム)群。
    <例>
    ► datetime :日付時間処理
    ► math :数学計算
    ► numpy :行列演算
    ► Pandas :データ加工
    ► Matplotlib :グラフ描画
    ► scikit-learn :機械学習
    ► Chainer :深層学習

    View Slide

  11. 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

    View Slide

  12. 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.

    View Slide

  13. 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!

    View Slide

  14. Pythonのバージョン:
    ► Python2(2系)とPython3(3系)
    ► Python2は2020年にはサポート終了となる
    ► 学習をはじめるならPython3
    ► 最新バージョンは Python3.7.4

    View Slide

  15. まずは、Pythonを触ってみよう

    View Slide

  16. まずはバージョンを確認
    Windows:
    コマンドプロンプト
    Mac:
    ターミナル
    $ python –-version
    Python 3.7.0
    もし “Python 2.x.x”と表示されたら、
    $ python3 --version
    と打ってみてください

    View Slide

  17. コマンドプロンプトやターミナル上で
    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.
    >>>
    ► インタープリタ(対話)モード
    ► コマンドを発行すると対話的に結果が返ってくる
    ► >>>に変わったら、インタープリタモード中。

    View Slide

  18. コマンドプロンプトやターミナル上で
    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 を表示させてみよう

    View Slide

  19. コマンドプロンプトやターミナル上で
    Pythonを実行してみる
    >>> exit()
    $
    ► インタープリタモードを終了したい時は、
     exit()もしくはctrl + D

    View Slide

  20. (参考)エディタ
    ► 実際にPythonでプログラムを書くときは、エディタと呼ばれるソ
    フトウェアを使うことが多い
    ► みんな自分の好みのエディタをメインで使っています。
    ► Visual Studio Code (ビジュアルスタジオコード)
    ► PyCharm (パイチャーム)
    ► Atom (アトム)
    ► Notepad++ (ノートパッドプラスプラス)
    ► Sublime (サブライム)
    ► Jupyter Notebook (ジュピターノートブック)

    View Slide

  21. (参考)エディタ
    ► PyCharm

    View Slide

  22. (参考)エディタ
    Jupyter Notebookとは、
    ブラウザ形式のテキストエディタ。
    ノートブックと呼ばれる形式でプログラムを作成でき、
    実行結果を確認しながら作業を進めるためのツールです。
    <実行画面>
    結果表示
    処理記載

    View Slide

  23. もう少しだけ、Pythonに詳しくなろう
     ■ データの種類
     ■ 数値を計算してみよう
     ■ 組み込み関数とモジュールを使ってみよう
     ■ 変数を使ってみよう

    View Slide

  24. STEP1:データ形式
    >>> 15
    15
    >>> おはよう
    Traceback (most recent call last):
    File "", line 1, in
    NameError: name 'おはよう' is not defined
    >>> ’15’
    ’15’
    >>> ’おはよう’
    ’おはよう’
    ► インタープリタモードで数字や文字を打ち込んでみよう
    その名前は定義さ
    れていないよってエ
    ラーがでる

    View Slide

  25. STEP1:データ形式
    ► 数値や文字列など、データには形式がある
    数値(整数、小数) 文字列
    真偽 リスト
    ► 小数点以下が必要な場合は、
    小数点をつければOK
    ► 文字列は引用符(’ ’または” ”)
    で囲う必要がある
    ► 2値のTrueかFalseで表される
    ► 数値として扱えるTrue:1/False0
    ► 複数のデータをまとめるオブ
    ジェクト
    ► リストは[’A’,’B’,’C’]と表記
    ► タプルや辞書型等もある

    View Slide

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

    View Slide

  27. STEP2:値の計算
    >>> 5 % 2
    1
    >>> 3 ** 2
    9
    >>> 8 / 2 + 2
    6
    >>> 8 / (2 + 2)
    2
    >>> ‘Pen’ + ‘Pineapple’ + ‘Apple’ + ‘Pen’
    ‘PenPineappleApplePen’
    ► 様々な値の計算ができ、電卓としても使えます
    +をつかって文字列の連結もで
    きる

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. 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日です。’
    ► 次の誕生日まで何日かを表示してみましょう

    View Slide

  32. 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」を指定してください。

    View Slide

  33. 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

    View Slide

  34. 【演習問題①】
    ► 自分の誕生日があと何週間と何日後に来るかを計算してみましょう。
    birthday.pyにコードを追加して拡張します。
    ► 出力:
      〇〇の誕生日まで、△週と■日です。
    ► ヒント:
      int()を使うと小数点を切り捨てて整数になります。
      11 / 2 ←割り算
      11 % 2 ←あまり

    View Slide

  35. 【演習問題①】
    ► 自分の誕生日があと何週間と何日後に来るかを計算してみましょう。
    birthday.pyの最後に追加
    week = int(cnt / 7 )
    day = cnt % 7
    print(name ,‘の誕生日まで’,week,’週と’, day, ’日です。’)

    View Slide

  36. 【演習問題②】
    ► 自分の誕生日が半年以内(182日より小さい)か判定しましょう。
    ► 出力:
      〇〇の誕生日は、半年以内です。
      〇〇の誕生日は、半年以降です。
    ► ヒント:
      条件判定はif文を使用します。
       if 条件に一致すれば
     else それ以外であれば
    if cnt < 10 :
    print(‘AAAA’)
    else :
    print(‘BBBB’)

    View Slide

  37. 【演習問題②】
    ► 自分の誕生日が半年以内(182日より小さい)か判定しましょう。
     birthday.pyの最後に追加
    if cnt < 182:
    print(name,‘の誕生日は、半年以内です。’)
    else:
    print(name,‘の誕生日は、半年以降です。’)

    View Slide

  38. 【if文もやったからfor文をやってみる】
    ► 配列(リスト)を使って1つずつ取り出して繰り返すこともできる
     birthday.pyの最後に追加
    food = [‘ウニ’, ‘メロン’, ‘納豆’]
    for m in food:
    print(name, ’は誕生日に’ , m,’が食べたい。’)

    View Slide

  39. これからPythonをはじめるために

    View Slide

  40. 勉強をはじめるためのポイント
    ► 書籍やサイトは、できれば新しいほうがよい
    ► 書籍の難易度は自分に合ったものを選ぼう
    ► オンライン学習サイトもオススメ

    View Slide

  41. 勉強をはじめるためのポイント
    ► 躓いたり、悩んだりした時は、
     知っている人に聞くのが、最大の近道!
    ► Slack「PyLadies Japan」はメンバーが気軽に
     コミュニケーションを取れる場所です
    ► 質問したら、きっと誰かが答えてくれるので、
     是非、一緒にワイワイしましょう。

    View Slide

  42. はじめようPython Life★

    View Slide