學習筆記19:Python賦值運算符,轉義字符及while循環

(1)while循環基本使用:while語句以及縮進部分是一個完整的代碼塊。

初始條件設置 - 通常是重複執行的計數器
while 條件(判斷 計數器 是否到達 目標次數):
	滿足條件,執行代碼行1
	滿足條件,執行代碼行2
	......
	處理條件(計數器+1)

練習1:打印3遍hello python

#1.定義一個整數變量,記錄循環次數
i = 1
#2.開始循環
while i<=3:
    print("hello python")
    i=i+1
執行結果:
F:\python-liuboke\venv\Scripts\python.exe "F:/python-liuboke/打印5遍hello python.py"
hello python
hello python
hello python

Process finished with exit code 0

死循環:在循環體內部未修改循環的判斷句,導致程序無法終止

#1.定義一個整數變量,記錄循環次數
i = 1
#2.開始循環
while i<=3:
    print("hello python")
  #  i=i+1 註釋計數條件會使程序一直輸出hello python,程序無法終止

(2)賦值運算符:使用=給變量賦值,賦值運算符中間不能有空格

運算符 描述
= 簡單賦值;c=a+b將a和b的相加結果賦值給c
+= 加法賦值;c+=a <> c=c+a
-= 減法賦值;c-=a <> c=c-a
*= 乘法賦值;c*=a <> c=c*a
/= 除法賦值;c/=a <> c=c/a
//= 取整除賦值;c//=a <> c=c//a
%= 取模(餘數)賦值;c%=a <> c=c%a
**= 冪賦值;c**=a <> c=c**a

練習1加入賦值運算符:

#1.定義一個整數變量,記錄循環次數
i = 1
#2.開始循環
while i<=3:
    print("hello python")
   # i=i+1
    i+=1

練習2 :while實現循環計數

#計算0-100之間所有數字的累計求和結果
# 0.定義最終結果的變量
result = 0
# 1.定義一個整數變量記錄循環次數
i = 0
# 2.開始循環
while i <=100:
   # print(i)
    #每次循環,result這個變量和i計數器相加
    result += i
    #處理計數器
    i += 1
print( "0-100之間的求和結果 = %d" % result)
執行結果:
D:\py\python.exe D:/py/liuna/循環計數.py
0-100之間的求和結果 = 5050

Process finished with exit code 0
#計算0-100之間所有偶數數字的累計求和結果
# 0.定義最終結果的變量
result = 0
# 1.定義一個整數變量記錄循環次數
i = 0
# 2.開始循環
while i <=100:
    # 判斷變量i的數值是否是一個偶數
    if i % 2 == 0:
        result += i
    #處理計數器
    i += 1
print( "0-100之間的偶數求和結果 = %d" % result)
執行結果:
D:\py\python.exe D:/py/liuna/循環計數.py
0-100之間的偶數求和結果 = 2550

Process finished with exit code 0

(3)break 和 continue:只針對當前循環有效

break 某一條件滿足時,退出循環,不再執行後續重複代碼
continue 某一條件滿足時,不執行後續重複的代碼,跳到循環判斷條件

練習3:break語句在循環中使用

i = 0
while i < 10:
    # break 某一條件滿足時,退出循環,不再執行後續重複代碼
    # i == 2
    if i == 2:
        break
    print(i)
    i += 1
print("over")
執行結果:
D:\py\python.exe D:/py/liuna/break.py
0
1
over
Process finished with exit code 0

練習4:continue在循環中使用

i = 0
while i < 5:
    # continue 某一條件滿足時,不執行後續重複的代碼,跳到循環判斷條件
    # i == 2
    if i == 2:
        #注意在循環中continue關鍵字的使用需確認循環計數是否修改,以防導致死循環
        i += 1
        continue
    print(i)
    i += 1
執行結果:
D:\py\python.exe D:/py/liuna/continue.py
0
1
3
4
Process finished with exit code 0

(4)循環嵌套

while 條件1:
	滿足條件1,執行代碼行1
	滿足條件1,執行代碼行2
	......
	while 條件2:
		滿足條件2,執行代碼行1
		滿足條件2,執行代碼行2
		......
		處理條件2
	處理條件1

練習5:控制檯輸出5行*號,每行依次遞增

'''
#方法1:使用字符串的*法操作
#定義一個計數器變量,這裏從1開始
row = 1
#開始循環
while row <= 5:
    print("* " * row)
    row +=1
#print函數額外知識點:print函數輸出內容之後,會自動換行,若想使輸出內容在同一行,可以在print輸出內容之後增加,end=""
'''
#方法2:循環嵌套
row = 1
while row <= 5:
    #每一行要打印的星星與當前的行數是一致的
    #增加一個小循環,專門負責當前行中,每一“列”的星星顯示
    #1.定義一個列計數器變量
    col = 1
    while col <= row:
    #2.開始循環
        #print("%d" % col)
        print("*",end="")
        col += 1
    #print("第 %d 行" % row)
    print("") #一行星星輸出之後添加換行
    row+=1
執行結果:
D:\py\python.exe D:/py/liuna/小星星.py
*
**
***
****
*****

Process finished with exit code 0

(5)轉義字符

轉義字符 描述
\\ 反斜槓符號
\’ 單引號
\" 雙引號
\n 換行符
\t 製表符,在輸出文本時垂直方向按列對齊文本

練習6:99乘法表

row = 1
while row <= 9:
    col = 1
    while col <= row:
        print(" %d * %d = %d" %(col,row,col*row),end="\t")
        col += 1
    print("")
    row += 1
執行結果:
D:\py\python.exe D:/py/liuna/99乘法表.py
 1 * 1 = 1	
 1 * 2 = 2	 2 * 2 = 4	
 1 * 3 = 3	 2 * 3 = 6	 3 * 3 = 9	
 1 * 4 = 4	 2 * 4 = 8	 3 * 4 = 12	 4 * 4 = 16	
 1 * 5 = 5	 2 * 5 = 10	 3 * 5 = 15	 4 * 5 = 20	 5 * 5 = 25	
 1 * 6 = 6	 2 * 6 = 12	 3 * 6 = 18	 4 * 6 = 24	 5 * 6 = 30	 6 * 6 = 36	
 1 * 7 = 7	 2 * 7 = 14	 3 * 7 = 21	 4 * 7 = 28	 5 * 7 = 35	 6 * 7 = 42	 7 * 7 = 49	
 1 * 8 = 8	 2 * 8 = 16	 3 * 8 = 24	 4 * 8 = 32	 5 * 8 = 40	 6 * 8 = 48	 7 * 8 = 56	 8 * 8 = 64	
 1 * 9 = 9	 2 * 9 = 18	 3 * 9 = 27	 4 * 9 = 36	 5 * 9 = 45	 6 * 9 = 54	 7 * 9 = 63	 8 * 9 = 72	 9 * 9 = 81	

Process finished with exit code 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章