slow-starter

なにをやるにもslow start……。

python_019

The Zen of Python

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.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

python_018

2. Python インタプリタを使う

Python チュートリアル — Python 3.6.13 ドキュメント

2. Python インタプリタを使う  
    2.1. インタプリタを起動する  
        2.1.1. 引数の受け渡し  
        2.1.2. 対話モード  
    2.2. インタプリタとその環境  
        2.2.1. ソースコードの文字コード  

スクリプトまたは対話式でのpythonの実行

  • インタプリタの起動
    • pythonコマンド (インストール済みでPATHが通っている必要がある。)
    • 引数の扱い
# args-test.py
import sys
# sysモジュールのインポートが必須
a = sys.argv
# sys.argvに引数が格納される
print(a)
# 引数を表示
print(type(a))
# sys.argvの型を確認
> python args-test.py abc
['args-test.py', 'abc']
# sys.argv[0]はファイル名自体らしい。
<class 'list'>
# sys.argvの型はリスト
>
#!/usr/bin/env python3
# -*- coding: cp932 -*-

その他

  • 文字コード指定のサンプルに使用されていた「cp1252( windows-1252)は「西ヨーロッパ言語」。
  • 日本語環境で作業するなら「cp932」「utf8」とかが多そう。