1. 程式人生 > >python 條件語句與數據類型(一)

python 條件語句與數據類型(一)

info 分享 基本語句 額外 -a 縮進 == inpu pass

 

例:

  如果 1=1,那麽就會輸出 "hello world" 否則 輸出 "hello penphy"

代碼塊:

1 if 條件 :
2   print"hello world")
3 else:
4   print ("Error")

  註:if下面的縮進 也就是空格,代表if 下面的代碼塊。 (縮進不正確,將會報錯)

1 if 1 == 1 :
2     print"hello world")
3 else:
4     print ("hello penphy")

  1=1 條件成立,所以執行第二行的代碼 否則 條件不成立 會執行第四行的代碼

1 if 1 == 1
2     if 2 == 2
3         print("hello world")
4         print("hello seattle")
5     else:
6         print("hello pengfei")
7 else:
8     print("hello penphy")

  

技術分享圖片

技術分享圖片

               以上兩張圖, if else 都是可以嵌套的.

  

 1 inp = input(請輸入會員名稱:
) 2 if inp == "高級會員" 3 print(美女) 4 elif inp == "白金會員" 5 print(跑車) 6 elif inp == "鉑金會員" 7 print(一線女演員) 8 else: 9 print(城管) 10 11 print(服務開始吧...)

---根據上面的條件,如果不是輸入這些會員字眼,會自動輸出 城管

總結:

  1. if 基本語句

    if 條件

      內部代碼塊

      內部代碼塊

    else:

    ... #內部代碼塊#

    print(‘....‘)

    滿足if執行條件時 再往下執行

2. if支持嵌套

3 elif語句

 額外補充:pass語句

      pass

      if 1 ==1:

        pass

      else:

        print(‘get out‘)

      註: 如果1 == 1 ,那麽會跳過不執行程序

    

            

            

python 條件語句與數據類型(一)