1. 程式人生 > 實用技巧 >4.運算子

4.運算子

運算子

一、算數運算

a=10 b=20

二、比較運算

a=10 b=20

三、賦值運算

a =10 b=20

四、邏輯運算

  • and 真真=True,否則為False

  • or 有一個為真則=Ture

  • not 取相反

  • ()優先運算

  • 同級運算從左到右

邏輯運算口訣:比大小等不等,並且或者分先後,帶括號的優第一,同級運算左往右

  • 例項

    # print(0 or 4 and 3 or 7 or 9 and 6)         #0為False   非0的都為True
    print("------------------")                   #運算and,運算時有0則取值為0(有一個為False時,結果就為False)
                                                  #當兩個值都為真時,把後者(起決定結果的值)作為輸出值
    print(4 and 0)                                #輸出為0
    print(9 and 6)                                #輸出為6
    print(6 and 9)                                #輸出為9
    
    print("------------------")                   #運算or,運算時會把第一個真值(決定結果的值)作為輸出值
                                                  #當兩個值都為偽時,結果輸出為0
    print(0 or 0)                                 #輸出為0
    print(0 or 3)                                 #輸出為3
    print(3 or 7)                                 #輸出為3
    print(7 or 3)                                 #輸出為7
    
    print("------------------")                   #運算not,取相反的邏輯結果輸出
                                                  #not(真)則為偽;not(偽)則為真
    print(not(0 or 0))                            #輸出True
    print(not(0 or 3))                            #輸出False
    print(not(1 and 2))                           #輸出False
    print(not(0 and 3))                           #輸出True
    

五、成員運算

in not in :

判斷子元素是否在原字串(字典,列表,集合)中:

例如:

#print('喜歡' in 'dkfljadklf喜歡hfjdkas')            #輸出True
#print('a' in 'bcvd')                               #輸出False
#print('y' not in 'ofkjdslaf')                      #輸出True