# 不知道該下啥標題 --- # 迴圈中奇怪的東西 ---- # range() ---- ```python for i in range(): ... ``` ---- #### 生成一個等差數列 ```python a=list(range(5, 10))#[5, 6, 7, 8, 9] b=list(range(0, 10, 3))#[0, 3, 6, 9] c=list(range(-10, -100, -30))#[-10, -40, -70] ``` ---- # continue ---- ## continue可以將迴圈推回起點,但其中的值不會變動 ---- ```python for num in range(2, 10): if num % 2 == 0: print("找到一個偶數", num) continue print("找到一個奇數", num) ``` ---- # break ---- ## break可以直接中斷整個迴圈 ---- ## break example ```python for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, '等於', x, '*', n//x) break else: print(n, '是一個質數') ``` ---- #### 對,你沒看錯,這不是錯誤程式碼,else確實隸屬於for ---- # for...else ---- ### 在 for 迴圈中,else子句會在迴圈完成後執行。 ```python for i in range(10): print(i) else: print("完整迴圈結束執行") ``` ---- ### 也就代表,只要中斷迴圈循環,就不會觸發else子句 ```python for i in range(10): print(i) break else: print("完整迴圈結束執行") ``` ---- # while...else ---- ### 在 while 迴圈中,它會在迴圈條件變為 false 後執行。 ```python a=0 while a<10: print(a) a+=1 else: print("變數a>=10") ``` ---- ### 相同的,中斷迴圈後,就不會觸發else子句 ```python a=0 while a<10: print(a) a+=1 else: print("變數a>=10") ``` --- ## 更多奇怪的東西 ---- # pass ---- ### 如同字面意思->跳過 ```python for i in range(10): pass print("嗨") ``` ---- ## 有什麼用? * 為程式做預留 * 製作一個框架 * anymore? ---- # match ---- ## 同個變數的多次比較 ---- ```python status=int(input()) if status==400: print("Bad request") elif status==401: print("Unauthorized") elif status==403: print("forbidden") elif status==404: print("Not found") elif status==418: print("I'm a teapot") else: print("Something's wrong with the internet") ``` ---- ```python status = int(input()) match status: case 400: print("Bad request") case 401: print("Unauthorized") case 403: print("forbidden") case 404: print("Not found") case 418: print("I'm a teapot") case _: print("Something's wrong with the internet") ``` --- ## 一個小觀念 ---- # =(賦值) ##### 他不是等於(==)歐~ ---- ```python a = ["柯o哲","侯o宜","郭o銘","賴o德"] b = a print(a) #["柯o哲","侯o宜","郭o銘","賴o德"] print(b) #["柯o哲","侯o宜","郭o銘","賴o德"] ``` ---- ```python del b[2] # 刪除 b list 中第三個元素 "郭o銘" print(a) #["柯o哲","侯o宜","賴o德"] print(b) #["柯o哲","侯o宜","賴o德"] ``` ---- ```python a = ["柯o哲","侯o宜","郭o銘","賴o德"] b = a print(id(a)) print(id(b)) ``` ---- ```py a = ["柯o哲","侯o宜","郭o銘","賴o德"] b = a.copy() ``` ---- [詳細解釋](https://www.796t.com/content/1545161954.html)
{"description":"list(range(5, 10))[5, 6, 7, 8, 9]","contributors":"[{\"id\":\"3963913a-1955-4863-b231-e15edfb3078e\",\"add\":5202,\"del\":2729}]","title":"迴圈延伸"}
    127 views
   Owned this note