- 追加された行はこの色です。
- 削除された行はこの色です。
** 概要 [#dc50af11]
- シンプルで習得しやすいオブジェクト指向言語
- Google App Engine、Django
- 公式サイト: https://www.python.org/
** 動作環境 [#uc1f4fd9]
$ python --version
Python 2.7.5
** Hello World [#r9db7290]
- インタラクティブ実行モード(対話モード)
$ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> exit()
- スクリプトファイル作成(hello.py)
# coding: UTF-8
# 日本語コメントを記載する場合には文字コードを明記
print "hello world"
$ python hello.py
hello world
** 変数とデータ型について [#f94eb04d]
- 変数の使い方
# coding: UTF-8
msg = "hello world" # 変数名は大文字・小文字を区別する
print msg
$ python hello.py
hello world
- データ型の種類
-- 数値
-- 真偽値 True False
-- None
-- 関数・オブジェクト
-- 要素が並んだもの
--- 文字列:文字が並んだもの
--- リスト:データが並んだもの
--- タプル:データが並んだもの(変更ができない)
--- セット:データが並んだもの(重複を許さない)
--- 辞書:キーと値がペア
** 数値を使ってみよう [#r26c2163]
- 整数、少数、複素数
- 演算子 + - * / // % **
# coding: UTF-8
print 10 * 5 # 50(乗算)
print 10 // 3 # 3(商)
print 10 % 3 # 1(余り)
print 2 ** 3 # 8(べき乗)
$ python hello.py
50
3
1
8
- 整数と小数の演算 → 小数
# coding: UTF-8
print 5 + 2.0
$ python hello.py
7.0
- 整数同士の割り算 → 切り捨ての整数
# coding: UTF-8
print 10 / 3
print 10 / 3.0
$ python hello.py
3
3.33333333333
** 文字列を使ってみよう [#r1df6598]
- 文字列はダブルクォートまたはシングルクォートで囲む
- 日本語の文字列の前にはUnicodeの意味の"u"を付ける
- 連結 +
- 繰り返し *
- エスケープ \n \t \\
print "hello" + "world"
print u"無駄!" * 10
print 'hello\n wo\trld\\ it\'s a pen'
# 改行を含む文字列
print """<html lang="ja">
<body>
</body>
</html>"""
$ python hello.py
helloworld
無駄!無駄!無駄!無駄!無駄!無駄!無駄!無駄!無駄!無駄!
hello
wo rld\ it's a pen
<html lang="ja">
<body>
</body>
</html>