一、基本運算子
上篇講了基本運算子:算數運算子,比較運算子
本篇講:賦值運算子,邏輯運算子
1、賦值運算子
(1)增量賦值
#也可以age-=1 age*=1 age/=1 age//=1 age%=1 1也可以變成其他,注意靈活運用,一下是一個模板分析 age = 18 age += 1 # age = age + 1 print(age)
(2)交叉賦值
#x,y =y,x 就是被注釋的那三行的轉換,簡單成一橫 x = 111 y = 222 # temp=x # x=y # y=temp x, y = y, x print(x) print(y)
(3)鏈式賦值
#x = y = z = 10就是那三橫的轉換,簡化成一橫 #x=10 #y=x #z=y x = y = z = 10 print(id(x),id(y),id(z)) #id相同
(4)解壓賦值
PS:解壓賦值:多/少一個變數名就會報錯;一般用于取開頭或結尾幾個值;如果要取中間的值用切片(后期會講);字典取出來的是key;字串取出來的是每個元素
#最原始的 salaries = [111, 222, 333, 444, 555] mon0 = salaries[0] mon1 = salaries[1] mon2 = salaries[2] mon3 = salaries[3] mon4 = salaries[4] #運用解壓賦值后 salaries = [111, 222, 333, 444, 555] mon0,mon1,mon2,mon3,mon4=salaries print(mon0) print(mon1) print(mon2) print(mon3) print(mon4) # 注意1: 變數名與值的個數必須一一對應 # mon0,mon1,mon2,mon3,mon4,mon5=salaries # 多一個變數名不行,報錯 # mon0,mon1,mon2,mon3=salaries # 少一個變數名不行,報錯
salaries = [111, 222, 333, 444, 555] #取前兩個值 # mon0,mon1,*_=salaries # print(mon0) # print(mon1) # print(_) #取后兩個值 # *_,x,y=salaries # print(x,y) # print(_) #取前后兩個值 # x,*_,y,z=salaries # print(x) # print(y) # print(z) #取中間部分的值,一般不這么用 # _,*midlle,_=salaries # print(midlle) #字典解壓賦值,取出的是key,再用索引取到值 # dic={'k1':111,'k2':222,'k3':3333} # x,y,z=dic # print(x,y,z) # print(dic[x],dic[y],dic[z]) #字串解壓賦值,元素個數要與要解壓的值的個數必須一一對應,多/少一個都不行,會報錯 # x, y, z,a,b = "hello" # print(x)
2、邏輯運算子
1.1補充
條件:只要能得到True 或 False兩種值的東西都能當做條件
1、顯式的布林值 : 表面上看就直接是True或False
(1)比較運算的結果 print(10 > 3) print(10 == 3) (2)變數值直接就是True或False tag = True
2、隱式的布林值 :表面上看上去是一種值,在底層會被解釋器轉換成True或False
ps:0、None、空對應的布林值為False,其余值對應的布林值均為True
1.2邏輯運算子:用來連接多個條件
(1) not 條件:對條件的結果取反
(2) 條件1 and 條件2:連接左右兩個條件,兩個條件必須都為True,最終結果才為True
(3) 條件1 or 條件2:連接左右兩個條件,兩個條件只要有一個為True,最終結果就為True
ps:偷懶原則=》短路運算
(4) 優先級:not > and > or,推薦用括號去標識優先級
# False or True res1=(3 > 4 and 4 > 3) or (not (1 == 3 and 'x' == 'x')) or 3 > 3 print(res1) res2= 3 > 4 and 4 > 3 or not 1 == 3 and 'x' == 'x' or 3 > 3 print(res) res2= (3 > 4 and 4 > 3) or (not 1 == 3 and 'x' == 'x') or 3 > 3 res3= 3 > 4 and 4 > 3 or not 1 == 3 and 'x' != 'x' or 3 > 3 print(res3) print(10 and 0) print(10 and False) print(10 or False)
二、流程控制之if判斷
ps:(1)可以只有if部分(2)可以只有if...elif...elif...(3)可以只有if...else...(4)if里面可以嵌套if
注意:if的條件可以放:顯示 隱式bool或and or not連接的
if條件是頂級代碼
縮進同一空格的屬于同一組代碼塊,從上至下依次運行
條件2能運行的前提是條件1不成立
if判斷的完整語法:
if 條件1:
代碼1
代碼2
代碼3
...
elif 條件2:
代碼1
代碼2
代碼3
...
elif 條件3:
代碼1
代碼2
代碼3
...
...
else:
代碼1
代碼2
代碼3
...
# 語法1: """ if 條件1: 代碼1 代碼2 代碼3 ... """ gender = "female" age = 18 is_beautiful = True if gender == "female" and 60 >= age >= 18 and is_beautiful: print('開始表白,,,') print('后續代碼,,,')
# 語法2: """ if 條件1: 代碼1 代碼2 代碼3 ... else: 代碼1 代碼2 代碼3 ... """ gender = "female" age = 70 is_beautiful = True if gender == "female" and 60 >= age >= 18 and is_beautiful: print('開始表白,,,') else: print('阿姨好,開個玩笑,,,') print('后續代碼,,,')
# 語法3: """ if 條件1: 代碼1 代碼2 代碼3 ... elif: 代碼1 代碼2 代碼3 ... """ # 如果:成績 >= 90 那么:優秀 # # 如果成績 >= 80 且 < 90, 那么:良好 # # 如果成績 >= 70 且 < 80, 那么:普通 # # 其他情況:很差 score = input("請輸入你的成績:") # score = "93" score=int(score) if score >= 90: print("優秀") elif score >= 80: print("良好") elif score >= 70: print("普通") else: print("很差") print('其他代碼,,,,')
# 語法4: """ if的嵌套 """ gender = "female" age = 18 is_beautiful = True is_successful = True if gender == "female" and 60 >= age >= 18 and is_beautiful: print('開始表白,,,') if is_beautiful: print('在一起') else: print('再見,,,') else: print('阿姨好,開個玩笑,,,') print('后續代碼,,,')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/93494.html
標籤:Python
