LoginSignup
6
6

More than 5 years have passed since last update.

Python を久々に使う時に役立つメモ

Posted at

インデントの役割

Python ではインデントを使ってブロックを表記する。

また、コロン(:)で終わる行がブロックの始まりを表す。

if num == 1:
    print "One."
elif num == 2:
    print "Two."
else:
    print "N/A."

Shebang

Shebang として、 Python スクリプトの1行目に以下を記述する。

#!/usr/bin/env python

Coding Style

Coding Style は PEP 8 を参考にする。
PEP 8 とは、Python Community がまとめた Python 向けの Style Guide 。

PEP 8 - Style Guide for Python Code
https://www.python.org/dev/peps/pep-0008/

日本語訳もあります。
http://pep8-ja.readthedocs.org/ja/latest/

  • インデントは「スペース4つ」
  • メソッド名は小文字とアンダースコアーを使う
  • インスタンス変数は小文字とアンダースコアーを使う
  • 定数名は大文字とアンダースコアを使う
    • 例) MAX_OVERFLOW

サンプルコード

sample.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-

num = 5

for i in range(num):
    print i, ": Hello!"
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6