8. 2. Pythonの基礎
? 変数
>>> spam = "ham"
>>> spam
'ham'
>>> egg
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
egg
NameError: name 'egg' is not defined
変数spamに
文字列hamを代入
未定義の変数を参照した場合
NameError
2014/02/24
8
9. 2. Pythonの基礎
? デバッグ?トレースバック
def main():
x = "ほげ"
y = 123
concat(x, y)
def concat(a, b):
return a + b
if __name__ = "__main__":
main()
$ python sample3.py
Traceback (most recent call last):
File "sample3.py", line 10, in <module>
main()
File "sample3.py", line 4, in main
concat(x, y)
File "sample3.py", line 7, in concat
return a + b
TypeError: cannot concatenate 'str' and
'int' objects
2014/02/24
9
11. 4. 制御構文
# if文
a = 1
b = 2
if a < b:
print "True"
else:
print "False"
True
0
1
2
ok
# for文
for i in range(3):
print i
# while文
while a < b:
print "ok"
a += 1
2014/02/24
11