加+、減-、乘*、除/、取整//、取餘%、**冪
等於 ==、不等於 !=、不等於 <>、大於 >、小於 <、大於等於 >=、小於等於 <=
c=a+b 將 a+b的運算結果賦值給 c,c += a 等效於 c=c+a
c* *=a等效於c=c**a
and、or、not
與、或、非
is和==的區別: is用於判斷兩個變數引用物件是否為同乙個(記憶體空間), ==用於判斷引用變數的值是否相等。
按位運算子是把數字當作二進位制來進行計算的。
在序列、列表、元組裡找值。
in/not in
if 條件:
**1**2
**3...
# 注意縮排
cls = 'human'
gender = 'female'
age = 18
if cls == 'human' and gender == 'female' and age >16 and < 22:
print('開始表白')
print('end...')
開始表白
end...
if 條件:
**1**2
**3...
else:
**1**2
**3...
if...else表示if成立**成立會幹什麼,else不成立會幹什麼。
cls = 'human'
gender = 'female'
age = 38
if cls == 'human' and gender == 'female' and age > 16 and < 22:
print('開始表白')
else:
print('阿姨好')
阿姨好
if 條件1:
**1**2
**3...
elif 條件2:
**1**2
**3...
elif 條件3:
**1**2
**3...
if...elif..else表示if條件1成立幹什麼,elif條件2成立什麼幹什麼,elif條件3成立幹什麼,elif...否則幹什麼。
cls = 'human'
gender = 'female'
age = 28
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('開始表白')
elif cls == 'human' and gender == 『female' and age > 22 and age < 30:
print('考慮下')
else:
print('阿姨好')
考慮下
# if的巢狀
cls = 'human'
gender = 'female'
age = 18
is_success = false
if cls == 'human' and gender == 'femal' and age >16 and age < 22:
print('開始表白')
if is_success:
print('那我們一起走把...')
else:
print('我逗你玩呢')
else:
print('阿姨好')
迴圈就是乙個重複的過程,計算機重複幹乙個活,while迴圈又稱為條件迴圈。
while 條件
code 1
code 2
code 3
...while true:
print('*1'*100)
print('*2'*100)
# 實現atm的輸入密碼重新輸入的功能
while true:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username:')
inp_pwd = input('password:')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
else:
print('username or password error')
上述**雖然實現了功能,但是使用者密碼輸對了,他也會繼續輸入。
break的意思是終止掉當前層的迴圈,執行其他**。
while true:
print('1')
print('2')
break
print('3')
1
2
while true:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username:')
inp_pwd = input('password:')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
break
else:
print('username or password error')
print('退出了while迴圈')
username:nick
password: 123
login successful
退出了while迴圈
continue的意思是終止本次迴圈,直接進入下一次迴圈.
n = 1
while n < 4:
print(n)
n += 1
123
n = 1
while n < 10 :
if n == 8 :
n += 1 # 如果注釋這一行,則進入死迴圈
continue
print(n)
n += 1
continue不能加在迴圈體的最後一步執行**,因為**加上去毫無意義,如下所示的continue所在的位置就是毫無意義的。ps:(注意是最後一步執行的**,而不是最後一行)
while true:
if 條件1:
code1
code2
code3
...else:
code1
code2
code3
...continue
atm密碼輸入成功還需要進行一系列的命令操作,比如取款,比如轉賬。並且在執行功能結束後會退出命令操作的功能,即在功能出執行輸入q會退出輸出功能的while迴圈並且退出atm程式。
while true:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username:')
inp_pwd = input('password:')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while true:
cmd = input('請輸入你需要的命令:')
if cmd == 'q':
break
print(f' 功能執行')
break
else:
print('username or password error')
print('退出了while迴圈')
# tag控制迴圈退出
tag = true
while tag:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username:')
inp_pwd = input('password:')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while tag:
cmd = input('請輸入你需要的命令:')
if cmd == 'q':
tag = false
print(f' 功能執行')
else:
print('username or password error')
print('退出了while迴圈')
n = 1
while n < 3:
print(n)
n += 1
else:
print('else會在while沒有被break時才會執行else中的**')
1
2else會在while沒有被break時才會執行else中的**
day04課堂小結
a 1 b 1 c 1 d 1 print f a b c d a 1,b 1,c 1,d 1 a b c d 1 print f a b c d x 10 y 20 x變成20,y變成10,不讓原來的10和20被垃圾 機制 設乙個中間變數 z x x y y z print f x print f...
day03課堂小結
ctrl c 複製,預設複製整行 ctrl v 貼上 ctrl z 撤銷 ctrl x 剪下,預設剪下整行 ctrl a 全選 ctrl f 查詢 選中批量修改 shift ctrl z 反撤銷 ctrl d 複製貼上選中內容,沒有選中預設整行 ctrl backspace 刪除乙個單詞 ctrl ...
裝飾器day12課堂小結
閉包 閉是封閉 函式內部函式 包是包含 該內部函式對外部作用域而非全域性作用域的變數的引用 閉包指的是 函式內部函式對外部作用域而非全域性作用域的引用。為函式傳參的方式一 使用引數的形式 def func x print x func 1 func 1 func 1 1 11為函式傳參的方式二 包給...