概要

動作環境

$ 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

数値を使ってみよう

# 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

文字列を使ってみよう

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>

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS