基本運算符,流程控制之if判斷

1、基本運算符ide

上篇講了基本運算符:算數運算符,比較運算符索引

本篇講:賦值運算符,邏輯運算符字符串

一、賦值運算符input

(1)增量賦值it

#也能夠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)鏈式賦值class

#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)

二、邏輯運算符註釋

1.1補充

 條件:只要能獲得True 或 False兩種值的東西都能當作條件
  一、顯式的布爾值 : 表面上看就直接是True或False
(1)比較運算的結果
print(10 > 3)
print(10 == 3)
(2)變量值直接就是True或False
tag = True

  二、隱式的布爾值 :表面上看上去是一種值,在底層會被解釋器轉換成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)

2、流程控制之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('後續代碼。。。')
相關文章
相關標籤/搜索