條件和條件語句

4.1 這就是布爾變量的做用

  1. 說明:布爾值,
    1. 假值:false,None,全部類型的數字0,空序列,空字典;
    2. 真值:全部的非空值;
    3. bool函數能夠用來將其餘值轉換成布爾值;
  2. 注意:儘管假值具備不一樣的類型,可是不一樣的假值以前也是 不相等 的
  3. 例子:
    1:  >>> True
    
     2:  True
    
     3:  >>> False
    
     4:  False
    
     5:  >>> []
    
     6:  []
    
     7:  >>> bool ([])
    
     8:  False
    
     9:  >>> bool ([1,])
    
    10:  True
    
    11:  >>> bool (0)
    
    12:  False
    
    13:  >>> bool (0.0)
    
    14:  False
    
    15:  >>> bool (0.1)
    
    16:  True
    
    17:  #不一樣的假值之間也是不相同的
    
    18:  >>> [] == {}
    
    19:  False
    
    20:  >>> [] == None
    
    21:  False
    
    22:  >>>
    
    23:

4.2 條件執行和if語句

  1. 說明:if 判斷其後面的條件語句是否爲真,若是爲真,執行if後面的語句 塊,不然不執行;

4.3 else子句

  1. 說明:之因此稱爲子句是由於else必須跟在if語句後面,而不能單獨使用;

4.4 elif子句

  1. 說明:若是須要更多的判斷,能夠使用elif,判斷更多的條件;
  2. 例子:
    1:  #if, elif, else應用
    
     2:  num = input("Please enter a number:")
    
     3:  num = int(num)
    
     4:  if num > 0:
    
     5:          print ('You input a positive number!')
    
     6:  elif num < 0:
    
     7:          print ('You input a negative number!')
    
     8:  else:
    
     9:      print ('You input a zero!')
    
    10:

4.5 嵌套代碼塊

  1. 說明:在if判斷後,還須要進一步進行判斷就能夠使用嵌套代碼的方式。
  2. 例子:
    1:  key = input("Please select type, color(c) or number(n):")
    
     2:  if key == 'c':
    
     3:      color = input ("Please select a color, Red(r), Green(g), Blue(b):")
    
     4:      if color == 'r':
    
     5:          print('You selected red')
    
     6:      elif color == 'g':
    
     7:          print('You selected green')
    
     8:      elif color == 'b':
    
     9:          print('You selected blue')
    
    10:      else:
    
    11:          print("Illegal color type!")
    
    12:  else:
    
    13:      print ("You select number!")
    
    14:

4.6 更復雜的條件

 

4.6.1 比較運算符

  1. 說明:
    1. x==y: 等於;
    2. x<y:  小於;
    3. x>y: 大於;
    4. x<=y: 小於等於;
    5. x>=y: 大於等於;
    6. x!=y: 不等於;
    7. x is y:x和y是同一對象;
    8. x is not y:x和y不是同一對象;
    9. x in y: x在y中;
    10. x not in y: x不在y中;
  2. 注意:
    1. 比較運算符是可鏈接的,例如:14 < age < 26;
    2. 比較運算符不能比較不一樣類型的數據;

4.6.2 相等運算符

  1. 說明:用來判斷兩個數據是否相等;

4.6.3 同一性運算符

  1. 說明:用於判斷兩個變量是否指向同一對象;
  2. 注意:避免把 is 比較運算符應用於比較常量值,如數字,字符串等。 即 避免如下比較:
    1:  if '123' is '123':

4.6.4 成員資格運算符

  1. 說明:判斷元素是否被包含在對象中;

4.6.5 字符串和序列比較

  1. 說明:字符串能夠按照字母順序排列進行比較;

4.6.6 布爾運算符

  1. 說明:包括,and, or, not
  2. 例子:
    1:  #or的特殊用法,若是沒有輸入,則會返回or後面的值
    
     2:  >>> name = input("Please enter a name:") or '<unknown>'
    
     3:  Please enter a name:
    
     4:  >>> name
    
     5:  '<unknown>'
    
     6:  
    
     7:  >>> a = 'a'
    
     8:  >>> c = 'c'
    
     9:  #若是if後面的判斷語句爲真,返回a
    
    10:  >>> a if True else c
    
    11:  'a'
    
    12:  #若是if後面的判斷語句爲假,返回c
    
    13:  >>> a if False else c
    
    14:  'c'
    
    15:  >>>
    
    16:

4.7 斷言

  1. 說明:關鍵字爲 assert , 若是斷言的條件判斷爲假,則程序直接崩潰
  2. 例子:
    1:  >>> age = 10
    
     2:  >>> assert 1<age<120, "Age must be realistic"
    
     3:  >>> age = -1
    
     4:  >>> assert 1<age<120, "Age must be realistic"
    
     5:  Traceback (most recent call last):
    
     6:    File "<pyshell#26>", line 1, in <module>
    
     7:      assert 1<age<120, "Age must be realistic"
    
     8:  AssertionError: Age must be realistic
    
     9:  >>>
    
    10:
相關文章
相關標籤/搜索