- 追加された行はこの色です。
- 削除された行はこの色です。
** 概要 [#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 \\
- 連結 +
- 繰り返し *
- エスケープ \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>
** len、find、[]を使ってみよう [#j8e0d44b]
- 文字数 len
- 検索 find
s = "abcdefghi"
print len(s)
print s.find("c")
print s.find("x") # 存在しない文字列を指定すると-1を返す
$ python hello.py
9
2
-1
- 切り出し [] [start:end]
s = "abcdefghi"
print s[2] # 2番目の文字列
print s[2:5] # 2番目〜5番目の文字列
print s[:5] # 0番目〜5番目の文字列
print s[2:] # 2番目〜最後の文字列
print s[2:-1] # 2番目〜末尾から1番目の文字列
$ python hello.py
c
cde
abcde
cdefghi
cdefgh
** 数値と文字列の相互変換 [#j52a6dc6]
- 文字列 -> 数値 int float
print 5 + int("5")
print 5 + float("5")
$ python hello.py
10
10.0
- 数値 -> 文字列 str
age = 20
print "i am " + str(age) + " years old!"
$ python hello.py
i am 20 years old!
** リストを使ってみよう [#i5019cf6]
sales = [255, 100, 353, 400, 'aba']
print len(sales) # 4
print sales[2] # 353
sales[2] = 100
print sales[2] # 100
print 100 in sales # 存在チェック(True/False)
$ python hello.py
5
353
100
True
- rangeを使ってリストを自動生成
print range(10) # 10未満の数値
print range(3, 10) # 3〜10未満の数値
print range(3, 10, 2) # 3〜10未満の数値。2つ飛ばし。
$ python hello.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[3, 5, 7, 9]
** sort、reverse、split、joinを使おう [#l542266a]
- 並び替え
# sort / reverse
sales = [50, 100, 80, 45]
sales.sort() # 昇順
sales.reverse() # 逆順。sortと組み合わせることで降順に並び替え可能。
print sales
$ python hello.py
[100, 80, 50, 45]
- 文字列とリストの相互変換
# split / join
d = "2016/5/4"
print d.split("/")
a = ["a", "b", "c"]
print "-".join(a)
$ python hello.py
['2016', '5', '4']
a-b-c
** タプルを使ってみよう [#w400848d]
- タプル:要素の値の変更ができない
- リスト同様にlen + * []を使用することは可能
- リストよりも安全かつ計算速度も速くなる
a = (2, 5, 8)
a[2] = 10
$ python hello.py
Traceback (most recent call last):
File "hello.py", line 4, in <module>
a[2] = 10
TypeError: 'tuple' object does not support item assignment
- タプルとリストの相互変換
a = (2, 5, 8)
b = list(a)
print b
c = tuple(b)
print c
$ python hello.py
[2, 5, 8]
(2, 5, 8)
** セットを使ってみよう [#mac04c74]
- セット(集合体):重複を許さない
a = set([1, 2, 3, 4, 3, 2]) # 2回目の3と2は無視される
print a
$ python hello.py
set([1, 2, 3, 4])
- 集合演算
a = set([1, 2, 3, 4])
b = set([3, 4, 5])
print a - b # 差集合
print a | b # 和集合
print a & b # 積集合
print a ^ b # 排他的論理和
$ python hello.py
set([1, 2])
set([1, 2, 3, 4, 5])
set([3, 4])
set([1, 2, 5])
** 辞書を使ってみよう [#a1574ffd]
sales = {"taguchi":200, "fkoji":300, "tanaka":500}
print sales # 表示順は順不同
$ python hello.py
{'tanaka': 500, 'taguchi': 200, 'fkoji': 300}
sales = {"taguchi":200, "fkoji":300, "tanaka":500}
print sales["taguchi"]
# 値の変更
sales["fkoji"] = 800
print sales
$ python hello.py
200
{'tanaka': 500, 'taguchi': 200, 'fkoji': 800}
sales = {"taguchi":200, "fkoji":300, "tanaka":500}
# 存在チェック
print "taguchi" in sales # True
# keys, values, items一覧表示
print sales.keys()
print sales.values()
print sales .items()
$ python hello.py
True
['tanaka', 'taguchi', 'fkoji']
[500, 200, 300]
[('tanaka', 500), ('taguchi', 200), ('fkoji', 300)]
** 文字列にデータを組み込もう [#b8b7c220]
a = 10
b = 1.234234
c = "taguchi"
d = {"fkoji":200, "tanaka":500}
print "age: %d" % a
print "age: %10d" % a
print "age: %010d" % a
print "age: %f" % b
print "age: %.2f" % b
print "age: %s" % c
$ python hello.py
age: 10
age: 10
age: 0000000010
age: 1.234234
age: 1.23
age: taguchi
# 辞書データの組み込み
a = 10
b = 1.234234
c = "taguchi"
d = {"fkoji":200, "tanaka":500}
print "sales %(fkoji)"
print "%d and %f" % (a, b)
$ python hello.py
sales %(fkoji)
10 and 1.234234