#contents ** Pythonを使ってみよう [#peb4c9dd] - 概要 -- シンプルで習得がしやすいオブジェクト指向言語 -- データ解析、機械学習 - 公式サイト -- https://www.python.org/ - 環境 -- Vagrant ローカル開発環境 (CentOS 6.7) ** Python 3 インストール [#j4dacc82] - CentOS 6 標準では Python 2 がインストールされているため pyenv を利用して Python 3 をインストール - epel リポジトリをダウンロード $ wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm - epel リポジトリをインストール $ sudo rpm -Uvh epel-release-6-8.noarch.rpm - Ansible インストール $ sudo yum -y install ansible $ ansible --version ansible 2.3.0.0 - git, patch をインストール $ sudo yum -y install git $ sudo yum install patch - Playbook 作成 $ vi playbook.yml --- - hosts: localhost connection: local sudo: yes vars: - python_version: 3.5.2 tasks: - name: disable iptables service: name=iptables state=stopped enabled=no - name: install libselinux-python yum: name=libselinux-python state=latest - name: remove localtime file: path=/etc/localtime state=absent - name: change timezone file: src=/usr/share/zoneinfo/Asia/Tokyo dest=/etc/localtime state=link force=yes mode=0644 - name: change locale lineinfile: >- dest=/etc/sysconfig/i18n state=present regexp=^LANG= line='LANG="ja_JP.UTF-8"' - name: install dependencies yum: name={{item}} state=present with_items: - gcc - openssl - openssl-devel - rpm-build - gcc-c++ - bzip2 - bzip2-devel - libtool - zlib - zlib-devel - httpd-devel - openssl-devel - curl-devel - ncurses-devel - gdbm-devel - readline - readline-devel - sqlite - sqlite-devel - libyaml-devel - libffi-devel - bison - name: check pyenv installed command: test -x /home/vagrant/.pyenv register: pyenv_present ignore_errors: yes become: no - name: git clone pyenv git: repo=https://github.com/yyuu/pyenv.git dest=/home/vagrant/.pyenv when: pyenv_present.rc != 0 become: no - name: check pyvirtual installed command: test -x /home/vagrant/.pyenv/plugins/pyenv-virtualenv register: pyvirtual_present ignore_errors: yes become: no - name: git clone pyenv-virtual git: repo=https://github.com/yyuu/pyenv-virtualenv.git dest=/home/vagrant/.pyenv/plugins/pyenv-virtualenv when: pyvirtual_present != 0 become: no - name: update pyenv command: git pull --rebase chdir=/home/vagrant/.pyenv become: no - name: update pyenv-virtualenv command: git pull --rebase chdir=/home/vagrant/.pyenv/plugins/pyenv-virtualenv become: no - name: check python installed shell: /bin/bash -lc "pyenv versions | grep {{python_version}}" register: python_installed ignore_errors: yes become: no - name: install python shell: /bin/bash -lc "pyenv install {{python_version}} && pyenv rehash && pyenv global {{python_version}}" when: python_installed.rc != 0 become: no - Playbook 実行 $ ansible-playbook playbook.yml - &color(red){※}; エラーとなるため下記を手動で実施 (to be automated) $ vi .bashrc export PYENV_ROOT="${HOME}/.pyenv" if [ -d "${PYENV_ROOT}" ]; then export PATH=${PYENV_ROOT}/bin:$PATH eval "$(pyenv init -)" fi $ source ~/.bashrc - Playbook 再実行 $ ansible-playbook playbook.yml - Python バージョン確認 $ python -V Python 3.5.2 - 作業ディレクトリ $ pwd /home/vagrant/python3_lessons ** はじめてのPythonプログラム [#x1a702dc] - インタラクティブモード $ python Python 3.5.2 (default, May 20 2017, 02:46:30) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 3 + 2 5 >>> exit() - スクリプト作成 (myapp.py) # coding: utf-8 print("Hello World") - 実行結果 $ python myapp.py Hello World - コメント # coding: utf-8 """ Comment Comment Comment """ # Comment print("Hello World") - Python 2 と Python 3 の違い -- Python 2 では print に括弧は不要だが Python 3 では必須 -- Python 2 では日本語コメントを記載する場合に # coding: utf-8 を明記したが Python 3 では必須ではない ** 定数について [#s903f012] - 値を再代入できない - Python では言語仕様として定数はサポートされていない - 変数名を全て大文字にすると慣習的に定数とみなすことが多いので値の変更は避ける ADMIN_EMAIL = "yuji@example.com" ** データの演算について [#z883940a] - 変数に他の変数を代入 y = 4 # y = y + 12 y += 12 print(y) # 16 - 論理計算 print(True and False) # False print(True or False) # True print(not True) # False ** 文字列に値を埋め込んでみよう [#sadcc680] - % を利用した記法 name = "yuji" score = 52.8 print("name: %s, score: %f" % (name, score)) - 実行結果 $ python myapp.py name: yuji, score: 52.800000 - % を利用した記法 (桁数指定、行揃い指定) name = "yuji" score = 52.8 print("name: %-10s, score: %10.2f" % (name, score)) - 実行結果 $ python myapp.py name: yuji , score: 52.80 - {} を利用した記法 name = "yuji" score = 52.8 print("name: {0}, score: {1}".format(name, score)) - 実行結果 $ python myapp.py name: yuji, score: 52.8 - {} を利用した記法 (桁数指定、行揃い指定) name = "yuji" score = 52.8 print("name: {0:>10s}, score: {1:<10.2f}".format(name, score)) - 実行結果 $ python myapp.py name: yuji, score: 52.80 ** ifで条件分岐をしてみよう [#p91e87f8] - ユーザーからの入力を受け付けて if 文で条件分岐 &color(red){※}; ユーザー入力は文字列が返るので数値に変換が必要 score = int(input("score ? ")) if score > 80: print("Great!") elif score > 60: print("Good!") else: print("so so ...") - 実行結果 $ python myapp.py score ? 90 Great! - 条件演算子を使った記法 (if else) score = int(input("score ? ")) print("Great!" if score > 80 else "so so ...") - &color(red){※}; Python ではインデントに半角スペース4つを利用 ** 関数の返り値を使ってみよう [#qa75b13b] - 返り値を指定した関数 def say_hi(): return "hi" print("hello") # return 後の記述のため処理されない msg = say_hi() print(msg) - 実行結果 $ python myapp.py hi - pass の利用 &color(red){※}; 返り値は None という特殊な値 def say_hi(): pass msg = say_hi() print(msg) - 実行結果 $ python myapp.py None ** 変数のスコープを理解しよう [#ka60680e] - ローカル変数: 関数内で定義された変数 - グローバル変数: 関数の外で定義された変数 -- 関数内から参照は可能だが書き換えは不可 - 関数内でグローバル変数を書き換える方法 (明示的にグローバル変数を定義) msg = "hello" # グローバル変数 def say_hi(): global msg msg = "hello global" print(msg) say_hi() print(msg) ** クラス変数を使ってみよう [#v2f54ead] class User: # クラス変数 count = 0 def __init__(self, name): User.count += 1 self.name = name print(User.count) # 0 tom = User("tom") bob = User("bob") print(User.count) # 2 print(tom.count) # 2 - 実行結果 $ python myapp.py 0 2 2 ** メソッドを使ってみよう [#zea107b7] - メソッド: クラスに定義した関数 - コンストラクタもメソッドの一つ