Python基础7 —— 条件控制语句

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整除
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值