python if 語句,布爾運算

來源:互聯網
上載者:User

標籤:if   布爾運算   

一:

    if 語句:

    作用:讓程式根據條件選擇性的執行某條語句或某些語句

    文法:

    if 真值運算式1:

            語句塊1...

    elif 真值運算式2:

            語句塊2....

        ............

    else:

            語句塊n.........

    說明:

        1,真值運算式判斷會自上而下進行,如果有一條為True則執行其中的語句塊,然後結束if 語句的執行,如果所有的真值運算式都為False。則執行else子句裡的語句。

        2,elif 子句可以有0個,1個,或多個

        3,else 子句可以有0個或多個

if語句的嵌套

if語句本身是由多條語句組成的一條複合陳述式

if語句可以作為語句嵌套到另一個語句的內部

注意:

  • 1、每個條件後面要使用冒號(:),表示接下來是滿足條件後要執行的語句塊。

  • 2、使用縮排來劃分語句塊,相同縮排數的語句在一起組成一個語句塊。

  • 3、在Python中沒有switch – case語句。


如:輸入一個正整數,判斷這個數是基數還是偶數,列印出結果。

n = int(input("請輸入一個數:")if n % 2 == 0:        print(n,"是偶數")else:        print(n,"是奇數")運行結果:[[email protected] data]# ./test.py   請輸入一個數:44 是偶數[[email protected] data]# ./test.py 請輸入一個數:33 是奇數[[email protected] data]#

二:條件運算式:

    文法:運算式1 if 真值運算式 else 運算式2

    作用:

        如果真值運算式的布爾值為True 時,則執行運算式1,並返回結果對象的引用,否則則執行運算式2,並返回對象的引用。

    如:

#商場促銷,滿100減20

money = int(input("請輸入商品金額:"))pay = money - 20 if money >= 100 else moneyprint("需要支付:", pay, "元")[[email protected] data]# ./test.py 請輸入商品金額:200需要支付: 180 元[[email protected] data]# ./test.py 請輸入商品金額:100需要支付: 80 元[[email protected] data]# ./test.py 請輸入商品金額:80需要支付: 80 元[[email protected] data]#

三:pass語句:

作用:

通常用來填充文法空白

num = int(input("請輸入一個數字1-4:"))if 1 <= num <= 4:    passelse:    print("輸入有錯")###########[[email protected] data]# ./test.py 請輸入一個數字1-4:3[[email protected] data]# ./test.py 請輸入一個數字1-4:7輸入有錯[[email protected] data]#

---------------------------------------------------------------------------------------------

布爾運算:

運算子:

notand   or

布爾非操作: not

文法: not  x

作用:對x 進行布爾取非,如bool(x)為True則返回False,否則返回True


布爾與操作:and

文法:xand y 

註:x,y 代表運算式。

作用: 優先返回假值得對象,當x的布爾值為Fals時,返回x,否則返回y.

示意:True and True  # 返回True

  True and False #返回False

  False and True #返回False

  Fales and False #返回False

x = int(input("請輸入一個月份:"))if 1 <= x and x <= 12:        print("合法的月份")else:        print("月份不合法") ###[[email protected] data]# ./test.py 請輸入一個月份:1合法的月份[[email protected] data]# ./test.py 請輸入一個月份:8合法的月份[[email protected] data]# ./test.py 請輸入一個月份:13月份不合法

布爾或運算子:or

文法:x or y

作用:優先返回真值對象,當x為True時,返回x,否則返回y

示意:

True or True # True

True or False #True

Flase or True # True

Flase or False # False

x = int(input("請輸入一個月份:"))if x < 1 or x > 12:print("輸入有誤")##[[email protected] data]# ./test.py 請輸入一個月份:13輸入有誤[[email protected] data]#


python if 語句,布爾運算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.