Python基礎學習 02-while-字串格式化-運算子-編碼
阿新 • • 發佈:2020-11-17
目錄
Day02
while
while 關鍵字(死迴圈)
if 條件:
結果
while 條件:
迴圈體
print(1)
while True:
print("我會一直迴圈,不會列印2")
print(2)
falg = True
while falg:
print("我會一直迴圈,不會列印2")
print(2)
數字中非零的都是True
print(bool(0))
正序的規範
count = 1 while count <= 5: print(count) count += 1 正序列印從25-57 count = 25 while count <= 57: print(count) count += 1
倒序的規範
count = 5
while count:
print(count)
count -= 1
倒序列印從57-25
count = 57
while count >= 25:
print(count)
count -= 1
break和continue
while True: print(1) print(2) break # 終止當前迴圈,break下方的程式碼不會進行執行 print(3) # 不會執行 print(4) # 1 # 2 # 4 while True: print(1) print(2) continue # 跳出當前迴圈繼續下次迴圈(偽裝成迴圈體中的最後一行程式碼) print(3) # 不會執行 print(4) # 不會執行
while else
while True:
print(1)
break
else:
print(2) # 不會被執行
while True:
print(1)
break
print(2) # 會被在執行
總結
打破迴圈的方式:
1:修改條件
2:break
break:打破當前迴圈(終止當前迴圈)
continue:跳出當前迴圈繼續下次迴圈(continue偽裝成當前迴圈體中的最後一行程式碼)
break和continue相同之處: 以下的程式碼都不執行
字串的格式化
%s 是佔的字串型別的位置 %d 是佔的數字型別的位置 %% 轉換成普通的%號 按照位置順序船體,佔位和補位必須一一對應 s = """ ------------- info ------------- name:%s age:%s job:%s -------------- end ------------- """ name = input("name") age = int(input("age")) job = input("job") print(s%(name,age,job)) num = input("學習進度: ") stu = "小明學習進度為: %s%%" print(stu%(num)) s = f"今天是周{input('幾')}" print(s) stu = "小明學習怎麼樣? %s" print(stu%("針不戳")) s = f"{1}{2}{3}" print(s)
運算子
算數運算子
+ 加
- 減
* 乘
/ 除 python2獲取的值是整數,python3獲取的值是浮點數
// 整除
** 冪(次方)
% 模(取餘)
比較運算子
>
<
==
!=
>=
<=
賦值運算子
= 賦值
+= 自加
-= 自減
*= 自乘
/= 自除
//=
**=
%=
邏輯運算子
and 與/和
or 或
not 非
and 都為真的時候取and後邊的值
and 都為假的時候取and前面的值
and 一真一假的時候取假的
or 都為真的時候取or前邊的值
or 都為假的時候取or後邊的值
or 一真一假的時候取真的
執行權重
() > not > and > or
成員運算子
in 存在
not in 不存在