if statement

  1. Python 程式語言認定任何 non-zero, non-null 的值為 true,zero 或 null 則為 false。
  2. 流程圖

    \begin{tikzpicture}[node distance=1.5cm,auto]\setlength \baselineskip{1ex}
......

  3. 語法
    if expression:
    	statement(s)
    
  4. 範例:輸入一個數字或字串,如果 TRUE 則印出結果。
    pyuser@deyu:~/zzz$ cat if0.py
    #!/usr/bin/env python
    
    var = input("Enter a number or string: ")
    if var:
    	print("Got a true expression value", var)
    
  5. 執行程式,輸入 123 或 asd 都是 TRUE,印出結果。
    pyuser@deyu:~/zzz$ ./if0.py
    Enter a number or string: 123
    Got a true expression value 123
    pyuser@deyu:~/zzz$ ./if0.py
    Enter a number or string: asd
    Got a true expression value asd
    
  6. 執行程式,不輸入任何字元為 FALSE,直接退出。
    pyuser@deyu:~/zzz$ ./if0.py
    Enter a number or string: