はじめてのPython(4)

条件分岐

#coding: UTF-8
x = 0
y = 0
z = 0

#if文 インデントによりブロックを生成する
if x == 0:
    print "x=0"
    if y == 1:
        print "y=0"
    if z == 0:
        print "z=0"

#True:真、False:偽を示す予約語
if True:
    print "Always True"

if False:
    print "NotPrintMe"

#オブジェクトの比較
str1 = "ABC"
str2 = "ABC"
str3 = str1

print str1 == str2
print str1 is str2 #is演算子はオブジェクトの比較

print str1 == str3
print str1 is str3

#でも結果はオールTrue。効率化の為にオブジェクトを使い回すためらしい

print str1 is not str2 # is not 演算子はオブジェクトが一致しないときTrue

#論理演算子 「and」「or」「not」
a = 0
b = 1
c = 1
if a == 0 and b == 1 :
    print "Success"
elif a == 0:
    print "Normal"
else :
    print "Failue"

出力

x=0
z=0
Always True
True
True
True
True
False
Success