概要 †
動作環境 †
$ python --version
Python 2.7.5
Hello World †
$ 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()
# coding: UTF-8
# 日本語コメントを記載する場合には文字コードを明記
print "hello world"
$ python hello.py
hello world
変数とデータ型について †
# coding: UTF-8
msg = "hello world" # 変数名は大文字・小文字を区別する
print msg
$ python hello.py
hello world
- データ型の種類
- 数値
- 真偽値 True False
- None
- 関数・オブジェクト
- 要素が並んだもの
- 文字列:文字が並んだもの
- リスト:データが並んだもの
- タプル:データが並んだもの(変更ができない)
- セット:データが並んだもの(重複を許さない)
- 辞書:キーと値がペア
数値を使ってみよう †
- 整数、少数、複素数
- 演算子 + - * / // % **
# 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
文字列を使ってみよう †
- 文字列はダブルクォートまたはシングルクォートで囲む
- 日本語の文字列の前には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>