if控制语句
>>> # 若满足if条件,则运行语句块内程序
>>> testStr1 = 'Main'
>>> testStr2 = 'main'
>>> if (testStr1 != testStr2):
... print ("Both of strings are different!")
...
Both of strings are different!
>>> # 若不满足if条件,则不运行语句块内程序
>>> testStr1 = 'main'
>>> testStr2 = 'main'
>>> if (testStr1 != testStr2):
... print ("Both of strings are different!")
...
>>>
if…else…
>>> # 如果满足if条件则执行if语句分支条件下程序
>>> testStr1 = 'Main'
>>> testStr2 = 'main'
>>> if (testStr1 != testStr2):
... print ("Both of strings are different!")
... else:
... print ("Both of strings are same!")
...
Both of strings are different!
>>> # 如果不满足if条件则执行else语句分支下程序
>>> testStr1 = 'main'
>>> testStr2 = 'main'
>>> if (testStr1 != testStr2):
... print ("Both of strings are different!")
... else:
... print ("Both of strings are same!")
...
Both of strings are same!
elif多条件判断
>>> # 如果满足if条件则执行if语句分支条件下程序
>>> testNum = 10
>>> if testNum > 5:
... print (str(testNum) + " is greater than 5")
... elif testNum < 5:
... print (str(testNum) + " is less than 5")
... else:
... print (str(testNum) + " is equals 5")
...
10 is greater than 5
>>> # 如果不满足if条件,判断是否符合elif条件,如满足执行elif语句分支条件下程序
>>> testNum = 2
>>> if testNum > 5:
... print (str(testNum) + " is greater than 5")
... elif testNum < 5:
... print (str(testNum) + " is less than 5")
... else:
... print (str(testNum) + " is equals 5")
...
2 is less than 5
>>> # 如果不满足if条件,也不满足elif条件,则执行else语句分支条件下程序
>>> testNum = 5
>>> if testNum > 5:
... print (str(testNum) + " is greater than 5")
... elif testNum < 5:
... print (str(testNum) + " is less than 5")
... else:
... print (str(testNum) + " is equals 5")
...
5 is equals 5
嵌套if
>>> testNum = 10
>>> if testNum%2 == 0:
... if testNum%5 == 0:
... print (str(testNum) + "能够被2整除并且能够被5整除")
... else:
... print (str(testNum) + "能够被2整除但不能被5整除")
... else:
... print(str(testNum) + "不能被2整除")
...
10能够被2整除并且能够被5整除
>>> testNum = 6
>>> if testNum%2 == 0:
... if testNum%5 == 0:
... print (str(testNum) + "能够被2整除并且能够被5整除")
... else:
... print (str(testNum) + "能够被2整除但不能被5整除")
... else:
... print(str(testNum) + "不能被2整除")
...
6能够被2整除但不能被5整除
>>> testNum = 9
>>> if testNum%2 == 0:
... if testNum%5 == 0:
... print (str(testNum) + "能够被2整除并且能够被5整除")
... else:
... print (str(testNum) + "能够被2整除但不能被5整除")
... else:
... print(str(testNum) + "不能被2整除")
...
9不能被2整除
逻辑运算符(and,or)
>>> testNum = 10
>>> if testNum%2==0 and testNum%5==0:
... print (str(testNum) + "能够被2和5整除")
... else:
... print (str(testNum) + "不能被2和5整除")
...
10能够被2和5整除
>>> testNum = 6
>>> if testNum%2==0 and testNum%5==0:
... print (str(testNum) + "能够被2和5整除")
... else:
... print (str(testNum) + "不能被2和5整除")
...
6不能被2和5整除
>>> testNum = 6
>>> if testNum%2==0 or testNum%5==0:
... print (str(testNum) + "能够被2或5整除")
... else:
... print (str(testNum) + "不能被2或5整除")
...
6能够被2或5整除
>>> testNum = 15
>>> if testNum%2==0 or testNum%5==0:
... print (str(testNum) + "能够被2或5整除")
... else:
... print (str(testNum) + "不能被2或5整除")
...
15能够被2或5整除
>>> testNum = 9
>>> if testNum%2==0 or testNum%5==0:
... print (str(testNum) + "能够被2或5整除")
... else:
... print (str(testNum) + "不能被2或5整除")
...
9不能被2或5整除